Split string into new column [duplicate] - sql

This question already has answers here:
How do I split a delimited string so I can access individual items?
(46 answers)
Closed 7 years ago.
There is a column containing following e.g. abcd/ef/g/hij.
Characters between the / are dynamic not fix.
I want to split in a select query the content into 4 separate new columns.
The already answered question is different, I want to split the content in a string seperated by / into new columns.

You can use REPLACE to replace '/' with '.'. Then use PARSENAME to get each separate part of the string:
CREATE TABLE #tmp (str VARCHAR(50))
INSERT INTO #tmp VALUES
('abcd/ef/g/hij'),
('1111111/222/33/4444')
SELECT PARSENAME(x.s, 4) AS [1], PARSENAME(x.s, 3) AS [2],
PARSENAME(x.s, 2) AS [3], PARSENAME(x.s, 1) AS [4]
FROM #tmp
CROSS APPLY (SELECT REPLACE(str, '/', '.')) AS x(s)
Output:
1 2 3 4
---------------------
abcd ef g hij
1111111 222 33 4444

If you ask me, fastest ad-hoc method would be to turn your data into xml and use nodes() method:
declare #temp table (data nvarchar(max))
insert into #temp
select 'abcd/ef/g/hij' union all
select '1/2/3'
select t.data, n.c.value('.', 'nvarchar(max)')
from #temp as t
outer apply (select cast('<t>' + replace(t.data, '/', '</t><t>') + '</t>' as xml) as data) as d
outer apply d.data.nodes('t') as n(c)

You need to find the position of the / characters using CHARINDEX and slice the string up that way. It will be a large expression, because to find the third slash, you need to use the 3rd parameter of CHARINDEX, passing the result of another CHARINDEX, which also has its 3rd parameter being used. Except for the last (fourth) fragment, you also need to use CHARINDEX to find and remove text after the next slash.
Something like this will extract the text after the third slash:
RIGHT(s, CHARINDEX('/', s, CHARINDEX('/', s, CHARINDEX('/', s)+1)+1)+1)
I leave the rest to you.

Related

How to fetch only a part of string

I have a column which has inconsistent data. The column named ID and it can have values such as
0897546321
ABC,0876455321
ABC,XYZ,0873647773
ABC,
99756
test only
The SQL query should fetch only Ids which are of 10 digit in length, should begin with a 08 , should be not null and should not contain all characters. And for those values, which have both digits and characters such as ABC,XYZ,0873647773, it should only fetch the 0873647773 . In these kind of values, nothing is fixed, in place of ABC, XYZ , it can be anything and can be of any length.
The column Id is of varchar type.
My try: I tried the following query
select id
from table
where id is not null
and id not like '%[^0-9]%'
and id like '[08]%[0-9]'
and len(id)=10
I am still not sure how should I deal with values like ABC,XYZ,0873647773
P.S - I have no control over the database. I can't change its values.
SQL Server generally has poor support regular expressions, but in this case a judicious use of PATINDEX is viable:
SELECT SUBSTRING(id, PATINDEX('%,08[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9],%', ',' + id + ','), 10) AS number
FROM yourTable
WHERE ',' + id + ',' LIKE '%,08[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9],%';
Demo
If you normalise your data, and split the delimited data into parts, you can achieve this some what more easily:
SELECT SS.value
FROM dbo.YourTable YT
CROSS APPLY STRING_SPLIT(YT.YourColumn,',') SS
WHERE LEN(SS.value) = 10
AND SS.value NOT LIKE '%[^0-9]%';
If you're on an older version of SQL Server, you'll have to use an alternative String Splitter method (such as a XML splitter or user defined inline table-value function); there are plenty of examples on these already on Stack Overflow.
db<>fiddle

How to get maximum value of a specific part of strings?

