Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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

like image 1000
Naveen Avatar asked Oct 23 '13 16:10

Naveen


People also ask

How do I extract text before a specific character in SQL?

Using the charindex function allows you to search for the @ sign and find its starting position and then the substring is used to extract the characters before the @ sign.

How do I get the first value in SQL?

We could use FIRST_VALUE() in SQL Server to find the first value from any table. FIRST_VALUE() function used in SQL server is a type of window function that results in the first value in an ordered partition of the given data set.

How do I extract a string after a specific character in SQL Server?

The SUBSTRING() extracts a substring with a specified length starting from a location in an input string. In this syntax: input_string can be a character, binary, text, ntext, or image expression. start is an integer that specifies the location where the returned substring starts.


1 Answers

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
like image 180
Khan Avatar answered Sep 20 '22 06:09

Khan