How to get the 1st value before delimiter in sql server - sql-server

In one of the column i am getting 2 values with a delimiter between it
How to extract both the values
I have some thing like this Column TRN02 is 115679-5757
I need to take values before delimiter and after delimter into 2 separate columns again.
Can some one help me on this

You can use SUBSTRING to do this:
SELECT
SUBSTRING(TRN02, 0, CHARINDEX('-', TRN02)) AS [First]
SUBSTRING(TRN02, CHARINDEX('-', TRN02) + 1, LEN(TRN02)) AS [Second]
FROM TABLE

Just another way USING LEFT and RIGHT -
SELECT LEFT(TRN02, CHARINDEX('-', TRN02) - 1) [before_delim],
RIGHT(TRN02, LEN(TRN02) - CHARINDEX('-', TRN02)) [after_delim]
FROM your_table

SELECT LEFT(details, CHARINDEX ('-', TRN02 ) - 1),
SUBSTRING(details, CHARINDEX ('-', TRN02 ) + 1, 100)
FROM Your_table

update df set df.column=s.[value] FROM table df
cross APPLY string_split(REPLACE(column, 'split_parameter', '#') ,'#') s
where column like '%split_parameter%' and s.[value] not like '=%'

Related

how extract first word using parsename function sql server? [duplicate]

In one of the column i am getting 2 values with a delimiter between it
How to extract both the values
I have some thing like this Column TRN02 is 115679-5757
I need to take values before delimiter and after delimter into 2 separate columns again.
Can some one help me on this
You can use SUBSTRING to do this:
SELECT
SUBSTRING(TRN02, 0, CHARINDEX('-', TRN02)) AS [First]
SUBSTRING(TRN02, CHARINDEX('-', TRN02) + 1, LEN(TRN02)) AS [Second]
FROM TABLE
Just another way USING LEFT and RIGHT -
SELECT LEFT(TRN02, CHARINDEX('-', TRN02) - 1) [before_delim],
RIGHT(TRN02, LEN(TRN02) - CHARINDEX('-', TRN02)) [after_delim]
FROM your_table
SELECT LEFT(details, CHARINDEX ('-', TRN02 ) - 1),
SUBSTRING(details, CHARINDEX ('-', TRN02 ) + 1, 100)
FROM Your_table
update df set df.column=s.[value] FROM table df
cross APPLY string_split(REPLACE(column, 'split_parameter', '#') ,'#') s
where column like '%split_parameter%' and s.[value] not like '=%'

Isolating String between 3 Characters

