Author |
Topic |
skybvi
Posting Yak Master
193 Posts |
Posted - 2012-02-10 : 10:01:35
|
Hi, update abc123 SET abc123.WH1_Stock = case when(abc123.WH1_Stock = '0.00000') then IS NULL else abc123.WH1_Stock endI am gettting "Incorrect syntax near "IS"the datatype for WH1_Stock is numeric (20,5).Regards,SushantDBAVirgin Islands(U.K) |
|
skybvi
Posting Yak Master
193 Posts |
Posted - 2012-02-10 : 10:03:59
|
If i change 'IS NULL' to ''then i getError converting data type varchar to numeric.Regards,SushantDBAVirgin Islands(U.K) |
 |
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-02-10 : 10:42:16
|
Don't use IS unless checking for NULLtry: UPDATE abc123SET [WH1_Stock] = CASE WHEN [WH1_Stock] = 0.00000 THEN NULL ELSE [WH1_Stockend] END Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-10 : 10:55:59
|
you just need thisupdate abc123SET abc123.WH1_Stock = NULLIF(abc123.WH1_Stock ,0.00000) ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
skybvi
Posting Yak Master
193 Posts |
Posted - 2012-02-10 : 10:56:02
|
Thanks charlie,It worked.Regards,SushantDBAVirgin Islands(U.K) |
 |
|
skybvi
Posting Yak Master
193 Posts |
Posted - 2012-02-10 : 10:57:24
|
Thanks Visakh.Regards,SushantDBAVirgin Islands(U.K) |
 |
|
skybvi
Posting Yak Master
193 Posts |
Posted - 2012-02-10 : 11:42:32
|
:(now wtf happened here.update abc123set Itemtype= CASE WHEN cast(itemtype as Varchar(7))='1' tHEN 'ACTIVE' WHEN cast(itemtype as Varchar(7))='2' then 'DISC' else Itemtypeend.P.S. Itemtype datatype is smallint.Error:-- Conversion failed when converting the varchar value 'ACTIVE' to data type smallint.Regards,SushantDBAVirgin Islands(U.K) |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-10 : 11:58:01
|
thats obvious. how do you think you can put values ACTIVE ,DISC etc in smallint field? What meaning does it make? you're trying to put values to Itemtype which is smallint in your above update which is wht it breaks------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
skybvi
Posting Yak Master
193 Posts |
Posted - 2012-02-10 : 11:59:25
|
quote: Originally posted by visakh16 thats obvious. how do you think you can put values ACTIVE ,DISC etc in smallint field? What meaning does it make? you're trying to put values to Itemtype which is smallint in your above update which is wht it breaks
But i first trying to change itemtype to varcharRegards,SushantDBAVirgin Islands(U.K) |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-10 : 12:04:18
|
quote: Originally posted by skybvi
quote: Originally posted by visakh16 thats obvious. how do you think you can put values ACTIVE ,DISC etc in smallint field? What meaning does it make? you're trying to put values to Itemtype which is smallint in your above update which is wht it breaks
But i first trying to change itemtype to varcharRegards,SushantDBAVirgin Islands(U.K)
nope..you're just changing type for comparison but not actual type in table ie it still remains as smallint onlyso once you do comparison and you put final value back it again tries to convert it back to smallint and breaks as ACTIVE etc are not numeric.so if you really want to do this, you should do it like belowALTER TABLE abc123 ALTER COLUMN itemtype varchar(7) NULLGOupdate abc123set Itemtype= CASE itemtype WHEN '1' tHEN 'ACTIVE'WHEN '2' then 'DISC'else Itemtypeend. ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
skybvi
Posting Yak Master
193 Posts |
Posted - 2012-02-10 : 12:18:04
|
Yes visakh, its working now...But i don't understand 1 thing,Every time we do change of datatypes by CAST and CONVERT statements,then, why this time we altering the table design to change the datatype of a column.Whats the difference this time.Regards,SushantDBAVirgin Islands(U.K) |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-10 : 12:33:16
|
quote: Originally posted by skybvi Yes visakh, its working now...But i don't understand 1 thing,Every time we do change of datatypes by CAST and CONVERT statements,then, why this time we altering the table design to change the datatype of a column.Whats the difference this time.Regards,SushantDBAVirgin Islands(U.K)
you can change data type using cast or convert in SELECT but in that case it gets changed only in the resultset not in actual objects where column resides. so whenever you try to store some value back like you did above it tries to convert it back to its native data type and fails if its imcompatible. so if ever you want store changed data type values back in table column you need to alter column data type before hand and then apply your rules of conversion------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
skybvi
Posting Yak Master
193 Posts |
Posted - 2012-02-10 : 14:21:03
|
quote: Originally posted by visakh16
quote: Originally posted by skybvi Yes visakh, its working now...But i don't understand 1 thing,Every time we do change of datatypes by CAST and CONVERT statements,then, why this time we altering the table design to change the datatype of a column.Whats the difference this time.Regards,SushantDBAVirgin Islands(U.K)
you can change data type using cast or convert in SELECT but in that case it gets changed only in the resultset not in actual objects where column resides. so whenever you try to store some value back like you did above it tries to convert it back to its native data type and fails if its imcompatible. so if ever you want store changed data type values back in table column you need to alter column data type before hand and then apply your rules of conversion
Cool....Thanks a lot.Regards,SushantDBAVirgin Islands(U.K) |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-10 : 16:05:02
|
welcome Hope I was able to explain it clearly for you to understand------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|