Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 update statement

Author  Topic 

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2008-02-21 : 04:14:52
i'm trying to update this statement, but the problem is col20 is
nchar(20 datatype and col10 is decimal(5,2).
i'm using the above statement.
i'm getting error.
Error converting data type nvarchar to numeric. please guide me.
UPDATE TABLE17 SET COL10=cast(COL20 as decimal(5,2))
GO


Vinod
Even you learn 1%, Learn it with 100% confidence.

PeterNeo
Constraint Violating Yak Guru

357 Posts

Posted - 2008-02-21 : 04:20:48
Try using ISNUMERIC(COL20) = 1 in where clause
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-21 : 04:21:36
You should make sure that COL20 has only numeric data

UPDATE TABLE17 SET COL10=cast(COL20 as decimal(5,2))
WHERE COL20 not like '%[^0-9]%'


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-21 : 04:50:02
quote:
Originally posted by PeterNeo

Try using ISNUMERIC(COL20) = 1 in where clause


Note that ISNUMERIC() is not reliable

Select data from
(
select '123' as data
union all
select '12d3'
) as t
where ISNUMERIC(data)=1

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2008-02-21 : 05:12:33
thank you very much

Vinod
Even you learn 1%, Learn it with 100% confidence.
Go to Top of Page

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2008-02-21 : 05:28:55
SORRY for not conveying completly...
actually, if col20 ='y', then col10 should be 1. if col20='N', then col10 should be 2.

i've written like this....please correct me

UPDATE TABLE17 SET COL10= CASE WHEN COL20='Y' THEN '1' ELSE COL20='N' THEN '2' else '0'
col20 is nchar(2) datatype and col10 is decimal(5,2).
sorry for not giving complete information.


help me in this regard


Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-21 : 05:54:01

Try

UPDATE
TABLE17
SET COL10= CASE WHEN COL20='Y' THEN '1' WHEN COL20='N' THEN '2' else '0' END

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

sunsanvin
Master Smack Fu Yak Hacker

1274 Posts

Posted - 2008-02-22 : 01:00:42
Thankyou very mcuh Madhi...
it worked for me

Vinod
Even you learn 1%, Learn it with 100% confidence.
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-22 : 01:38:14
Also If col10 would have only 0,1 or 2, then change the datatype of Col10 to tinyint

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -