SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL Function to remove excess characters


SQL Function to remove excess characters

Author
Message
kwoznica
kwoznica
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1061 Visits: 481
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.
pietlinden
pietlinden
SSChampion
SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)SSChampion (13K reputation)

Group: General Forum Members
Points: 13196 Visits: 14085
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
SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63331 Visits: 18570
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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

Robert klimes
Robert klimes
SSCarpal Tunnel
SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)SSCarpal Tunnel (4.8K reputation)

Group: General Forum Members
Points: 4751 Visits: 3463
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
dwain.c
dwain.c
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16715 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
mickyT
mickyT
SSCrazy
SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)SSCrazy (2.7K reputation)

Group: General Forum Members
Points: 2708 Visits: 3318
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


SQLRNNR
SQLRNNR
SSC Guru
SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)SSC Guru (63K reputation)

Group: General Forum Members
Points: 63331 Visits: 18570
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, MVP


SQL RNNR

Posting Performance Based Questions - Gail Shaw

dwain.c
dwain.c
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16715 Visits: 6431
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. :-P


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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
VSSGeorge
VSSGeorge
SSCrazy
SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)SSCrazy (2.1K reputation)

Group: General Forum Members
Points: 2053 Visits: 1578
dwain, your code is superb one..

Please write the UPDATE syntax also, if want to update the column.
dwain.c
dwain.c
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16715 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search