Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

SQL Function to remove excess characters Expand / Collapse
Author
Message
Posted Wednesday, March 5, 2014 1:19 PM


SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Saturday, June 14, 2014 11:52 AM
Points: 179, Visits: 404
Hello all,

I am looking for a function or way to return only results which does not include appended characters to order numbers.
For instance, below is a list of order numbers. I only want the order number that is SO-123456

OrderNumbers
SO-123456
SO-123456-01
SO-123456-2
SO-123457
SO-123457-1
SO-123457-02
SO-123458


I would like my query to only show the below results

SO-123456
SO-123457
SO-123458

What functions or query methods could achieve this?

I was hoping for something similar to RTRIM but that is only specific to white space.
Any suggestions are welcome.
Post #1547994
Posted Wednesday, March 5, 2014 1:31 PM This worked for the OP Answer marked as solution
Say Hey Kid

Say Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey KidSay Hey Kid

Group: General Forum Members
Last Login: Yesterday @ 10:45 PM
Points: 704, Visits: 4,497
You need CHARINDEX() to find the position of the second dash (if it exists).

This almost works:

use tempdb;
go

SELECT OrderNo
, CHARINDEX('-',OrderNo,4) AS FindDash
, LEFT(OrderNo,CHARINDEX('-',OrderNo,4)) AS NoSuffix
FROM (
SELECT 'SO-123456' AS OrderNo
UNION ALL
SELECT 'SO-123456-01'
UNION ALL
SELECT 'SO-123456-2'
UNION ALL
SELECT 'SO-123457'
UNION ALL
SELECT 'SO-123457-1'
UNION ALL
SELECT 'SO-123457-02'
UNION ALL
SELECT 'SO-123458') x

If you set up your question so that people can answer it really easily, (by providing readily consumable code/SELECT statements), you'll get more answers, because people won't have to work as hard to solve your problem. I usually find that setting up the question properly gets me most of the way to the answer too.

HTH,
Pieter
Post #1548002
Posted Wednesday, March 5, 2014 1:37 PM This worked for the OP Answer marked as solution


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:44 PM
Points: 21,209, Visits: 14,899
Slight fix to the code

use tempdb;
go

SELECT OrderNo
, CHARINDEX('-',OrderNo,4) AS FindDash
, CASE WHEN LEFT(OrderNo,CHARINDEX('-',OrderNo,4)) =''
THEN OrderNo
ELSE LEFT(OrderNo,CHARINDEX('-',OrderNo,4))
END AS NoSuffix
FROM (
SELECT 'SO-123456' AS OrderNo
UNION ALL
SELECT 'SO-123456-01'
UNION ALL
SELECT 'SO-123456-2'
UNION ALL
SELECT 'SO-123457'
UNION ALL
SELECT 'SO-123457-1'
UNION ALL
SELECT 'SO-123457-02'
UNION ALL
SELECT 'SO-123458') x





Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1548005
Posted Wednesday, March 5, 2014 1:45 PM This worked for the OP Answer marked as solution


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 7:34 AM
Points: 1,343, Visits: 2,518
I'll tweak it a little more

SELECT distinct CASE WHEN LEFT(OrderNo,CHARINDEX('-',OrderNo,4)) =''
THEN OrderNo
ELSE substring(OrderNo,1,CHARINDEX('-',OrderNo,4)-1)
END AS NoSuffix
FROM (
SELECT 'SO-123456' AS OrderNo
UNION ALL
SELECT 'SO-123456-01'
UNION ALL
SELECT 'SO-123456-2'
UNION ALL
SELECT 'SO-123457'
UNION ALL
SELECT 'SO-123457-1'
UNION ALL
SELECT 'SO-123457-02'
UNION ALL
SELECT 'SO-123458') x

but based on the sample data if your order numbers are always in the format xx-xxxxxx-xx then a simple substring would work

select substring(OrderNo,1,9) from  (
SELECT 'SO-123456' AS OrderNo
UNION ALL
SELECT 'SO-123456-01'
UNION ALL
SELECT 'SO-123456-2'
UNION ALL
SELECT 'SO-123457'
UNION ALL
SELECT 'SO-123457-1'
UNION ALL
SELECT 'SO-123457-02'
UNION ALL
SELECT 'SO-123458') x
group by substring(OrderNo,1,9) ;



Bob
-----------------------------------------------------------------------------
How to post to get the best help
Post #1548008
Posted Wednesday, March 5, 2014 5:44 PM This worked for the OP Answer marked as solution


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:01 PM
Points: 3,609, Visits: 5,222
This could be done in a possibly more general fashion using DelimitedSplit8K but if you've only got 1 or 2 hyphens, you can also do it like this:

WITH SampleData (OrderNo) AS
(
SELECT 'SO-123456' AS OrderNo
UNION ALL SELECT 'SO-123456-01'
UNION ALL SELECT 'SO-123456-2'
UNION ALL SELECT 'SO-123457'
UNION ALL SELECT 'SO-123457-1'
UNION ALL SELECT 'SO-123457-02'
UNION ALL SELECT 'SO-123458'
)
SELECT OrderNo, NewOrderNo
FROM SampleData a
CROSS APPLY
(
SELECT p1=CHARINDEX('-', OrderNo)
,s1=RIGHT(OrderNo, LEN(OrderNo)-CHARINDEX('-', OrderNo))
) b
CROSS APPLY
(
SELECT NewOrderNo=LEFT(OrderNo, p1+CHARINDEX('-', s1+'-')-1)
) c;


