SQL Subquery with delimiter - sql

I need to be able to split one string by the delimiter * into separate columns without including *
The column y from table x looks like this:
column y
*1HS*AB*GXX*123*02*PA45*2013-08-10*
*1R1*B*GX*123*02*PA45*2013-08-10*
*1HS*B*GX*13*01*PA45*2013-08-01*
*1P*C*GXX*123*02*PA45*2013-08-10*
STRING_SPLIT is not avalible
The outcome should be this:
Column1 Column2 Column3 Column4 Column5 Column6 Column7
1HS AB GXX 123 2 PA45 10-08-2013
1R1 B GX 123 2 PA45 10-08-2013
1HS B GX 13 1 PA45 01-08-2013
1P C GXX 123 2 PA45 10-08-2013

will you use the below query..
select RTRIM (REGEXP_SUBSTR (column y, '[^,]*,', 1, 1), ',') AS column 1
, RTRIM (REGEXP_SUBSTR (column y, '[^,]*,', 1, 2), ',') AS column 2
, RTRIM (REGEXP_SUBSTR (column y, '[^,]*,', 1, 3), ',') AS column 3
, LTRIM (REGEXP_SUBSTR (column y, ',[^,]*', 1, 3), ',') AS column 4
from YOUR_TABLE

Unfortunately, string_split() does not guarantee that it preserves the ordering of the values. And, SQL Server does not offer other useful string functions.
So, I recommend using recursive CTEs for this purpose:
with t as (
select *
from (values ('*1HS*AB*GXX*123*02*PA45*2013-08-10*'), ('1HSB*GX*13*01*PA45*2013-08-01*')) v(str)
),
cte as (
select convert(varchar(max), null) as val, 0 as lev, convert(varchar(max), str) as rest,
row_number() over (order by (select null)) as id
from t
union all
select left(rest, charindex('*', rest) - 1), lev + 1, stuff(rest, 1, charindex('*', rest) + 1, ''), id
from cte
where rest <> '' and lev < 10
)
select max(case when lev = 1 then val end) as col1,
max(case when lev = 2 then val end) as col2,
max(case when lev = 3 then val end) as col3,
max(case when lev = 4 then val end) as col4,
max(case when lev = 5 then val end) as col5,
max(case when lev = 6 then val end) as col6,
max(case when lev = 7 then val end) as col7
from cte
where lev > 0
group by cte.id;
Here is a db<>fiddle.

Assuming you can add a table valued function to your database then Jeff Moden's string split function is the best approach I've encountered. It will allow you to maintain order as well.
Find details here

Related

Migrate a Column into Multiple Columns

