How to create multiple columns from a delimited string - sql-server

I have a an inventory field with 4 components, each separated by a "-". I'm not sure how to use charindex to parse the string so that I am returning a column for each value found between delmiters. For example: field value or string = item-color-size-shape.
My goal is to end up with a item as column, color as column, size as a column and finally shape.

If it is max 4 columns you can try like this:
declare #Table table (SomeColumn varchar(100));
insert into #Table
select 'ball-blue-small-round' union all
select 'ball-red-small-round' union all
select 'ball-green-small-round' union all
select 'ball---square' union all
select '----';
;with stage (s)
as ( select replace(SomeColumn, '-', '.')
from #Table
)
select [item] = parsename(s,4),
[color] = parsename(s,3),
[size] = parsename(s,2),
[shape] = parsename(s,1)
from stage;
If its > 4 please reply and we can work on a more dynamic solution.

Using CROSS APPLYs (used lengthy names for understanding) -
declare #Table table (SomeColumn varchar(100));
insert into #Table
select 'ball-Orange-small-round' union all
select 'bat-blue-medium-square' union all
select 'stumps-green-large-rectangle'
SELECT * FROM #Table
SELECT Sub1.FirstSub1 AS Item
,Sub2.SecondSub1 AS Color
,Sub3.ThirdSub1 AS Size
,SubAfterThirdHyphen AS Shape
FROM #Table
CROSS APPLY (SELECT CHARINDEX('-',SomeColumn) AS FirstHyphenPos) AS Pos1
CROSS APPLY (SELECT SUBSTRING(SomeColumn,1,FirstHyphenPos-1) AS FirstSub1) AS Sub1
CROSS APPLY (SELECT SUBSTRING(SomeColumn,FirstHyphenPos+1,LEN(SomeColumn)) AS SubAfterFirstHyphen) AS Substr1
CROSS APPLY (SELECT CHARINDEX('-',Substr1.SubAfterFirstHyphen) AS SecondHyphenPos) AS Pos2
CROSS APPLY (SELECT SUBSTRING(Substr1.SubAfterFirstHyphen,1,SecondHyphenPos-1) AS SecondSub1) AS Sub2
CROSS APPLY (SELECT SUBSTRING(Substr1.SubAfterFirstHyphen,SecondHyphenPos+1,LEN(Substr1.SubAfterFirstHyphen)) AS SubAfterSecondHyphen) AS Substr2
CROSS APPLY (SELECT CHARINDEX('-',Substr2.SubAfterSecondHyphen) AS ThirdHyphenPos) AS Pos3
CROSS APPLY (SELECT SUBSTRING(Substr2.SubAfterSecondHyphen,1,ThirdHyphenPos-1) AS ThirdSub1) AS Sub3
CROSS APPLY (SELECT SUBSTRING(Substr2.SubAfterSecondHyphen,ThirdHyphenPos+1,LEN(Substr2.SubAfterSecondHyphen)) AS SubAfterThirdHyphen) AS Substr3

Related

How to find all objects of type Item with thier parent type Family