I have a view where I would like to create a column that isolates a string between 3 of the same character ("-"). So for example, I want AC-RBQ/4110-WS-L1 to become RBQ/4110.
So far I have tried this and it gets me very close.
SELECT SUBSTRING(locnum,CHARINDEX('-',locnum)+1,(((LEN(locnum))-CHARINDEX('-', REVERSE(locnum)))-CHARINDEX('-',locnum))) AS Result
Result:
RBQ/4110-WS
RBQ/4110-CS
I just need to remove that last "-WS" or "-CS"
Any thoughts or ideas would be greatly appreciated!
If the pattern is consistent, perhaps parsename() would be a good fit here
Example
Declare #S varchar(100) = 'AC-RBQ/4110-WS-L1'
Select parsename(replace(#S,'-','.'),3)
Returns
RBQ/4110
EDIT -
Just in case your data is more variable, you can use XML to extract the SECOND value
Example
Declare #YourTable table (locnum varchar(100))
Insert Into #YourTable values
('AC-RBQ/4110-WS-L1')
Select NewValue = convert(xml,'<x>'+replace(locnum,'-','</x><x>')+'</x>').value('/x[2]','varchar(100)')
From #YourTable
Returns
NewValue
RBQ/4110
It would seem simpler to split the value and then extract the 2nd item from your data. If your data always has 4 elements (and you always need the second), you could use PARSENAME:
SELECT PN.P
FROM (VALUES('AC-RBQ/4110-WS-L1'))V(S)
CROSS APPLY (VALUES(PARSENAME(REPLACE(V.S,'-','.'),3)))PN(P); --Part 3 as PARSENAME works right to left
Otherwise you could use a splitter like delimitedsplit8k_lead:
SELECT DS.Item
FROM (VALUES('AC-RBQ/4110-WS-L1'))V(S)
CROSS APPLY dbo.DelimitedSplit8K_lead(V.S,'-') DS
WHERE DS.ItemNumber = 2;
Please Try below Query. If your pattern is same per your question then below will work.
select left(SUBSTRING('AC-RBQ/4110-WS-L1', charindex('-','AC-RBQ/4110-WS-L1') + 1, LEN('AC-RBQ/4110-WS-L1')),8)
WITH TEMP AS
(
SELECT 'AC-RBQ/4110-WS-L1' AS COL
)
SELECT * ,
CHARINDEX('-', [COL]) AS FR,
CHARINDEX('-', [COL],CHARINDEX('-', [COL])+1) AS FR2,
SUBSTRING([COL], CHARINDEX('-', [COL]) +1 ,CHARINDEX('-', [COL],CHARINDEX('-
', [COL])+1) - (CHARINDEX('-', [COL]) +1) ) AS RESULT
FROM TEMP
You've done most of the work and you need to get the left part of the result up to -:
SELECT
LEFT(SUBSTRING(locnum, CHARINDEX('-', locnum) + 1,
(((LEN(locnum)) - CHARINDEX('-', REVERSE(locnum))) - CHARINDEX('-', locnum))),
CHARINDEX('-', SUBSTRING(locnum, CHARINDEX('-', locnum) + 1,
(((LEN(locnum)) - CHARINDEX('-', REVERSE(locnum))) - CHARINDEX('-', locnum)))) - 1) AS Result

SQL Server : Extracting Everything Before a Certain Character

I have this:
21654-8012
1234-127834
12345-1222
I want to extract this:
21654
1234
12345
Basically, everything before the hyphen, - character. Does anyone have any suggestions on where to start?
Use left WITH charindex() :
select t.col, left(col, charindex('-', col)-1)
from table t;
You can use CHARINDEX function
DECLARE #text VARCHAR(20)
SET #text = '123456-0000'
SELECT SUBSTRING(#text, 0, CHARINDEX('-', #text))
Instead of #text, you can use your field name
SELECT SUBSTRING(YOUR_COLUMN_NAME, 0, CHARINDEX('-', YOUR_COLUMN_NAME)) FROM YOUR_TABLE_NAME

Substring and split in sql server

I have these data in my sql server as you can see here :
1/2
1/4
2/23
12/13
1/10
...
I need to change these to 001,001,002,012,001,..
I use this .but it doesn't work
LEFT(SheetNumber,LEN(SheetNumber)-CHARINDEX('/',SheetNumber))
My query
SELECT [Id]
,LEFT(SheetNumber,LEN(SheetNumber)-CHARINDEX('/',SheetNumber))
,[SubmitDateTime]
FROM [SPMS2].[dbo].[Lines] where SheetNumber like '%/%'
You dont need to use LEN. Just use
LEFT(SheetNumber,CHARINDEX('/',SheetNumber) - 1)
To make it into 3 digits with 0 in the front, you could use something like this
Right('000' + LEFT(SheetNumber,CHARINDEX('/',SheetNumber) - 1), 3)
Try this
RIGHT('000' + LEFT(SheetNumber, CHARINDEX('/', SheetNumber) - 1), 3)
Try this
DECLARE #tbl TABLE(SheetNumber VARCHAR(100));
INSERT INTO #tbl VALUES
('1/2')
,('1/4')
,('2/23')
,('12/13')
,('1/10');
SELECT STUFF(
(
SELECT ',' + REPLACE(STR(LEFT(SheetNumber,CHARINDEX('/',SheetNumber) - 1),3),' ','0')
FROM #tbl
FOR XML PATH('')
),1,1,'');
The result
001,001,002,012,001

Splitting of varchar column using the special character

I have a column which values will be
1_987123
12_987654
Here, I need to separate only the first letter that is '1' and '12' in the column values. I have tried substring and charindex to find the value. The syntax I have used is:
substring(Columnname,1,charindex('_',Columnname,1)-1).
This works to find for the column which have values. But in my case we have blank values in the column. In that case the above syntax is giving error. Tried the scenarios like coalesce and its not accepting as the column data type is varchar.
Can you please suggest me some other option?
You need to use a CASE expression to handle values without '_':
WITH Tbl(col) AS(
SELECT '1_987123' UNION ALL
SELECT '12_987654' UNION ALL
SELECT '12'
)
SELECT
CASE
WHEN CHARINDEX('_', col) > 0 THEN LEFT(col, CHARINDEX('_', col)-1)
ELSE col
END
FROM Tbl
or you can cheat a bit by apending the underscore at the back
SELECT LEFT (col, CHARINDEX('_', col + '_') - 1)
You can try like this, It supports null values, values without underscore and empty strings values in your columns.
SELECT CASE
WHEN charindex('_', COL) > 0
THEN substring(col, 1, charindex('_', COL) - 1)
ELSE col
END
FROM (
VALUES ('1_987123')
,('12_987654')
,('12')
,(NULL)
,('')
) T(COL)
Use filter like this,
SELECT CASE
WHEN charindex('_', COL) > 0
THEN substring(col, 1, charindex('_', COL) - 1)
ELSE col
END
FROM (
VALUES ('1_987123')
,('12_987654')
,('12')
,(NULL)
,('')
,('3_545')
,('3')
) T(COL)
where col like '3%'
There is a trick you can use, start at position 0 instead of position 1 and do not subtract 1. This will return empty string when there is no underscore.
SELECT
SUBSTRING(Columnname,0,charindex('_',Columnname,1))
FROM (values('1_987123'),('7123'),('12_987654')) x(columnname)
Result:
1
12

Resources