I have a table with the following columns 'ID, LIST_OF_VALUES'.
Example data is:
ID| LIST_OF_VALUES
--+----------------------------
1 | firstval-secondval-thirdval
2 | val1-val2
3 | val10-val20-val30
4 | singleval
I would like to select the data like this:
ID| VAL1 | VAL2 | VAL3
--+----------+-----------+-------
1 | firstval | secondval | thirdval
2 | val1 | val2 | NULL
3 | val10 | val20 | val30
4 | singlval | NULL | NULL
I am aware of the STRING_SPLIT function. I have tried using it in various ways with Cross Apply, but I can't seem to get the result I want.
I know I can do this using a mess of SUBSTR/INDEX, but I am just curious if STRING_SPLIT offers a more elegant solution.
Just another option
Example of XML Option
Select A.ID
,Val1 = tmpXML.value('/x[1]','varchar(100)')
,Val2 = tmpXML.value('/x[2]','varchar(100)')
,Val3 = tmpXML.value('/x[3]','varchar(100)')
from YourTable A
Cross Apply ( values ( Cast('<x>' + replace([LIST_OF_VALUES],'-','</x><x>')+'</x>' as xml) ) ) B(tmpXML)
Returns
ID Val1 Val2 Val3
1 firstval secondval thirdval
2 val1 val2 NULL
3 val10 val20 val30
4 singleval NULL NULL
Example of JSON Option - as suggested by #PanagiotisKanavos if 2016+
Select A.ID
,Val1 = JSON_VALUE(S,'$[0]')
,Val2 = JSON_VALUE(S,'$[1]')
,Val3 = JSON_VALUE(S,'$[2]')
from #YourTable A
Cross Apply ( values ( '["'+replace(replace([LIST_OF_VALUES],'"','\"'),'-','","')+'"]' ) ) B(S)
Assuming you don't have duplicates, you can use it . . . but it is not trivial:
select t.*, s.*
from t cross apply
(select max(case when seqnum = 1 then value end) as val1,
max(case when seqnum = 2 then value end) as val2,
max(case when seqnum = 3 then value end) as val3
from (select s.value,
row_number() over (order by charindex('-' + value + '-', '-' + t.list_of_values + '-') as seqnum
from string_split(t.list_of_values, '-') s
) s
) s;
Unfortunately, string_split() doesn't provide the ordering. This recreates it using charindex().
One simple and very efficient and scalable way would be to use an ordinal splitter such as dbo.DelimitedSplit8K (or dbo.DelimitedSplitN4K (for nchar/nvarchar). Then the query would be something like this
dbo.DelimitedSplit8K tvf
CREATE FUNCTION dbo.DelimitedSplit8K
--===== Define I/O parameters
(#pString VARCHAR(8000), #pDelimiter CHAR(1))
--WARNING!!! DO NOT USE MAX DATA-TYPES HERE! IT WILL KILL PERFORMANCE!
RETURNS TABLE WITH SCHEMABINDING AS
RETURN
--===== "Inline" CTE Driven "Tally Table" produces values from 1 up to 10,000...
-- enough to cover VARCHAR(8000)
WITH E1(N) AS (
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL
SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1 UNION ALL SELECT 1
), --10E+1 or 10 rows
E2(N) AS (SELECT 1 FROM E1 a, E1 b), --10E+2 or 100 rows
E4(N) AS (SELECT 1 FROM E2 a, E2 b), --10E+4 or 10,000 rows max
cteTally(N) AS (--==== This provides the "base" CTE and limits the number of rows right up front
-- for both a performance gain and prevention of accidental "overruns"
SELECT TOP (ISNULL(DATALENGTH(#pString),0)) ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) FROM E4
),
cteStart(N1) AS (--==== This returns N+1 (starting position of each "element" just once for each delimiter)
SELECT 1 UNION ALL
SELECT t.N+1 FROM cteTally t WHERE SUBSTRING(#pString,t.N,1) = #pDelimiter
),
cteLen(N1,L1) AS(--==== Return start and length (for use in substring)
SELECT s.N1,
ISNULL(NULLIF(CHARINDEX(#pDelimiter,#pString,s.N1),0)-s.N1,8000)
FROM cteStart s
)
--===== Do the actual split. The ISNULL/NULLIF combo handles the length for the final element when no delimiter is found.
SELECT ItemNumber = ROW_NUMBER() OVER(ORDER BY l.N1),
Item = SUBSTRING(#pString, l.N1, l.L1)
FROM cteLen l;
Query
select t.id,
max(case when ds.ItemNumber=1 then ds.Item end) as val1,
max(case when ds.ItemNumber=2 then ds.Item end) as val2,
max(case when ds.ItemNumber=3 then ds.Item end) as val3
from tTable t
cross apply
dbo.DelimitedSplit8K(t.LIST_OF_VALUES, '-') ds
group by t.id
order by t.id;
[EDIT] Here is an alternate method which produces the same output but does not use the DelimitedSplit8K function. This is the same approach as Gordon's but with an outer GROUP BY clause.
;with charindex_split_cte(id, Item, ItemNumber) as (
select t.id, sp.value,
row_number() over (order by charindex('-' + sp.value + '-', '-' + t.list_of_values + '-'))
from tTable t
cross apply string_split(t.list_of_values, '-') sp)
select id,
max(case when ItemNumber=1 then Item end) as val1,
max(case when ItemNumber=2 then Item end) as val2,
max(case when ItemNumber=3 then Item end) as val3
from charindex_split_cte
group by id
order by id;

SQL issues, How can i separate this row of data into multiple column. i want it to separate by the pipe | and put it in its columns

I am trying to separate a one-row data into multiple columns, and I have a pipe | between each data that I wanted to be separated.
I want this one-row data to split into a multi column
1234 |abcd | 123abc | some | more | 0922
to Be like this
col1 col2 col3 col4 col5 col6
1234 abcd 123abc some more 0922
select
[Col1] = SUBSTRING(PostData,1,CHARINDEX('|',PostData)-1) --does what it suppose to
,[Col1] = SUBSTRING(PostData,CHARINDEX('|',PostData)+1,CHARINDEX('|',PostData,CHARINDEX('|',PostData)+1)-CHARINDEX('|', PostData)-1) --does what it suppose to
,[Col1] = SUBSTRING(PostData,CHARINDEX('|',PostData,CHARINDEX('|',PostData)+1)-CHARINDEX('|', PostData)-1,CHARINDEX('|',PostData,CHARINDEX('|',PostData)+1)-CHARINDEX('|', PostData)-1) --i need help with this
,[Col1] = SUBSTRING(PostData,CHARINDEX('|',PostData)+1,CHARINDEX('|',PostData,CHARINDEX('|',PostData)+1)-CHARINDEX('|', PostData)-1)--i need help with this
,[Col1] = SUBSTRING(PostData,CHARINDEX('|',PostData)+1,CHARINDEX('|',PostData,CHARINDEX('|',PostData)+1)-CHARINDEX('|', PostData)-1)--i need help with this
,[Col1] = SUBSTRING(PostData,CHARINDEX('|',PostData)+1,CHARINDEX('|',PostData,CHARINDEX('|',PostData)+1)-CHARINDEX('|', PostData)-1) --i need help with this
,[ID] = REVERSE(SUBSTRING(reverse(PostData),0,CHARINDEX('|',REVERSE(PostData) --does what
it suppose to
from tableName
col1 col2 col3 col4 col5 col6
1234 abcd 123abc some more 0922
what I am getting is:
col1 col2 col3 col4 col5 col6
1234 abcd abcd abcd abcd 0922
One method uses a recursive CTE:
with cte as (
select convert(varchar(max), left(row, charindex('|', row + '|') - 1)) as val,
convert(varchar(max), stuff(row, 1, charindex('|', row) + 1, '')) as rest,
1 as lev, row
from (values ('1234 |abcd | 123abc | some | more | 0922')) v(row)
union all
select convert(varchar(max), left(rest, charindex('|', rest + '|') - 1)) as val,
convert(varchar(max), stuff(rest, 1, charindex('|', rest + '|') + 1, '')) as rest,
lev + 1 as lev, row
from cte
where lev < 5 and rest <> ''
)
select max(case when lev = 1 then val end) as col1,
max(case when lev = 2 then val end) as col2,
max(case when lev = 3 then val end) as col3,
max(case when lev = 4 then val end) as col4,
max(case when lev = 5 then val end) as col5
from cte
group by row;
Here is a db<>fiddle.

SQL extract string where it starts with specific character

I have a column that includes strings that are separated by space and commas. It looks like:
Column1
----------------------------
T1234, C1234, D1234, C1234
E1234, C1234
I need a SQL query to extract anything that starts with C. So the result would look like:
Column1
--------------
C1234, C1234
C1234
This is also an opportunity to use a recursive CTE:
with t as (
select 'T1234, C1234, D1234, C1234' as col1 union all
select 'E1234, C1234'
),
cte as (
select col1,
convert(varchar(max), (case when col1 like 'C%' then ', ' + left(col1, charindex(',', col1 + ',') - 1 ) else '' end)) as c_list,
convert(varchar(max), stuff(col1, 1, charindex(',', col1 + ',') + 1, '')) as rest,
1 as lev
from t
union all
select col1,
c_list + (case when rest like 'C%' then ', ' + left(rest, charindex(',', rest + ',') - 1 ) else '' end) ,
convert(varchar(max), stuff(rest, 1, charindex(',', rest + ',') + 1, '')) as rest,
lev + 1
from cte
where rest > '' and lev < 10
)
select stuff(c_list, 1, 2, '') as c_list
from (select cte.*, row_number() over (partition by col1 order by lev desc) as seqnum
from cte
) cte
where seqnum = 1;
This approach does not require extracting strings and then reaggregating. It also guarantees that the values remain in the original order as in the original data.
Here is a db<>fiddle.
declare #t table (c varchar(200) PRIMARY KEY)
insert into #t values ('T1234, C1234, D1234, C1234'), ('E1234, C1234')
;with cte1 as ( -- cte1 - number the rows. Order by PK
select row_number() over(order by c) rn, *
from #t
), cte2 as ( -- cte2 - turn comma delimited lists into rows of trimmed values
select rn, replace(ca.value, ' ', '') val
from cte1
cross apply (
select value from string_split((select c from cte1 cte1_inner where cte1_inner.rn = cte1.rn), N',')
)ca
), cte3 as ( -- cte3 - get distinct row numbers and re-concat vals in a subquery
select distinct rn, (
SELECT STUFF(
(
select ', ' + val
from cte2 cte2_inner
where val like 'c%' and cte2_inner.rn = cte2.rn
for xml path('')
), 1, 2, '')
)concatenated
from cte2
)
select concatenated
from cte3
Returns:
concatenated
C1234
C1234, C1234
Here's what each CTE returns:
cte1:
rn c
1 E1234, C1234
2 T1234, C1234, D1234, C1234
cte2:
rn val
1 E1234
1 C1234
2 T1234
2 C1234
2 D1234
2 C1234
cte3:
rn concatenated
1 C1234
2 C1234, C1234

How do I pivot in big query

Say I have data
id,col1,col2,col3,col4,col5
1,a,b,c,d,e
and I want the result to be ...
1,a
1,b
1,c
1,d
1,e
How do I pivot on id in big query ?
Below is for BigQuery Standard SQL
#standardSQL
CREATE TEMP FUNCTION cols_to_rows(root STRING) AS (
ARRAY(SELECT REPLACE(SPLIT(kv, ':') [OFFSET(1)], '"', '') cols
FROM UNNEST(SPLIT(REGEXP_REPLACE(root, r'^{|}$', ''))) kv
WHERE SPLIT(kv, ':') [OFFSET(0)] != '"id"'
)
);
SELECT id, col
FROM `project.dataset.table` t,
UNNEST(cols_to_rows(TO_JSON_STRING(t))) col
You can test / play with above using dummy data as below
#standardSQL
CREATE TEMP FUNCTION cols_to_rows(root STRING) AS (
ARRAY(SELECT REPLACE(SPLIT(kv, ':') [OFFSET(1)], '"', '') cols
FROM UNNEST(SPLIT(REGEXP_REPLACE(root, r'^{|}$', ''))) kv
WHERE SPLIT(kv, ':') [OFFSET(0)] != '"id"'
)
);
WITH `project.dataset.table` AS (
SELECT 1 id, 'a' col1, 'b' col2, 'c' col3, 'd' col4, 'e' col5 UNION ALL
SELECT 2 id, 'x', 'y', 'z', 'v', 'w'
)
SELECT id, col
FROM `project.dataset.table` t,
UNNEST(cols_to_rows(TO_JSON_STRING(t))) col
with result as
id col
1 a
1 b
1 c
1 d
1 e
2 x
2 y
2 z
2 v
2 w

MS SQL Server: advanced substring, splitting one string column to multimle columns

I have a column in a table, I would like to separate the contents into different columns, locations of dash is not always same. Please advise more simple code.
Thanks.
COL1
AGH-WH6X-23-4534-OPDQE-QADF
xxx-xxxx-xxxx-xxxx-xxx-xxxx
xxx-xxxx-xxxxxx-xxxx-xxxxx-xx
x-xx-xxxx-xxxxxx-xxxx-xxx-xx
xxx-xx-xxxx-xxxx-xxx-xxx-x
xxx-xxxx-xxxxxx-xxxxxx-xxx-xx
x-xxx-xxxx-xxxx-xxxxxx-xxx-xx
xxx-xxxxx-xxxx-xxxxxx-xxx-xx
Expectation:
COL2 COL3 COL4 COL5 COL6 COL7
AGH WH6X 23 4534 OPDQE QADF
xxx xxxx xxxx xxxx xxx NULL
One method is a recursive CTE with aggregation:
with cte as (
select col1, left(col1, charindex('-', col1 + '-') - 1) as val,
1 as level,
substring(col1, charindex('-', col1) + 1, len(col1)) as rest
from t
union all
select col1, left(rest, charindex('-', rest + '-') - 1),
level + 1,
substring(rest, charindex('-', rest + '-') + 1, len(col1))
from cte
where rest > ''
)
select max(case when level = 1 then val end) as val1,
max(case when level = 2 then val end) as val2,
max(case when level = 3 then val end) as val3,
max(case when level = 4 then val end) as val4,
max(case when level = 5 then val end) as val5,
max(case when level = 6 then val end) as val6,
max(case when level = 7 then val end) as val7
from cte
group by col1;