views:

28

answers:

1

For eg. I have table T1. There are 4 columns in it c1 c2 c3 and c4. I have c1 as id, c2 contains combine name and address. c3 and c4 are empty. There are multiple rows for given id. Let's say there are 10 rows for id=10.

What I want is for all the rows with id=10, I want to read c2, separate values in c2 as name and address and store name in c3 and address in c4.

How can I do this in SQL server 2005/2008?

Thanks

+1  A: 

Try:

UPDATE YourTable
    SET c3=LEFT(c2,CHARINDEX(' ',c2))
       ,c4=RIGHT(c2,LEN(c2)-CHARINDEX(' ',c2))
    WHERE c1=@YourIdValue

In the question, the method to separate values in c2 as name and address is not described, so I just split column c2 based on the first space found. c2='abcd efgh' becomes: c3='abcd', c4='efgh'.

Working sample:

DECLARE @YourTable table (c1 int,c2 varchar(10),c3 varchar(10),c4 varchar(10))
INSERT @YourTable VALUES (1,'aaaa bbbb',null,null)
INSERT @YourTable VALUES (1,'aaa bbb'  ,null,null)
INSERT @YourTable VALUES (1,'aa bb'    ,null,null)
INSERT @YourTable VALUES (1,'a b'      ,null,null)
INSERT @YourTable VALUES (2,'222 333'  ,null,null)
INSERT @YourTable VALUES (2,'aaa bbb'  ,null,null)
INSERT @YourTable VALUES (3,'a b'      ,null,null)

DECLARE @YourIdValue int
SET @YourIdValue=1

UPDATE @YourTable
    SET c3=LEFT(c2,CHARINDEX(' ',c2))
       ,c4=RIGHT(c2,LEN(c2)-CHARINDEX(' ',c2))
    WHERE c1=@YourIdValue

SELECT * FROM @YourTable

OUTPUT:

c1          c2         c3         c4
----------- ---------- ---------- ----------
1           aaaa bbbb  aaaa       bbbb
1           aaa bbb    aaa        bbb
1           aa bb      aa         bb
1           a b        a          b
2           222 333    NULL       NULL
2           aaa bbb    NULL       NULL
3           a b        NULL       NULL

(7 row(s) affected)
KM
Thanks a lot KM, I want to apply logic such as: for C2 column read all characters till a number is read; all the values till number goes in c3 and rest till the end goes in c4!! Can you appy this in your above query for meThanks again in advance!
Kris
Also, if the number is read is earlier in the string thn do nothinig with that row. Like is index of number in below or equal to 6 than do nothing with that row and proceed to next row, else split where there is number in the column c2.
Kris
scope creep, seriously, what else will change? those are the kind of important things to know up front. just write a loop.
KM
I am completely new to SQL...So I dont know whether any function exists that can identify char is number or not. similar to CHARINDEX!
Kris
You should never combine multiple items into a single column, if you do, you end up with a mess like you are in now. To determine numbers look at `ISNUMERIC()`, and the LIKE syntax: `select 'found it!' where '1' like '[0-9]'`. You'll find pulling a number (a group of digits) out of a string is a real pain to do in SQL. I'd look to create a function that you can use. You'll most likely need a WHILE loop in this function. I'd ask a new question about how to pull a number (a group of digits) out of a column for a set of rows. Take that answer and build on it to get the rest of your problem solved
KM