Naveen
Naveen

Reputation: 701

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

Upvotes: 30

Views: 121255

Answers (4)

Khan
Khan

Reputation: 18162

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

Upvotes: 52

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 '=%' 

Upvotes: -3

AgentSQL
AgentSQL

Reputation: 2940

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

Upvotes: 13

mucio
mucio

Reputation: 7119

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

Upvotes: 0

Related Questions