Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


replace


replace

Author
Message
phamm
phamm
SSC Rookie
SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)

Group: General Forum Members
Points: 43 Visits: 190
I have 2 columns in the database. The value in column 1 is 10000000. The value in column 2 is 987. How would I update column 1 with the value of column 2 like this: 10009870. Thanks for all the help.
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47261 Visits: 44392
Update table
set column1 = column1 + column2


Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


phamm
phamm
SSC Rookie
SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)

Group: General Forum Members
Points: 43 Visits: 190
Thanks. I forgot to mention that these columns are string not integer.
Greg Snidow
Greg Snidow
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1611 Visits: 2478
phamm (10/15/2012)
Thanks. I forgot to mention that these columns are string not integer.


Is there a reason they are not integer? You could try something like this...


DECLARE @col1 VARCHAR(20), @col2 VARCHAR(20)
SELECT @col1 = '10000000', @col2 = '9870'

SELECT CAST(CAST(@col1 AS INT) + CAST(@col2 AS INT) AS VARCHAR(20))



A couple of caveats though. Since they are strings (varchar?, char?) you may have some instances of leading zeros which will be lost, so you will have to deal with finding them and adding them back. Also, it will fail if there are any non-numeric data in your values.

Greg
_________________________________________________________________________________________________
The glass is at one half capacity: nothing more, nothing less.
jfdelette
jfdelette
SSC Journeyman
SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)

Group: General Forum Members
Points: 94 Visits: 510
You can try this :

update table
set column1 = convert(int,column1) + convert(int,column2)



Jeff.
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8550 Visits: 18142
I don't have SQL in this computer right now, but something like this could solve the issue.

UPDATE table SET
column1 = STUFF( column1, LEN(column1) - LEN(column2) - 1,LEN(column2), column2)




Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
phamm
phamm
SSC Rookie
SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)SSC Rookie (43 reputation)

Group: General Forum Members
Points: 43 Visits: 190
Thank you very much. what if my column1 have value 'abcdef0000' and column 2 value is 123. Then I cannot convert them to integer to add them together. In this case, how would I handle it, can I use replace, right, etc. which I tried to play around with but did not get the correct result which is 'abcdef01230'.
Luis Cazares
Luis Cazares
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8550 Visits: 18142
phamm (10/15/2012)
Thank you very much. what if my column1 have value 'abcdef0000' and column 2 value is 123. Then I cannot convert them to integer to add them together. In this case, how would I handle it, can I use replace, right, etc. which I tried to play around with but did not get the correct result which is 'abcdef01230'.


Did you try STUFF as I suggested?


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
jfdelette
jfdelette
SSC Journeyman
SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)

Group: General Forum Members
Points: 94 Visits: 510
hmmm, it's dirty but it's works fine.

;with CTE as (select * from table
where c1 like '%[0-9]%'
and c1 not like '%[a-z]%')
update cte
set c1 = convert(int,c1)+convert(int,c2)
where c2 like '%[0-9]%'
and c2 not like '%[a-z]%'



Jeff.
jfdelette
jfdelette
SSC Journeyman
SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)

Group: General Forum Members
Points: 94 Visits: 510
You can try this too :

update table
set c1 = convert(int,c1)+convert(int,c2)
where isnumeric(c1) = 1
and isnumeric(c2)= 1



Jeff.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search