I have below records
Id Title
500006 FS/97/98/037
500007 FS/97/04/035
500008 FS/97/01/036
500009 FS/97/104/040
I should split Title field and get 4th part of text and return maximum value. In this example my query should return 040 or 40.
select max(cast(right(Title, charindex('/', reverse(Title) + '/') - 1) as int))
from your_table
SQLFiddle demo
You can use PARSENAME function since you always have 4 parts(confirmed in comments section)
select max(cast(parsename(replace(Title,'/','.'),1) as int))
from yourtable
If you want to split the data in the Title column and get the part from the splitted text by position, you may try with one JSON-based approach with a simple string transformation. You need to transform the data in the Title column into a valid JSON array (FS/97/98/037 into ["FS","97","08","037"]) and after that to parse thе data with OPENJSON(). The result from OPENJSON() (using default schema and parsing JSON array) is a table with columns key, value and type, and the key column holds the index of the items in the JSON array:
Note, that using STRING_SPLIT() is not an option here, because the order of the returned rows is not guaranteed.
Table:
CREATE TABLE Data (
Id varchar(6),
Title varchar(50)
)
INSERT INTO Data
(Id, Title)
VALUES
('500006', 'FS/97/98/037'),
('500007', 'FS/97/04/035'),
('500008', 'FS/97/01/036'),
('500009', 'FS/97/104/040')
Statement:
SELECT MAX(j.[value])
FROM Data d
CROSS APPLY OPENJSON(CONCAT('["', REPLACE(d.Title, '/', '","'), '"]')) j
WHERE (j.[key] + 1) = 4
If you data has fixed format with 4 parts, even this approach may help:
SELECT MAX(PARSENAME(REPLACE(Title, '/', '.'), 1))
FROM Data
You can also try the below query.
SELECT Top 1
CAST('<x>' + REPLACE(Title,'/','</x><x>') + '</x>' AS XML).value('/x[4]','int') as Value
from Data
order by 1 desc
You can find the live demo Here.

SQL server split string into columns by delimiter (dynamic length) [duplicate]

