Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Sql server query help


Sql server query help

Author
Message
mydanes
mydanes
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
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
Steven Willis
Steven Willis
SSC-Addicted
SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)

Group: General Forum Members
Points: 477 Visits: 1721
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



mydanes
mydanes
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
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??
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4253 Visits: 6431
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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Steven Willis
Steven Willis
SSC-Addicted
SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)

Group: General Forum Members
Points: 477 Visits: 1721
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'






 
Attachments
XML_Output Example.png (1 view, 16.00 KB)
Steven Willis
Steven Willis
SSC-Addicted
SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)SSC-Addicted (477 reputation)

Group: General Forum Members
Points: 477 Visits: 1721
Yeah, I suppose I can over-complicate things at times. :-P

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

 
mydanes
mydanes
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
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!
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