2

I have 2 columns - first one column A contains strings "abc/def" and second column B is empty. I would like to split strings and update columns with following results:

first column A: abc second column B: def

For all column A entries.

2
  • I recommend that you read about basic SQL string manipulation functions and basic update statement. There are also few examples on SO. Commented Mar 10, 2017 at 13:14
  • You can find many examples about it. Please google it and you wil find many things. Commented Mar 10, 2017 at 13:34

3 Answers 3

2

Using charindex() to find the location of the delimiter, left() to get the left part, and stuff() to get the remaining part:

update t
set a = left(a, charindex('/',a+'/')-1)
  , b = stuff(a, 1,charindex('/',a+'/'),'')
;

select * from t;

rextester demo: http://rextester.com/WZFPH72760

returns:

+-----+-----+
|  a  |  b  |
+-----+-----+
| abc | def |
+-----+-----+
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

select SUBSTRING('abc/def', 0, CHARINDEX('/','abc/def'))
select SUBSTRING('abc/def', CHARINDEX('/','abc/def')+1, len('abc/def'))

The first one for column A and the second one for column B

UPDATED with your update:

update myTable set ColumnA = SUBSTRING(ColumnA, 0, CHARINDEX('/',ColumnA)), ColumnB= SUBSTRING(ColumnA, CHARINDEX('/',ColumnA)+1, len(ColumnA))

Comments

0

You can try with "SUBSTRING"...

   update <table> set 
       columnB = (select SUBSTRING(columnA, CHARINDEX('/',columnA)+1, len(columnA)) from <table>),
       columnA = (select SUBSTRING(columnA, 0, CHARINDEX('/',columnA)) from <table>);

or look this link for use STRING_SPLIT transact-Sql..

Hope this help!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.