Here is my Objects table -
I am trying to write a query that can fetch all objects of type C with their parent of type A. So the query should return like
I am trying to do it using recursion but not getting the desired result. Any help would be appreciated. Thank you.
This gets you what you are after, but if the logic is right for your requirements is a different question:
DECLARE #ObjectID int = 3;
DECLARE #EndType char(1) = 'A';
WITH VTE AS(
SELECT *
FROM (VALUES(1,'A',NULL),
(2,'B',1),
(3,'C',2))V(ObjectID, ObjectType, ParentID)),
rCTE AS(
SELECT V.ObjectID,
V.ObjectType,
V.ParentID,
V.ObjectID AS StartID,
V.ObjectType AS StartType
FROM VTE V
WHERE v.ObjectID = #ObjectID
UNION ALL
SELECT V.ObjectID,
V.ObjectType,
V.ParentID,
r.StartID,
r.StartType
FROM rCTE r
JOIN VTE V ON V.ObjectID = r.ParentID)
SELECT r.StartID AS ObjectID,
r.StartType AS ObjectType,
r.ObjectID AS ParentObjectID,
r.ObjectType AS PArentObjectType
FROM rCTe r
WHERE r.ObjectType = #EndType;
Sample data
DECLARE #Temp AS TABLE (ObjectId INT,ObjectType VARCHAR(2),ParentObjectId INT)
INSERT INTO #Temp
SELECT 1,'A',NUll UNION ALL
SELECT 2,'B',1 UNION ALL
SELECT 3,'C',NULL UNION ALL
SELECT 4,'D',3 UNION ALL
SELECT 5,'E',4 UNION ALL
SELECT 6,'B',3
Sql server Script
;WITH CTE
AS
(
SELECT ObjectId,
ObjectType,
ParentObjectId,
CAST('\'+ CAST(ObjectId AS VARCHAR(MAX))AS VARCHAR(MAX)) AS [ObjectIdHierarchy] ,
CAST('\'+ ObjectType AS VARCHAR(MAX)) AS [Hierarchy]
FROM #Temp
WHERE ParentObjectId IS NULL
UNION ALL
SELECT t.ObjectId,
t.ObjectType,
t.ParentObjectId,
[ObjectIdHierarchy]+'\'+ CAST(t.ObjectId AS VARCHAR(MAX)) AS [ObjectIdHierarchy],
[Hierarchy]+'\'+ t.ObjectType AS [Hierarchy]
FROM CTE c
INNER JOIN #Temp t
ON t.ParentObjectId = c.ObjectId
)
SELECT ObjectId,
ObjectType,
LEFT(RIGHT([ObjectIdHierarchy],LEN([ObjectIdHierarchy])-1),1) AS ParentObjectId,
LEFT(RIGHT([Hierarchy],LEN([Hierarchy])-1),1) AS ParentChildHierarchy
FROM CTE
WHERE ObjectId =1
Result
ObjectId ObjectType ParentObjectId ParentChildHierarchy
------------------------------------------------------------
1 A 1 A

How to obtain a string equals to each first letter of words in a sentence IN SQL

I have words separated with a space in a column like
apple orange banana I need the first letters as the result will be something like :
aob
First, split your text. I recommend some function:
CREATE FUNCTION Split(#text nvarchar(MAX),#separator nvarchar(MAX))
RETURNS TABLE AS RETURN
WITH Indexed AS
(
SELECT 1 N, CAST(1 AS bigint) S, CHARINDEX(#separator, #text, 1) E WHERE #text IS NOT NULL
UNION ALL
SELECT N+1, E+DATALENGTH(#separator)/2, CHARINDEX(#separator, #text, E+DATALENGTH(#separator)/2) FROM Indexed WHERE E>S
), Token AS
(
SELECT N, SUBSTRING(#text, S, CASE WHEN E=0 THEN DATALENGTH(#text)/2 ELSE E-S END) T FROM Indexed
)
SELECT * FROM Token
If you are using SQL 2016 and greater, use STRING_SPLIT instead.
Then, you can select first character of every word and join. See following example:
DECLARE #Sample TABLE (T nvarchar(100));
INSERT #Sample VALUES (N'apple orange banana'),(N'dog cat');
SELECT (SELECT SUBSTRING(T,1,1) [*] FROM Split(T,N' ') FOR XML PATH(''))
FROM #Sample
Result:
(no column name)
------
aob
dc
If you declare REGEX function in your DB (not native with SQL SERVER).
Using regexp_replace
select regexp_replace('apple orange banana','(\\w)(\\w* ?)','$1')
return
aob
I think the shortest will be this:
Here a mockup-table with two rows to simulate your issue:
DECLARE #mockup TABLE(ID INT IDENTITY,YourWords VARCHAR(100));
INSERT INTO #mockup VALUES('apple orange banana'),('one two three');
--That is the query:
SELECT m.ID
,REPLACE(Casted.query('for $w in /x return substring($w,1,1)').value('.','varchar(max)'),' ','')
FROM #mockup m
CROSS APPLY(SELECT CAST('<x>' + REPLACE(m.YourWords,' ','</x><x>') + '</x>' AS XML)) A(Casted);
The idea behind:
The string apple orange banana is tranformed to <x>apple</x><x>orange</x><x>banana</x> and is casted to XML, which allows to use XQuery.
Now we use .query() on the XML with a simple FLWOR statement. It tells the engine: run through each value of /x and return just the first letter. Calling value() on this with a . as XPath will return the values in one.
We need a final REPLACE() to get rid of blanks, which would otherwise appear as a o b instead of aob.
Just another option using a little XML. You could also use ParseName() provided you trap any periods in the string.
Example
Declare #YourTable table(ID int,LastName varchar(50),FirstName varchar(50))
Insert Into #YourTable values
(1,'Waston','Mary Jane')
Select A.ID
,NewValue = upper(
concat(
xmlData.value('/x[1]','varchar(1)')
,xmlData.value('/x[2]','varchar(1)')
,xmlData.value('/x[3]','varchar(1)')
,xmlData.value('/x[4]','varchar(1)')
,'.'
,LastName
)
)
From #YourTable A
Cross Apply ( values (convert(xml,'<x>' + replace(A.FirstName,' ','</x><x>')+'</x>' )) ) B(xmlData)
Returns
ID NewValue
1 MJ.WASTON
EDIT - Added ParseName() option
Select A.ID
,NewValue = upper(concat(Pos1,Pos2,Pos3,Pos4,'.',LastName))
From #YourTable A
Cross Apply (
Select Pos1 = left(parsename(tStr,4),1)
,Pos2 = left(parsename(tStr,3),1)
,Pos3 = left(parsename(tStr,2),1)
,Pos4 = left(parsename(tStr,1),1)
From ( values(replace(FirstName,' ','.'))) B1(tStr)
) B

Filling the ID column of a table NOT using a cursor

Tables have been created and used without and ID column, but ID column is now needed. (classic)
I heard everything could be done without cursors. I just need every row to contain a different int value so I was looking for some kind of row number function :
How do I use ROW_NUMBER()?
I can't tell exactly how to use it even with these exemples.
UPDATE [TableA]
SET [id] = (select ROW_NUMBER() over (order by id) from [TableA])
Subquery returned more than 1 value.
So... yes of course it return more than one value. Then how to mix both update and row number to get that column filled ?
PS. I don't need a precise order, just unique values. I also wonder if ROW_NUMBER() is appropriate in this situation...
You can use a CTE for the update
Example
Declare #TableA table (ID int,SomeCol varchar(50))
Insert Into #TableA values
(null,'Dog')
,(null,'Cat')
,(null,'Monkey')
;with cte as (
Select *
,RN = Row_Number() over(Order by (Select null))
From #TableA
)
Update cte set ID=RN
Select * from #TableA
Updated Table
ID SomeCol
1 Dog
2 Cat
3 Monkey
You can use a subquery too as
Declare #TableA table (ID int,SomeCol varchar(50))
Insert Into #TableA values
(null,'Dog')
,(null,'Cat')
,(null,'Monkey');
UPDATE T1
SET T1.ID = T2.RN
FROM #TableA T1 JOIN
(
SELECT ROW_NUMBER()OVER(ORDER BY (SELECT 1)) RN,
*
FROM #TableA
) T2
ON T1.SomeCol = T2.SomeCol;
Select * from #TableA

How to use Substring to pull multiple items from a field

First post - I am trying to pull ten different pieces of information from a single field. Let me start with this is not my table, just what I was given to work with. This is a varchar max field.
'3350|#|1234567|~|3351|#|8/1/2017|~|3352|#|Acme|~|3353|~|10000.00|~|3354|#||~|3355|#||~3356|#|Yes|~|3357|#|Doe,John|~|3358|#|CA|~|3359|#|5551212'
I know that the numbers that start with 33 are keys telling me what information is in that section. 3350 has the invoice #1234567. 3351 has the invoice date of 8/1/17. etc. 3354 and 3355 were left null. The keys are unchanging and will be the same for every record in the table.
I need to pull the data from between 3350|#| and |~|3351 to get my invoice# and between 3351|#| and |~|3352 to get my date, etc, but I am struggling with how to word this. Any help would be appreciated and any critiques on my first post will be taken constructively.
The #YourTable is just a table variable used for demonstration / illustration
For Rows - Example
Declare #YourTable table (ID int,SomeCol varchar(max))
Insert Into #YourTable values
(1,'3350|#|1234567|~|3351|#|8/1/2017|~|3352|#|Acme|~|3353|#|10000.00|~|3354|#||~|3355|#||~|3356|#|Yes|~|3357|#|Doe,John|~|3358|#|CA|~|3359|#|5551212')
Select A.ID
,Item = left(RetVal,charindex('|#|',RetVal+'|#|')-1)
,Value = right(RetVal,len(RetVal)-charindex('|#|',RetVal+'|#|')-2)
From #YourTable A
Cross Apply [dbo].[udf-Str-Parse](A.SomeCol,'|~|') B
Returns
For Columns - Example
Declare #YourTable table (ID int,SomeCol varchar(max))
Insert Into #YourTable values
(1,'3350|#|1234567|~|3351|#|8/1/2017|~|3352|#|Acme|~|3353|#|10000.00|~|3354|#||~|3355|#||~|3356|#|Yes|~|3357|#|Doe,John|~|3358|#|CA|~|3359|#|5551212')
Select *
From (
Select A.ID
,Item = left(RetVal,charindex('|#|',RetVal+'|#|')-1)
,Value = right(RetVal,len(RetVal)-charindex('|#|',RetVal+'|#|')-2)
From #YourTable A
Cross Apply [dbo].[udf-Str-Parse](A.SomeCol,'|~|') B
) A
Pivot (max([Value]) For [Item] in ([3350],[3351],[3352],[3353],[3354],[3355],[3356],[3357],[3358],[3359]) ) p
Returns
The UDF if Interested
CREATE FUNCTION [dbo].[udf-Str-Parse] (#String varchar(max),#Delimiter varchar(10))
Returns Table
As
Return (
Select RetSeq = Row_Number() over (Order By (Select null))
,RetVal = LTrim(RTrim(B.i.value('(./text())[1]', 'varchar(max)')))
From (Select x = Cast('<x>' + replace((Select replace(#String,#Delimiter,'§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml).query('.')) as A
Cross Apply x.nodes('x') AS B(i)
);
--Thanks Shnugo for making this XML safe
--Select * from [dbo].[udf-Str-Parse]('Dog,Cat,House,Car',',')
--Select * from [dbo].[udf-Str-Parse]('John Cappelletti was here',' ')
You can try a tally based splitter like below
declare #t table ( id int, col nvarchar(max));
insert into #t values
(1, '3350|#|1234567|~|3351|#|8/1/2017|~|3352|#|Acme|~|3353|~|10000.00|~|3354|#||~|3355|#||~3356|#|Yes|~|3357|#|Doe,John|~|3358|#|CA|~|3359|#|5551212')
,(2, '3350|#|123334567|~|3351|#|8/2/2017|~|3352|#|Acme|~|3353|~|10000.00|~|3354|#||~|3355|#||~3356|#|Yes|~|3357|#|Doe,John|~|3358|#|CA|~|3359|#|5551212');
select
id,
case
when split_values like '3350|#|%' then 'id'
when split_values like '3351|#|%' then 'date'
end as fieldname,
SUBSTRING(split_values,8,LEN(split_values)-7) as value
from
(
select
--t.col as col,
row_number() over (partition by t.col order by t1.N asc) as row_num,
t.id,
SUBSTRING( t.col, t1.N, ISNULL(NULLIF(CHARINDEX('|~|',t.col,t1.N),0)-t1.N,8000)) as split_values
from #t t
join
(
select
t.col,
1 as N
from #t t
UNION ALL
select
t.col,
t1.N + 3 as N
from #t t
join
(
select
top 8000
row_number() over(order by (select NULL)) as N
from
sys.objects s1
cross join
sys.objects s2
) t1
on SUBSTRING(t.col,t1.N,3) = '|~|'
) t1
on t1.col=t.col
)a
where
split_values like '3350|#|%' or
split_values like '3351|#|%'
Live demo

String Replace column data with data from another table

I have two tables:
TableA:
ID Values
---------------
1 Q
2 B
3 TA
4 BS
TableB:
RawValue Value
------------------
[1][4] QBS
[2][1][3] BQTA
I need to generate TableB values with its given RawValues. each [X] in rawvalue is the ID coulmn of TableA and shoud be replace with its value .
[1][4] means that Value of TableA with has ID of 1 (Q) and Value of TableA with has ID of 4 (BS) then should equal to QBS.
can anyone suggest a way to do it?
this is what I have already tried:
update tableb set value=replace(rawvalue,'[' + (select id from tablea where id = cast(replace(replace(rawdata,'[',''),']','') as int)) + ']',
(select values from tablea where id = cast(replace(replace(rawdata,'[',''),']','') as int)))
By the way: this is still in test process and I can totally change tables, rowvalue format and replacement methods if anyone has a better idea.
declare #tableA table (id int, value varchar(50))
insert into #tableA (id, value)
select 1, 'Q' union all
select 2, 'B' union all
select 3, 'TA' union all
select 4, 'BS'
declare #tableB table (rawdata varchar(255), value varchar(255))
insert into #tableB (rawdata)
select '[1][4]' union all -- QBS
select '[2][1][3]' -- BQTA
update b
set value = (
select a.value + ''
from #tableA a
cross apply (select charindex ('[' + cast (a.id as varchar(50)) + ']', b.rawdata) as pos) p
where pos > 0
order by pos
for xml path('')
)
from #tableB b
select * from #tableB
P.S. I would recommend not to name field similar to reserved keywords (I mean Values).
Turn RawValue into XML, shred the XML to get one row for each value in RawValue and join to TableA to get the value.
Use the for xml path() trick to concatenate the values from TableA.
update TableB
set Value = (
select T.Value as '*'
from (
select row_number() over(order by T2.X) as SortOrder,
TableA.Value
from (select cast(replace(replace(TableB.RawValue, '[', '<x>'), ']', '</x>') as xml)) as T1(X)
cross apply T1.X.nodes('x') as T2(X)
inner join TableA
on TableA.ID = T2.X.value('text()[1]', 'int')
) as T
order by T.SortOrder
for xml path('')
)
SQL Fiddle

Resources