This question already has answers here:
how to separate string into different columns?
(5 answers)
How to split a comma-separated value to columns
(38 answers)
Closed 3 years ago.
SQL server cannot use MySQL split_index function, and my environment face accessibility blocking to use some function on server like "CREATE" "INSERT"
Are there any method to split strings by fixed delimiter into columns ?
Has 3 delimiters but length is dynamic.
e.g.
STRING : sometimes - "AA.0.HJ", sometimes - "AABBCC.099.0",sometimes - "0.91.JAH21"
The combinations of substring is not work.
SUBSTRING(STRING ,
CHARINDEX('.', STRING )+1,
LEN(STRING )-CHARINDEX('.', STRING )
Origin:
STRING
AA.0.HJ
AABBCC.099.0
0.91.JAH21
Target :
STRING First Second Third
AA.0.HJ AA 0 HJ
AABBCC.099.0 AABBCC 099 0
0.91.JAH21 0 91 JAH21
What is the solution in this situation ?
An xml-based solution
declare #tmp table (STRING varchar(500))
insert into #tmp
values
('AA.0.HJ')
,('AABBCC.099.0')
,('0.91.JAH21')
;WITH Splitted
AS (
SELECT STRING
,CAST('<x>' + REPLACE(STRING, '.', '</x><x>') + '</x>' AS XML) AS Parts
FROM #tmp
)
SELECT STRING
,Parts.value(N'/x[1]', 'varchar(50)') AS [First]
,Parts.value(N'/x[2]', 'varchar(50)') AS [Second]
,Parts.value(N'/x[3]', 'varchar(50)') AS [Third]
FROM Splitted;
Output:
You can use parsename
Declare #t table (name varchar(50))
insert into #t values ('AA.0.HJ')
insert into #t values ('AABBCC.099.0')
select parsename(name,3),parsename(name,2),parsename(name,1) from #t

sql extract rightmost number in string and increment

i have transaction codes like
"A0004", "1B2005","20CCCCCCC21"
I need to extract the rightmost number and increment the transaction code by one
"AA0004"----->"AA0005"
"1B2005"------->"1B2006"
"20CCCCCCCC21"------>"20CCCCCCCC22"
in SQL Server 2012.
unknown length of string
right(n?) always number
dealing with unsignificant number of string and number length is out of my league.
some logic is always missing.
LEFT(#a,2)+RIGHT('000'+CONVERT(NVARCHAR,CONVERT(INT,SUBSTRING( SUBSTRING(#a,2,4),2,3))+1)),3
First, I want to be clear about this: I totally agree with the comments to the question from a_horse_with_no_name and Jeroen Mostert.
You should be storing one data point per column, period.
Having said that, I do realize that a lot of times the database structure can't be changed - so here's one possible way to get that calculation for you.
First, create and populate sample table (Please save us this step in your future questions):
DECLARE #T AS TABLE
(
col varchar(100)
);
INSERT INTO #T (col) VALUES
('A0004'),
('1B2005'),
('1B2000'),
('1B00'),
('20CCCCCCC21');
(I've added a couple of strings as edge cases you didn't mention in the question)
Then, using a couple of cross apply to minimize code repetition, I came up with that:
SELECT col,
LEFT(col, LEN(col) - LastCharIndex + 1) +
REPLICATE('0', LEN(NumberString) - LEN(CAST(NumberString as int))) +
CAST((CAST(NumberString as int) + 1) as varchar(100)) As Result
FROM #T
CROSS APPLY
(
SELECT PATINDEX('%[^0-9]%', Reverse(col)) As LastCharIndex
) As Idx
CROSS APPLY
(
SELECT RIGHT(col, LastCharIndex - 1) As NumberString
) As NS
Results:
col Result
A0004 A0005
1B2005 1B2006
1B2000 1B2001
1B00 1B01
20CCCCCCC21 20CCCCCCC22
The LastCharIndex represents the index of the last non-digit char in the string.
The NumberString represents the number to increment, as a string (to preserve the leading zeroes if they exists).
From there, it's simply taking the left part of the string (that is, up until the number), and concatenate it to a newly calculated number string, using Replicate to pad the result of addition with the exact number of leading zeroes the original number string had.
Try This
DECLARE #test nvarchar(1000) ='"A0004", "1B2005","20CCCCCCC21"'
DECLARE #Temp AS TABLE (ID INT IDENTITY,Data nvarchar(1000))
INSERT INTO #Temp
SELECT #test
;WITH CTE
AS
(
SELECT Id,LTRIM(RTRIM((REPLACE(Split.a.value('.' ,' nvarchar(max)'),'"','')))) AS Data
,RIGHT(LTRIM(RTRIM((REPLACE(Split.a.value('.' ,' nvarchar(max)'),'"','')))),1)+1 AS ReqData
FROM
(
SELECT ID,
CAST ('<S>'+REPLACE(Data,',','</S><S>')+'</S>' AS XML) AS Data
FROM #Temp
) AS A
CROSS APPLY Data.nodes ('S') AS Split(a)
)
SELECT CONCAT('"'+Data+'"','-------->','"'+CONCAT(LEFT(Data,LEN(Data)-1),CAST(ReqData AS VARCHAR))+'"') AS ExpectedResult
FROM CTE
Result
ExpectedResult
-----------------
"A0004"-------->"A0005"
"1B2005"-------->"1B2006"
"20CCCCCCC21"-------->"20CCCCCCC22"
STUFF(#X
,LEN(#X)-CASE PATINDEX('%[A-Z]%',REVERSE(#X)) WHEN 0 THEN LEN(#X) ELSE PATINDEX('%[A-Z]%',REVERSE(#X))-1 END+1
,LEN(((RIGHT(#X,CASE PATINDEX('%[A-Z]%',REVERSE(#X)) WHEN 0 THEN LEN(#X) ELSE PATINDEX('%[A-Z]%',REVERSE(#X))-1 END)/#N)+1)#N)
,((RIGHT(#X,CASE PATINDEX('%[A-Z]%',REVERSE(#X)) WHEN 0 THEN LEN(#X) ELSE PATINDEX('%[A-Z]%',REVERSE(#X))-1 END)/#N)+1)#N)
works on number only strings
99 becomes 100
mod(#N) increments

Query to pad left of a field with 0's [duplicate]

This question already has answers here:
Formatting Numbers by padding with leading zeros in SQL Server
(14 answers)
Closed 7 years ago.
I have been working on a query (in sql Server TSQL) which fills left of a number with 0's so output is always 5 digit.
So:
Select MuNumber From Mytable
for data 11,011,2132,1111
Creates output like
00011
02134
01111
I tried Lpad Function but numer of 0's can be different.
if Munumber is 1 we need 0000 and If MyNumber is 34 we need 000
Assuming that MuNumber is VARCHAR simply use RIGHT
SELECT RIGHT('00000' + MuNumber, 5)
FROM Mytable
Otherwise you need to convert it first
SELECT RIGHT('00000' + CONVERT(VARCHAR(5), MuNumber), 5)
FROM Mytable
And in general you can use this pattern:
DECLARE #num INT = 10;
SELECT RIGHT(REPLICATE('0', #num) + CONVERT(VARCHAR(5), MuNumber), #num)
FROM Mytable
Try this
select right('00000'+cast(col as varchar(5)),5) from table
You can use the user defined function udfLeftSQLPadding where you can find the source codes at SQL Pad Leading Zeros
After you create the function on your database, you can use it as follows
select
dbo.udfLeftSQLPadding(MuNumber,5,'0')
from dbo.Mytable
Another option:
declare #n int = 6
select stuff(replicate('0', #n), 6-len(n), len(n), n)
from (values('123'), ('2493'), ('35')) as x(n)