Discussion:
tricky update
(too old to reply)
mcnewsxp
2012-07-12 14:06:11 UTC
Permalink
i have a Results column that can contain data that looks like this:

XXX X
XXX XX/XXX
XXXX/XXX X/XX

so i want to parse this data into Results1, Results2, Results3 using the '/' as the delimiter.

can i do this in one pass with one sql statement? if so, example please.

tia,
mcnewsxp
Gene Wirchenko
2012-07-12 16:21:04 UTC
Permalink
On Thu, 12 Jul 2012 07:06:11 -0700 (PDT), mcnewsxp
Post by mcnewsxp
XXX X
XXX XX/XXX
XXXX/XXX X/XX
so i want to parse this data into Results1, Results2, Results3 using the '/' as the delimiter.
can i do this in one pass with one sql statement? if so, example please.
GIYF with "SQL Server split string".

Sincerely,

Gene Wirchenko
mcnewsxp
2012-07-12 21:12:27 UTC
Permalink
Post by Gene Wirchenko
On Thu, 12 Jul 2012 07:06:11 -0700 (PDT), mcnewsxp
>
>XXX X
>XXX XX/XXX
>XXXX/XXX X/XX
>
>so i want to parse this data into Results1, Results2, Results3 using the '/' as the delimiter.
>
>can i do this in one pass with one sql statement? if so, example please.
GIYF with "SQL Server split string".
Sincerely,
Gene Wirchenko

Erland Sommarskog
2012-07-12 21:45:47 UTC
Permalink
Post by mcnewsxp
XXX X
XXX XX/XXX
XXXX/XXX X/XX
so i want to parse this data into Results1, Results2, Results3 using the
'/' as the delimiter.
can i do this in one pass with one sql statement? if so, example please.
As long as you only have three fields and there is no period in the data,
parsename comes in handy:

; WITH CTE AS (
SELECT reverse(replace(col, '/', '.')) AS revcol
FROM tbl
)
SELECT reverse(parsename(revcol, 1)) AS results1,
reverse(parsename(revcol, 2)) AS results2,
reverse(parsename(revcol, 3)) AS results3
FROM CTE
--
Erland Sommarskog, SQL Server MVP, ***@sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
Continue reading on narkive:
Loading...