how extract first word using parsename function sql server? [duplicate] - 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

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

STRING_SPLIT from column value and reverse it in SQL Server

I am trying to reverse string values in a column in SQL Server.
What I am trying to achieve is, when the value of the column is child/parent, I want to change it to parent/child when child contains '112'
For example I want to change columns with value Reports/112-Major to 112-Major/Reports
To start with, I tried to use STRING_SPLIT and append them like String_Split(ColumnName,'/')[1] + String_Split(ColumnName,'/')[0] if String_Split(ColumnName,'/')[1] like '%112%'
Most of the examples that I see online have something like
SELECT Value FROM STRING_SPLIT('Lorem/ipsum/dolor/sit/amet.', '/');
But I want to split and then merge based on condition and then update the column
Something like,
update tblTableName
set siteUrl = String_Split(ColumnName,'/')[1] + String_Split(ColumnName,'/')[0]
where `String_Split(ColumnName,'/')[1] like '%112%'
Is there a way to do this in SQL Server?
You can use this expression:
stuff(col, 1, charindex('/', col), '') + '/' + left(col, charindex('/', col) - 1)
Another option just for fun.
Gordon's solution would be my first choice and is certainly more performant (+1), but the following illustrates a simple technique which can be used to split a string into columns and not rows.
Sample
Declare #YourTable table (ID int,YourCol varchar(100))
Insert Into #YourTable values
(1,'Reports/112-Major'),
(2,'Reports/Something Else')
Update #YourTable Set YourCol = Pos2+'/'+Pos1
From #YourTable A
Cross Apply (
Select Pos1 = ltrim(rtrim(xDim.value('/x[1]','varchar(max)')))
,Pos2 = ltrim(rtrim(xDim.value('/x[2]','varchar(max)')))
From (Select Cast('<x>' + replace((Select replace(A.YourCol,'/','§§Split§§') as [*] For XML Path('')),'§§Split§§','</x><x>')+'</x>' as xml) as xDim) as B1
) B
Where Pos2 Like '%112%'
Updated Results
ID YourCol
1 112-Major/Reports
2 Reports/Something Else

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

How to get the 1st value before delimiter in 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 '=%'

Resources