Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Split string variable into separate values

Author  Topic 

Bex
Aged Yak Warrior

580 Posts

Posted - 2013-12-30 : 12:26:50
I have a string:

'RN-CITC050021-5223-B00-000'


I want to select the following results from the string in a single select statement:

SELECT 'RN' AS [Pos1]
, 'CITC050021' AS [Pos2]
, '5223' AS [Pos3]
, 'B00' AS [Pos4]
, '000' AS [Pos5]


I cannot use a user defined function or declare any variables.

Thanks

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-12-30 : 13:42:12
Before I spend any time on this I have a few questions:

are you saying you want results like this?:
Pos1 Pos2       Pos3 Pos4 Pos5
---- ---------- ---- ---- ----
RN CITC050021 5223 B00 000

or this?:

SELECT 'RN'    AS [Pos1]
, 'CITC050021' AS [Pos2]
, '5223' AS [Pos3]
, 'B00' AS [Pos4]
, '000' AS [Pos5]

Does your string always have exactly 5 parts?
Is the separator always a hyphen?
Is each of the parts always the same length?
Does this need to work on a whole column of strings or just a single value?

I'm curious, why can't you use variables or UDFs?


Be One with the Optimizer
TG
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-12-30 : 16:22:02
No response so here is a solution - assuming always 5 parts separated by hyphens and each part can be any length.

;with yourTable (s) as
(
select 'RN-CITC050021-5223-B00-000' union all
select 'Z-AB1-123-A0-23' union all
select 'ZZZ-F-AB-K-69' union all
select 'not valid' union all
select 'not-valid'
)

,c2 (s,p,i) as
(--last value
select s
,right(s, charindex('-', reverse(s))-1)
,len(s)
from yourTable
--make sure we have a 5 part string
where len(s) - len(replace(s, '-','')) = 4
)
,c3 (s,p,i) as
(--first through fourth values
select s
,substring(s, 1, charindex('-', s)-1)
,charindex('-', s)
from c2
where charindex('-', s) > 0
union all
select s
,substring(s, i+1, charindex('-', s, i+1)-(i+1))
,charindex('-', s, i+1)
from c3
where charindex('-', s, i+1) > 0
)
,c4 (s,p,i) as
(--combine the last value with the first four
select * from c3
union all
select * from c2
)
--Pivot the vaues into columns
select s
,left([1], 15) as pos1
,left([2], 15) as pos2
,left([3], 15) as pos3
,left([4], 15) as pos4
,left([5], 15) as pos4
from (--name the values that will become columns
select s
,pos = row_number() over (partition by s order by i)
,p
from c4
) d
pivot (
max(p)
for [pos] in ([1],[2],[3],[4],[5])
) p

OUTPUT:

s pos1 pos2 pos3 pos4 pos4
-------------------------- --------------- --------------- --------------- --------------- ---------------
RN-CITC050021-5223-B00-000 RN CITC050021 5223 B00 000
Z-AB1-123-A0-23 Z AB1 123 A0 23
ZZZ-F-AB-K-69 ZZZ F AB K 69


Be One with the Optimizer
TG
Go to Top of Page

Bex
Aged Yak Warrior

580 Posts

Posted - 2013-12-31 : 05:14:35
Hi there

Sorry for not responding, I had gone home (on UK time). Anyway, the reason I don't have the freedom to use variables, etc is because of the application that will be calling the sql. It is very limited. Anyway, all your assumptions were spot on, so thank you for taking the time to give me a solution.

Much appreciated!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-12-31 : 13:24:58
quote:
Originally posted by Bex

Hi there

Sorry for not responding, I had gone home (on UK time). Anyway, the reason I don't have the freedom to use variables, etc is because of the application that will be calling the sql. It is very limited. Anyway, all your assumptions were spot on, so thank you for taking the time to give me a solution.

Much appreciated!


see alternate approaches here

http://visakhm.blogspot.in/2010/02/parsing-delimited-string.html

http://visakhm.blogspot.in/2013/01/delimited-string-split-xml-parsing.html

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2014-01-06 : 02:12:47
You may be interested to read this too http://beyondrelational.com/modules/2/blogs/70/posts/10844/splitting-delimited-data-to-columns-set-based-approach.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -