Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string after specific character in SQL Server and update this value to specific column

I have table with data 1/1 to 1/20 in one column. I want the value 1 to 20 i.e value after '/'(front slash) is updated into other column in same table in SQL Server.

Example:

Column has value 1/1,1/2,1/3...1/20
new Column value 1,2,3,..20

That is, I want to update this new column.

like image 934
SHEKHAR SHETE Avatar asked Feb 13 '12 11:02

SHEKHAR SHETE


People also ask

How do I get the 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.

How do I separate a text string in SQL?

The STRING_SPLIT(string, separator) function in SQL Server splits the string in the first argument by the separator in the second argument. To split a sentence into words, specify the sentence as the first argument of the STRING_SPLIT() function and ' ' as the second argument. FROM STRING_SPLIT( 'An example sentence.

How to split a string just after a certain character in SQL?

First, to split a string with SQL Server just after a certain character, use the three following built-in text functions: SUBSTRING () to trim a text by indicating the start and length to be trimmed.

How do I split a column into two columns in SQL?

In Sql 2017 and above, it’s as simple as using the STRING_SPLIT function: STRING_SPLIT ( [col], 'character to split with') AS [column name] CROSS APPLY STRING_SPLIT ( [col], 'character to split with') AS [values] The two examples there will do it.

How do you split a sentence into words in SQL Server?

sentence. The STRING_SPLIT (string, separator) function in SQL Server splits the string in the first argument by the separator in the second argument. To split a sentence into words, specify the sentence as the first argument of the STRING_SPLIT () function and ' ' as the second argument.

How to split string delimited by commas in SQL Server?

Moreover, the “STRING SPLIT” table_valued function returns an empty table if the input string is NULL. Each database is connected with a compatibility level. It enables the database’s behavior to be compatible with the particular SQL Server version it runs on. Now we will call “string_split” function to split string delimited by commas.


1 Answers

Try this:

UPDATE YourTable SET Col2 = RIGHT(Col1,LEN(Col1)-CHARINDEX('/',Col1)) 
like image 170
Lamak Avatar answered Oct 02 '22 01:10

Lamak