Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

replace Expand / Collapse
Author
Message
Posted Monday, October 15, 2012 2:18 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, August 7, 2014 12:46 PM
Points: 43, Visits: 182
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.
Post #1372933
Posted Monday, October 15, 2012 2:22 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:50 AM
Points: 39,977, Visits: 36,339
Update table
set column1 = column1 + column2



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
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

Post #1372934
Posted Monday, October 15, 2012 2:38 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, August 7, 2014 12:46 PM
Points: 43, Visits: 182
Thanks. I forgot to mention that these columns are string not integer.
Post #1372942
Posted Monday, October 15, 2012 2:50 PM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Wednesday, August 27, 2014 1:06 PM
Points: 1,563, Visits: 2,392
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.
Post #1372946
Posted Monday, October 15, 2012 2:54 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 3:28 AM
Points: 87, Visits: 469
You can try this :

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

Jeff.
Post #1372949
Posted Monday, October 15, 2012 3:01 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 10:33 AM
Points: 3,629, Visits: 8,136
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1372953
Posted Monday, October 15, 2012 3:03 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Thursday, August 7, 2014 12:46 PM
Points: 43, Visits: 182
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'.
Post #1372956
Posted Monday, October 15, 2012 3:13 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 10:33 AM
Points: 3,629, Visits: 8,136
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1372962
Posted Monday, October 15, 2012 3:14 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 3:28 AM
Points: 87, Visits: 469
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.
Post #1372963
Posted Monday, October 15, 2012 4:27 PM
SSC Journeyman

SSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC JourneymanSSC Journeyman

Group: General Forum Members
Last Login: Yesterday @ 3:28 AM
Points: 87, Visits: 469
You can try this too :

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

Jeff.
Post #1372986
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse