I have this data in my column:
32-HC-100-10001-G03P2-N-1-1001
The problem is my value doesn't have a fixed length. What I need to do is split this value into 2 columns 32-HC-100-10001-G03P2-N and 1 - the numbers after last - don't important
Another example
4-G-100-10029-F23S-S-2-1001
should be split into 4-G-100-10029-F23S-S and 2. I have used SUBSTRING([Line No#], 0, 21) but because of the length it didn't work.
Try this way
declare #str varchar(100)=reverse('4-G-100-10029-F23S-S-2-1001')
select reverse(substring(#str,charindex('-',#str)+1,len(#str))) as first_col,
left(substring(#str,charindex('-',#str)+1,len(#str)),charindex('-',substring(#str,charindex('-',#str)+1,len(#str)))-1) as second_col
May not be the shortest method but should get the job done
Note : I did not hard-code any length here
As long as the last part(1-1001,2-2002...) have the same number of values,this will work..
declare #string varchar(max)
set #string='32-HC-100-10001-G03P2-N-1-1001'
select replace(#string, right(#string,7),''),substring(right(#string,6),1,1)
Output:
32-HC-100-10001-G03P2-N 1
When doing complex string operations in SQL Server, one method uses outer apply to simplify the calculations:
select t.col, s2.firstpart, s2.secondpart
from t outer apply
(select left(col, len(col) - charindex('-', reverse(col)) as s1
-- remove the last number
) s1 outer apply
(select left(s1, len(s1) - charindex('-', reverse(s1)) as firstpart,
right(s1, charindex('-', reverse(s1)) -1) as secondpart
) s2;
I find the calculations easier to construct, follow, and debug.
You can try this:
DECLARE #string nvarchar(max) = '32-HC-100-10001-G03P2-N-1-1001'
SELECT REVERSE(STUFF(SUBSTRING(REVERSE(#string),CHARINDEX('-',REVERSE(#string))+1,LEN(#string)),1,CHARINDEX('-',SUBSTRING(REVERSE(#string),CHARINDEX('-',REVERSE(#string))+1,LEN(#string))),'')),
REVERSE(LEFT(SUBSTRING(REVERSE(#string),CHARINDEX('-',REVERSE(#string))+1,LEN(#string)),CHARINDEX('-',SUBSTRING(REVERSE(#string),CHARINDEX('-',REVERSE(#string)),LEN(#string)))))
Output:
32-HC-100-10001-G03P2-N 1
If it is always comes as a 7th part you can use XML:
DECLARE #string nvarchar(max) = '32-HC-100-10001-G03P2-N-1-1001',
#xml xml
SELECT #xml = CAST('<d>'+REPLACE(#string,'-','</d><d>') +'</d>' as xml)
SELECT t.v.value('/d[1]','nvarchar(10)') + '-' +
t.v.value('/d[2]','nvarchar(10)') + '-' +
t.v.value('/d[3]','nvarchar(10)') + '-' +
t.v.value('/d[4]','nvarchar(10)') + '-' +
t.v.value('/d[5]','nvarchar(10)') + '-' +
t.v.value('/d[6]','nvarchar(10)'),
t.v.value('/d[7]','nvarchar(10)')
FROM #xml.nodes('/') as t(v)
Output:
32-HC-100-10001-G03P2-N 1
Related
I'm trying to create a stored procedure for updating a table in a batch. I want to take parameters in as a nvarchar and call string_split on them.
#ParamList1 NVARCHAR(max) = '1,2,3,4,5'
#ParamList2 NVARCHAR(max) = 'a,b,c,d,e'
I want to get a temporary table like
Param1 Param2
1 a
2 b
3 c
...
How would I do this?
Unfortunately, string_split() does not guarantee ordering or provide a position argument (Microsoft are you listening?).
So, the safest method is a recursive CTE (or perhaps another approach using XML):
with cte as (
select convert(nvarchar(max), NULL) as x1, convert(nvarchar(max), NULL) as x2, #paramlist1 as rest1, #paramlist2 as rest2, 1 as lev
union all
select convert(nvarchar(max), left(rest1, charindex(',', rest1 + ',') - 1)),
convert(nvarchar(max), left(rest2, charindex(',', rest2 + ',') - 1)),
stuff(rest1, 1, charindex(',', rest1 + ','), ''),
stuff(rest2, 1, charindex(',', rest2 + ','), ''),
lev + 1
from cte
where rest1 <> '' and rest2 <> ''
)
select *
from cte
where x1 is not null;
Here is a db<>fiddle.
You've got an answer already, which is working fine, but this should be faster and easier:
You did not specify your SQL-Server's version, but - talking about STRING_SPLIT() - I assume it's at least v2016. If this is correct, you can use OPENJSON. Your list of numbers needs nothing more than brackets to be a JSON-array ([1,2,3]), while an array of words/letters can be transformed with some easy string operations (["a","b","c"]).
Following the docs, OPENJSON returns the elements position in [key], while the element itself is returned in [value]. You can simply JOIN these sets:
DECLARE #ParamList1 NVARCHAR(max) = '1,2,3,4,5';
DECLARE #ParamList2 NVARCHAR(max) = 'a,b,c,d,e';
SELECT p1.[key] AS FragmentNr
,p1.[value] AS P1
,p2.[value] AS P2
FROM OPENJSON(CONCAT('[',#ParamList1 + ']')) p1
INNER JOIN OPENJSON(CONCAT('["',REPLACE(#ParamList2,',','","'),'"]')) p2 ON p1.[key]=p2.[key] ;
In this answer you will find some details (UPDATE section 1 and 2).
I have a string like this:
Apple
I want to include a separator after each character so the end result will turn out like this:
A,p,p,l,e
In C#, we have one liner method to achieve the above with Regex.Replace('Apple', ".{1}", "$0,");
I can only think of looping each character with charindex to append the separator but seems a little complicated. Is there any elegant way and simpler way to achieve this?
Thanks HABO for the suggestions. I'm able to generate the result that I want using the code but takes a little bit of time to really understand how the code work.
After some searching, I manage to found one useful article to insert empty spaces between each character and it's easier for me to understand.
I modify the code a little to define and include desire separator instead of fixing it to space as the separator:
DECLARE #pos INT = 2 -- location where we want first space
DECLARE #result VARCHAR(100) = 'Apple'
DECLARE #separator nvarchar(5) = ','
WHILE #pos < LEN(#result)+1
BEGIN
SET #result = STUFF(#result, #pos, 0, #separator);
SET #pos = #pos+2;
END
select #result; -- Output: A,p,p,l,e
Reference
In following SQL scripts, I get each character using SUBSTRING() function using with a number table (basically I used spt_values view here for simplicity) and then I concatenate them via two different methods, you can choose one
If you are using SQL Server 2017, we have a new SQL string aggregation function
First script uses string_agg function
declare #str nvarchar(max) = 'Apple'
SELECT
string_agg( substring(#str,number,1) , ',') Within Group (Order By number)
FROM master..spt_values n
WHERE
Type = 'P' and
Number between 1 and len(#str)
If you are working with a previous version, you can use string concatenation using FOR XML Path and SQL Stuff function as follows
declare #str nvarchar(max) = 'Apple'
; with cte as (
SELECT
number,
substring(#str,number,1) as L
FROM master..spt_values n
WHERE
Type = 'P' and
Number between 1 and len(#str)
)
SELECT
STUFF(
(
SELECT
',' + L
FROM cte
order by number
FOR XML PATH('')
), 1, 1, ''
)
Both solution yields the same result, I hope it helps
If you have SQL Server 2017 and a copy of ngrams8k it's ultra simple:
declare #word varchar(100) = 'apple';
select newString = string_agg(token, ',') within group (order by position)
from dbo.ngrams8k(#word,1);
For pre-2017 systems it's almost as simple:
declare #word varchar(100) = 'apple';
select newstring =
( select token + case len(#word)+1-position when 1 then '' else ',' end
from dbo.ngrams8k(#word,1)
order by position
for xml path(''))
One ugly way to do it is to split the string into characters, ideally using a numbers table, and reassemble it with the desired separator.
A less efficient implementation uses recursion in a CTE to split the characters and insert the separator between pairs of characters as it goes:
declare #Sample as VarChar(20) = 'Apple';
declare #Separator as Char = ',';
with Characters as (
select 1 as Position, Substring( #Sample, 1, 1 ) as Character
union all
select Position + 1,
case when Position & 1 = 1 then #Separator else Substring( #Sample, Position / 2 + 1, 1 ) end
from Characters
where Position < 2 * Len( #Sample ) - 1 )
select Stuff( ( select Character + '' from Characters order by Position for XML Path( '' ) ), 1, 0, '' ) as Result;
You can replace the select Stuff... line with select * from Characters; to see what's going on.
Try this
declare #var varchar(50) ='Apple'
;WITH CTE
AS
(
SELECT
SeqNo = 1,
MyStr = #var,
OpStr = CAST('' AS VARCHAR(50))
UNION ALL
SELECT
SeqNo = SeqNo+1,
MyStr = MyStR,
OpStr = CAST(ISNULL(OpStr,'')+SUBSTRING(MyStR,SeqNo,1)+',' AS VARCHAR(50))
FROM CTE
WHERE SeqNo <= LEN(#var)
)
SELECT
OpStr = LEFT(OpStr,LEN(OpStr)-1)
FROM CTE
WHERE SeqNo = LEN(#Var)+1
Select charindex('% %',CAST(X.MyCharStringField AS NVARCHAR(max))) from X
Im figuring out how to split up a field that has two spaces between string values.
example field content
'AFLOP 46.95 46.95 36.95 0 0 '
Both charindex and patindex return 0 when used again table varchar field, but return position if I declair a varchar(max) variable and test with that.
I can't figure out how to get this working with the table varchar(max) field.
This Works
Declare #theBefore varchar(100)
SET #theBefore = 'AFLOP 46.95 46.95 36.95 0 0 '
select charindex(' ',#theBefore) as spaceIndex,SUBSTRING(#theBefore,0,6)as ITEM_ID
,ltrim(rtrim(SUBSTRING(#theBefore,charindex(' ',#theBefore),len(#theBefore)+2))) as BEFORE
This does not work, returns 0's for each records char string value
Select charindex('% %',CAST(X.MyCharStringField AS NVARCHAR(max))) from X
Anyone have a clue why ?
You might try this easy way to split a string: Just replace the blank with </x><x> and add an opening tag at the beginning and one closing at the end, and - voila! - You've got XML:
DECLARE #YourString VARCHAR(MAX)='AFLOP 46.95 46.95 36.95 0 0 ';
SELECT part.value('.','varchar(max)')
FROM
(
SELECT CAST('<x>' + REPLACE(#YourString,' ','</x><x>') + '</x>' AS XML)
) AS tbl(Casted)
CROSS APPLY Casted.nodes('/x') AS A(part)
UPDATE
If your input string has always the same structure you might - easier - get the values typesafe and directly:
DECLARE #YourString VARCHAR(MAX)='AFLOP 46.95 46.95 36.95 0 0 ';
SELECT Casted.value('/x[1]','varchar(max)') AS part1
,Casted.value('/x[2]','decimal(6,2)') AS part2
,Casted.value('/x[3]','decimal(6,2)') AS part3
,Casted.value('/x[4]','decimal(6,2)') AS part4
,Casted.value('/x[5]','decimal(6,2)') AS part5
,Casted.value('/x[6]','decimal(6,2)') AS part6
FROM
(
SELECT CAST('<x>' + REPLACE(#YourString,' ','</x><x>') + '</x>' AS XML)
) AS tbl(Casted)
I figured it out: the 'spaces' were not spaces but cr lf - i.e. CHAR(13) and CHAR(10)
This worked as expected:
Select patindex(cast('%'+CHAR(13)+CHAR(10)+'%' as varchar(max)),BEFORE) from SECAUDIT
Select charindex(CHAR(13)+CHAR(10),SECAUDIT.BEFORE) from SECAUDIT
How I can select
"ALT1" if value is "W61N03D20V0-WHIH-ALT1"
"ALT2" if for "W61N03D20V0-WHIH-ALT2"
"SW" for "W61N03D20V0-WHIH-SW"
"Default" for "W61N26D1YA1-VICU" (without prefix)
"Defailt" for "W61N27D21V2-AZTD"
In other words I'm looking for a way extract last part after second suffix, but if I have't second suffix - then default
Thanks for advice
Try it like this:
First you "split" the string on its minus signs with the XML trick.
Then you read the third node from you XML - voila!
CREATE TABLE #tbl(content VARCHAR(100));
INSERT INTO #tbl VALUES('W61N03D20V0-WHIH-ALT1')
,('W61N03D20V0-WHIH-SW')
,('W61N26D1YA1-VICU');
WITH SplittedAsXml AS
(
SELECT CAST('<x>' + REPLACE(content,'-','</x><x>') + '</x>' AS XML) AS Content
FROM #tbl
)
SELECT ISNULL(Content.value('/x[3]','varchar(max)'),'default') AS TheThirdPart
FROM SplittedAsXml;
DROP TABLE #tbl;
The result
ALT1
SW
default
Going this ways would also give you the chance to get the other parts in one go just querying /x[1] and /x[2] too
I did it using the built-in substring() function:
declare #str VARCHAR(40) = 'W61N03D20V0-WHIH-ALT1' -- also works for the other examples
declare #sep VARCHAR(1) = '-'
declare #middleToEnd VARCHAR(40) = substring(#str, charindex(#sep, #str) + 1, len(#str))
declare #pos INT = charindex(#sep, #middleToEnd)
declare #lastPart VARCHAR(40) =
CASE WHEN #pos = 0
THEN 'Default'
ELSE substring(#middleToEnd, #pos + 1, len(#middleToEnd))
END
select #lastPart
For best performance, you can solve it with this one-liner(calculation is one line)
SELECT
COALESCE(STUFF(col,1,NULLIF(CHARINDEX('-',col, CHARINDEX('-',col)+1), 0),''),'Default')
FROM (values
('W61N03D20V0-WHIH-ALT1'),('W61N03D20V0-WHIH-ALT2'),
('W61N03D20V0-WHIH-SW'),('W61N26D1YA1-VICU'),
('W61N27D21V2-AZTD')) x(col)
Result:
ALT1
ALT2
SW
Default
Default
If I understand what you are asking for, the following does what you need:
-- fake table
WITH SomeTable AS (
SELECT 'W61N03D20V0-WHIH-ALT1' AS Field1
UNION ALL
SELECT 'W61N03D20V0-WHIH-SW'
UNION ALL
SELECT 'W61N26D1YA1-VICU'
)
-- select
SELECT
CASE CHARINDEX('-WHIH-', Field1)
WHEN 0 THEN 'Default'
ELSE SUBSTRING(Field1, CHARINDEX('-WHIH-', Field1) + 6, LEN(Field1) - (CHARINDEX('-WHIH-', Field1) + 5))
END
FROM SomeTable
Use can use a CASE expression to check whether the string starts with W61N03D20V0-WHIH.
If it starts with it use a combination of RIGHT, REVERSE and CHARINDEX functions to get last part from the string, else Default.
Query
select case when [your_column_name] like 'W61N03D20V0-WHIH%'
then right([your_column_name], charindex('-', reverse([your_column_name]), 1) - 1)
else 'Default' end as new_column_name
from your_table_name;
SQl Fiddle demo
in Sql server
I have a following string
DECLARE #str nvarchar(max);
set #str = "Hello how are you doing today,Its Monday and 5 waiting days";
DECLARE #srch nvarchar(max);
set #srch = " how,doing,monday,waiting";
Now i want to check whether str contains any of string (comma separated string) of srch
I want it in only sql server
is there possibilites to write some query with in clause
like
select from #str where _____ in (select * from CommaSplit(#srch)
where CommaSplit function rerturns rows of #srch comma separted value
I dont want to use cursor or any loop concept as the #srch value can be very long
Thanks
you can use same function to get first string in rows
select string from CommaSplit(#srch,'') where string in (select * from CommaSplit(#srch)
You can use the following common table expressions query to split your string into parts. cte will contain one record per phrase in #srch. In my example below, I show where in #str each of the search phrase is located. It returns 0 if it cannot locate a search phrase.
Note 1: it won't show the location twice if your search phrase is duplicated - you would need another CTE for that.
Note 2: I have to add comma at the end of #srch to make my CTE work. You can do that inside the CTE if you prefer not to change the search string.
DECLARE #str nvarchar(max);
set #str = 'Hello how are you doing today,Its Monday and 5 waiting days';
DECLARE #srch nvarchar(max);
set #srch = 'how,doing,monday,waiting';
set #srch = #srch + ','
-- first split the text into 1 character per row
;with cte
as
(
select substring(#srch, 1, CHARINDEX(',', #srch, 1) - 1) as Phrase, CHARINDEX(',', #srch, 1) as Idx
union all
select substring(#srch, cte.Idx + 1, CHARINDEX(',', #srch, cte.Idx + 1) - cte.Idx - 1) as Phrase, CHARINDEX(',', #srch, cte.Idx + 1) as Idx
from cte
where cte.Idx < CHARINDEX(',', #srch, cte.Idx + 1)
)
select charindex(cte.Phrase, #str, 1) from cte
I don't think that the IN clause is what you need. Instead of this you can use the LIKE construction as following:
if (select count(*) from CommaSplit(#srch) where #str like '%' + val + '%') > 0
select 'true'
else
select 'false'
In this case you will receive 'true' when at least 1 result of CommaSplit function exists in the #str text. But in this case you also will receive a 'true' value when the result of CommaSplit function is a part of the word in the #str string.
If you need more accurate solution, this can be achieved by the following way: you need to split the #str into the words (also replacing punctuation by spaces beforehand). And, after this, intersect of CommaSplit (#srch) and SpaceSplit(#str) will be the answer on the question. Among this, you also will be able to check which words are matching between two strings.
The overhead of this method is to create function SpaceSplit which is copy of CommaSplit but with another separator. Or the function CommaSplit can be modified to receive a separator as parameter.