Edit: Note that this also works if there are more than two characters ahead of the first hyphen.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1548055
Posted Wednesday, March 5, 2014 7:12 PM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Yesterday @ 5:20 AM
Points: 950, Visits: 2,876
And yet another variation
SELECT DISTINCT LEFT(OrderNo, ISNULL(NULLIF(CHARINDEX('-',OrderNo,CHARINDEX('-',OrderNo)+1) - 1,-1),LEN(OrderNo)))
FROM (
SELECT 'SO-123456' AS OrderNo
UNION ALL
SELECT 'SO-123456-01'
UNION ALL
SELECT 'SO-123456-2'
UNION ALL
SELECT 'SO-123457'
UNION ALL
SELECT 'SO-123457-1'
UNION ALL
SELECT 'SO-123457-02'
UNION ALL
SELECT 'SO-123458') x

Post #1548066
Posted Thursday, March 6, 2014 7:42 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Yesterday @ 7:44 PM
Points: 21,209, Visits: 14,899
dwain.c (3/5/2014)
This could be done in a possibly more general fashion using DelimitedSplit8K but if you've only got 1 or 2 hyphens, you can also do it like this:

WITH SampleData (OrderNo) AS
(
SELECT 'SO-123456' AS OrderNo
UNION ALL SELECT 'SO-123456-01'
UNION ALL SELECT 'SO-123456-2'
UNION ALL SELECT 'SO-123457'
UNION ALL SELECT 'SO-123457-1'
UNION ALL SELECT 'SO-123457-02'
UNION ALL SELECT 'SO-123458'
)
SELECT OrderNo, NewOrderNo
FROM SampleData a
CROSS APPLY
(
SELECT p1=CHARINDEX('-', OrderNo)
,s1=RIGHT(OrderNo, LEN(OrderNo)-CHARINDEX('-', OrderNo))
) b
CROSS APPLY
(
SELECT NewOrderNo=LEFT(OrderNo, p1+CHARINDEX('-', s1+'-')-1)
) c;


Edit: Note that this also works if there are more than two characters ahead of the first hyphen.


Nice!




Jason AKA CirqueDeSQLeil
I have given a name to my pain...
MCM SQL Server


SQL RNNR

Posting Performance Based Questions - Gail Shaw
Posting Data Etiquette - Jeff Moden
Hidden RBAR - Jeff Moden
VLFs and the Tran Log - Kimberly Tripp
Post #1548286
Posted Thursday, March 6, 2014 5:31 PM This worked for the OP Answer marked as solution


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:01 PM
Points: 3,609, Visits: 5,222
SQLRNNR (3/6/2014)
dwain.c (3/5/2014)
This could be done in a possibly more general fashion using DelimitedSplit8K but if you've only got 1 or 2 hyphens, you can also do it like this:

WITH SampleData (OrderNo) AS
(
SELECT 'SO-123456' AS OrderNo
UNION ALL SELECT 'SO-123456-01'
UNION ALL SELECT 'SO-123456-2'
UNION ALL SELECT 'SO-123457'
UNION ALL SELECT 'SO-123457-1'
UNION ALL SELECT 'SO-123457-02'
UNION ALL SELECT 'SO-123458'
)
SELECT OrderNo, NewOrderNo
FROM SampleData a
CROSS APPLY
(
SELECT p1=CHARINDEX('-', OrderNo)
,s1=RIGHT(OrderNo, LEN(OrderNo)-CHARINDEX('-', OrderNo))
) b
CROSS APPLY
(
SELECT NewOrderNo=LEFT(OrderNo, p1+CHARINDEX('-', s1+'-')-1)
) c;


Edit: Note that this also works if there are more than two characters ahead of the first hyphen.


Nice!


Gee whiz, thanks! And I didn't even GROUP BY NewOrderNo to get only the DISTINCTs!

Perhaps I just wanted to leave a little fun for the OP.



My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1548541
Posted Tuesday, March 11, 2014 6:23 AM


Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 6:03 AM
Points: 588, Visits: 1,061
dwain, your code is superb one..

Please write the UPDATE syntax also, if want to update the column.
Post #1549693
Posted Tuesday, March 11, 2014 6:04 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 6:01 PM
Points: 3,609, Visits: 5,222
Junglee_George (3/11/2014)
dwain, your code is superb one..

Please write the UPDATE syntax also, if want to update the column.


Do you mean like this?

WITH SampleData (OrderNo) AS
(
SELECT 'SO-123456' AS OrderNo
UNION ALL SELECT 'SO-123456-01'
UNION ALL SELECT 'SO-123456-2'
UNION ALL SELECT 'SO-123457'
UNION ALL SELECT 'SO-123457-1'
UNION ALL SELECT 'SO-123457-02'
UNION ALL SELECT 'SO-123458'
)
SELECT OrderNo
INTO #Table
FROM SampleData;

UPDATE a
SET OrderNo = NewOrderNo
FROM #Table a
CROSS APPLY
(
SELECT p1=CHARINDEX('-', OrderNo)
,s1=RIGHT(OrderNo, LEN(OrderNo)-CHARINDEX('-', OrderNo))
) b
CROSS APPLY
(
SELECT NewOrderNo=LEFT(OrderNo, p1+CHARINDEX('-', s1+'-')-1)
) c;

SELECT *
FROM #Table;

GO
DROP TABLE #Table;





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
Post #1549988
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse