| Author |
Topic |
|
Chinni
Yak Posting Veteran
95 Posts |
Posted - 2008-10-01 : 13:55:09
|
| I have a case likecase WHEN (@income IS NULL)THEN ( '0000')WHEN (@income='') THEN ( '0000')WHEN (@income LIKE '%N%A%')THEN 'NCD'WHEN (@income = 0) THEN 'ST' ELSE 'NA'ENDreturn @income@income is DECIMAL(17,0)but @income need to be 'ncd'how to cast ?ERRORMsg 8114, Level 16, State 5, Line 28Error converting data type varchar to numeric. |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-10-01 : 14:09:42
|
| No CAST!When you need your variable @income to take something like 'NCD' or 'ST' then your variable should be declared as VARCHAR(17) because you cannot cast 'NCD' or 'ST' to a DECIMAL.WebfredPlanning replaces chance by mistake |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-01 : 14:12:56
|
| Also in the above case you're not doing any casting.what you want to do is to just put result of case into a varchar field as webfred pointed out. |
 |
|
|
Chinni
Yak Posting Veteran
95 Posts |
Posted - 2008-10-01 : 14:31:12
|
| This income field is a function and is used in another procedure.In that procedure column name Income is decimal(17,0).That cannot be changed.the value for that field is from @income,so i think this should be decimal(17,0) |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-10-01 : 14:41:26
|
quote: Originally posted by Chinni This income field is a function and is used in another procedure.In that procedure column name Income is decimal(17,0).That cannot be changed.the value for that field is from @income,so i think this should be decimal(17,0)
Sorry, but i'm total confused with this art of information and i'm not able to give any tips.WebfredPlanning replaces chance by mistake |
 |
|
|
Chinni
Yak Posting Veteran
95 Posts |
Posted - 2008-10-01 : 14:44:46
|
| Sorry-This case statement is in a fucntion.This fuction is used to retrive income field in aa Stored Procedre.In SP income field is decimal(17,0).This cannot be changedIn function if the return @income is converted to varchar that may become problem at SP.So do we have any chance with out changinf @income to varchar?and also a condition is present before case which has a value like @income = 2345 if this dont have a value then case statement.Thanks |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-10-01 : 14:54:57
|
| Where does the value in @income come from?A part of your function is: [WHEN (@income LIKE '%N%A%')THEN 'NCD'].But when @income is decimal then why you're testing its content for impossible characters like N and A?Say it's possible to return 'NCD' or 'ST' or 'NA', then what should happen in the calling Storecd Procedure?Planning replaces chance by mistake |
 |
|
|
Chinni
Yak Posting Veteran
95 Posts |
Posted - 2008-10-01 : 15:12:34
|
| @INCOME = select income from table 1income datatype: decimalIf income has a value then display value else these conditionscaseWHEN (@income IS NULL)THEN ( '0000')WHEN (@income='') THEN ( '0000')WHEN (@income LIKE '%N%A%')THEN 'NCD'WHEN (@income = 0) THEN 'ST' ELSE (select income from table )ENDThis @ income is used in stored proc that is to retrive one incoem field in Sp we use this fucntion.The retrived fields are loaded to a table2 where in table income datatype in decimalwhen i used as varchar they suggested me *You can cast to varchar later in your case statement if you want to return a stringSorry if i did not explain properly |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-10-01 : 15:33:11
|
| OK!There is table1 and it's the source.INCOME - coming from table1 - can only be a numeric value or a null value because of datatype decimal.[qoute]If income has a value then display value[/quote] What is the meaning of DISPLAY? What is the meaning of "has a value"?-->Now there is your CASE-Statement and in some cases it will return non-numeric values!-->Now the calling SP wants to store THE RETURNED VALUE in table2 where income data type is decimal?That is not possible!Don't know your system, but maybe it's a way to declare some numeric values to treat as 'NCD' or 'ST' likevalue of income = -9999999999 is treatet as 'NCD'value of income = -8888888888 is treated as 'ST'That is not a fine solution but i cannot see another way without picking your system to pieces.WebfredPlanning replaces chance by mistake |
 |
|
|
Chinni
Yak Posting Veteran
95 Posts |
Posted - 2008-10-01 : 15:45:19
|
| so what i understand is the column in source table 1 Income with datatype decimal can not have N or A as its value in the field.So the logic given is not correct in caseand we can change the source table datatype if we want to Varchar . |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-10-01 : 15:52:24
|
| Yes.Yes.Yes.But it's not the solution for the destination table and your Returnvalues - isn't it?Planning replaces chance by mistake |
 |
|
|
Chinni
Yak Posting Veteran
95 Posts |
Posted - 2008-10-01 : 16:04:07
|
| yes..I dont know why they have this logic like if NA in table1 send NCD to table2I will find out my BA regarding thissince we dont have a question of having NA in table1 |
 |
|
|
Chinni
Yak Posting Veteran
95 Posts |
Posted - 2008-10-01 : 16:12:10
|
| I have one more doubtdeclare @Incomeid varchar(17)declare @Income DECIMAL(17,0)set @income =(select v_table.income from v_table where id = 91686) set @incomeid = case WHEN (@income LIKE '%N%A%')THEN 'NCD'WHEN (@income = 0) THEN 'ST'WHEN (@income ='')THEN 'NCD'WHEN (@income IS NULL)THEN ( '0000')else @incomeendError:Msg 8114, Level 16, State 5, Line 7Error converting data type varchar to numeric.but when i dont have those two conditions its working goodwhen i split them its goodlikeif(select v_table.income from v_table where id = 91686)in not nullset @income =(select v_table.income from v_table where id = 91686) set @incomeid = case WHEN (@income LIKE '%N%A%')THEN 'NCD'WHEN (@income = 0) THEN 'ST'else @incomeendendelsebeginset @incomeid = case WHEN (@income ='')THEN 'NCD'WHEN (@income IS NULL)THEN ( '0000')else'na'endend |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2008-10-01 : 16:13:28
|
Now here it's about 10:13 PM and i will get a delicious Beer (elephant) greetingsWebfredPlanning replaces chance by mistake |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-02 : 03:19:27
|
| what's the datatype of v_table.income ?And you've declared @income as decimal. then why are you comparing it to '%N%A%'? if its decimal how can it hold N,A characters? |
 |
|
|
|