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

Sql server query help Expand / Collapse
Author
Message
Posted Monday, July 22, 2013 8:29 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 23, 2013 12:51 AM
Points: 3, Visits: 14
Ok I hope I can explain my dilema

I need a sql select statement that will

Select part_number from RCTOOL

the column part_number has values such as

BR643-0034-344-34
AS54-54689
A77360-3454-34456-4
B7374-343-33-32-356-433-11-1

In each of these cases ALL we want it to return is the values UP to the last -
Desired result set from example above
BR643-0034-344
AS54
A77360-3454-34456
B7374-343-33-32-356-433-11

Thank you in advance
Post #1476327
Posted Monday, July 22, 2013 10:59 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
See http://www.sqlservercentral.com/articles/Tally+Table/72993/
to get the code and explanation for use of the DelimitedSplit8K function.


;WITH SampleData (ID,PartNumber)
AS
(
SELECT 1,'BR643-0034-344-34' UNION ALL
SELECT 2,'AS54-54689' UNION ALL
SELECT 3,'A77360-3454-34456-4' UNION ALL
SELECT 4,'B7374-343-33-32-356-433-11-1'
)
SELECT
r.PartNumber
,LEFT(r.PartNumber,LEN(r.PartNumber)-LEN(r.Item)-1) AS NewPartNumber
FROM
(
SELECT
ID
,PartNumber
,ItemNumber
,Item
,(MAX(ItemNumber) OVER (PARTITION BY PartNumber)) AS MaxItem
FROM
SampleData AS s
CROSS APPLY
dbo.DelimitedSplit8K(s.PartNumber,'-') AS dsk
) r
WHERE
r.ItemNumber = r.MaxItem
ORDER BY
r.ID


Post #1476342
Posted Monday, July 22, 2013 11:12 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 23, 2013 12:51 AM
Points: 3, Visits: 14
Thank you for the reply and query - this table could have thousands of values I was just trying to give you an IDEA of some of the values but the key is we ALWAYS want it to yield everything UP to the last - in the record? Will what you gave us work on more than just those four examples I gave you that are in the 'Sample" data section??
Post #1476345
Posted Tuesday, July 23, 2013 12:01 AM


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: Today @ 6:12 PM
Points: 3,609, Visits: 5,220
I'm as big an advocate of DelimitedSplit8K in the SQLverse as you're gonna find, but in this case I'd say there's a simpler way:

;WITH SampleData (ID,PartNumber)
AS
(
SELECT 1,'BR643-0034-344-34' UNION ALL
SELECT 2,'AS54-54689' UNION ALL
SELECT 3,'A77360-3454-34456-4' UNION ALL
SELECT 4,'B7374-343-33-32-356-433-11-1' UNION ALL
SELECT 5,'B7374'
)
SELECT ID, LEFT(PartNumber, LEN(PartNumber)-CHARINDEX('-', REVERSE(PartNumber)))
FROM SampleData


It even handles cases where there is no hyphen at all in the part number.

Note that I don't mean to imply that Steven's solution doesn't handle this case.



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 #1476355
Posted Tuesday, July 23, 2013 12:04 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
I think this would still work for you. But the important point is to take a look at the article link I posted and learn how to use DelimitedSplit8K. That function can split your part numbers into the individual pieces and place them into table form. Then in this case all you have to do is drop the last item and concantenate the others back together or just use string logic as here to do a replace.

The split is the critical part...the method you choose to toss away the undesired value would depend on whether this is a one-time deal or for production. If just a one time conversion, then this would probably do the trick. If I was going to do this in real-time indefinitely in a production environment I'd probably go with doing the split and then using FOR XML PATH to create the new part numbers.

Or turn the entire part number (post-split less the undesired column) into an XML file. The code I posted deals with the issue of an unequal number of elements in each part number, but can be turned into XML easily. Once that's done then the XML can be parsed by a number of different methods.


;WITH SampleData (UniqueID,PartNumber)
AS
(
SELECT 1,'BR643-0034-344-34' UNION ALL
SELECT 2,'AS54-54689' UNION ALL
SELECT 3,'A77360-3454-34456-4' UNION ALL
SELECT 4,'B7374-343-33-32-356-433-11-1'
)
SELECT
CAST(r.UniqueID AS VARCHAR(10)) AS 'ID/@UniqueID'
,(SELECT LEFT(r.PartNumber,LEN(r.PartNumber)-LEN(r.Item)-1)) AS 'ID/@PartNumber'
FROM
(
SELECT
UniqueID
,PartNumber
,ItemNumber
,Item
,(MAX(ItemNumber) OVER (PARTITION BY PartNumber)) AS MaxItem
FROM
SampleData AS s
CROSS APPLY
dbo.DelimitedSplit8K(s.PartNumber,'-') AS dsk
) r
WHERE
r.ItemNumber = r.MaxItem
ORDER BY
r.UniqueID
FOR XML PATH(''), ROOT('root')



<root>
<ID UniqueID="1" PartNumber="BR643-0034-344" />
<ID UniqueID="2" PartNumber="AS54" />
<ID UniqueID="3" PartNumber="A77360-3454-34456" />
<ID UniqueID="4" PartNumber="B7374-343-33-32-356-433-11" />
</root>


Parse XML to Table


EXEC dbo.ParseXMLtoTable
'<root>
<ID UniqueID="1" PartNumber="BR643-0034-344" />
<ID UniqueID="2" PartNumber="AS54" />
<ID UniqueID="3" PartNumber="A77360-3454-34456" />
<ID UniqueID="4" PartNumber="B7374-343-33-32-356-433-11" />
</root>'
,'root'




 


  Post Attachments 
XML_Output Example.png (0 views, 16.75 KB)
Post #1476356
Posted Tuesday, July 23, 2013 12:09 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
Yeah, I suppose I can over-complicate things at times.

There's so many ways to do this stuff that it's easy to get lost in the pursuit.

 
Post #1476358
Posted Tuesday, July 23, 2013 12:26 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, July 23, 2013 12:51 AM
Points: 3, Visits: 14
Thank you BOTH so much - I am good to go and am so thankful for the query help and the wonderful link! Have a great night to you both!
Post #1476365
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse