all groups > sql server programming > march 2004 >
You're in the

sql server programming

group:

Case statement


Case statement ajmiester
3/13/2004 9:26:10 PM
sql server programming: Hi
tmp_table
company_name char(33)
prd_yr char(4)
prd_month char(2)
value char(25)


select company_name,
sum (case prd_yr when 2001 then convert(numeric(25,5),value) else 0 end)
as yr1,
sum (case prd_yr when 2002 then convert(numeric(25,5),value) else 0 end)
as yr2,
sum (case prd_yr when 2003 then convert(numeric(25,5),value) else 0 end)
as yr3

from tmp_table
group by company_name
go

But due to NULL values in the value field the query is giving me an error
message

Server: Msg 8114. level 16, state 5, line 1
Error converting data type varchar to numeric

Is there a way to correct this.

Please help
Ajay

Re: Case statement oj
3/14/2004 6:38:25 PM
select company_name,
sum (case prd_yr when 2001 then convert(numeric(25,5),isnull(value,0)) else
0 end)
as yr1,
sum (case prd_yr when 2002 then convert(numeric(25,5),isnull(value,0)) else
0 end)
as yr2,
sum (case prd_yr when 2003 then convert(numeric(25,5),isnull(value,0)) else
0 end)
as yr3

--
-oj
http://www.rac4sql.net


[quoted text, click to view]

Re: Case statement Grant Case
3/14/2004 8:44:41 PM
Don't use CHAR data type to hold numbers. Avoid the use of the numeric
datatype, use decimal instead. Otherwise the following should work:

select company_name,
sum (case prd_yr when 2001 then CAST(value AS DECIMAL (25,5)) else 0
end)
as yr1,
sum (case prd_yr when 2002 then CAST(value AS DECIMAL (25,5)) else 0
end)
as yr2,
sum (case prd_yr when 2003 then CAST(value AS DECIMAL (25,5)) else 0
end)
as yr3

from tmp_table
group by company_name



[quoted text, click to view]

Re: Case statement Steve Kass
3/14/2004 10:42:47 PM
Ajay,

I don't see why NULL in the value column would cause any problem. I
suspect you have empty strings or other strings that don't represent
numeric values. As others mentioned, you shouldn't store numeric values
as char(25). I'd also suggest writing your years in the query as
'2001', '2002', etc., since with 2001, 2002, etc., you are requiring an
implicit type conversion that's not necessary.

Here's a repro to show that NULL in the value column is fine:

create table tmp_table (
company_name char(33),
prd_yr char(4),
prd_month char(2),
value char(25)
)
insert into tmp_table values ('abc','2001','12',null)
insert into tmp_table values ('abc','2001','12',0)
insert into tmp_table values ('abc',null,'12',14)

select company_name,
sum (case prd_yr when 2001 then convert(numeric(25,5),value) else 0 end)
as yr1,
sum (case prd_yr when 2002 then convert(numeric(25,5),value) else 0 end)
as yr2,
sum (case prd_yr when 2003 then convert(numeric(25,5),value) else 0 end)
as yr3

from tmp_table
group by company_name
go

drop table tmp_table

SK

[quoted text, click to view]
Re: Case statement ajmiester
3/14/2004 11:01:50 PM
Thank you Steve,
The table was created by someone else and the value field has (null)
values that are causing the error. I tried to insert the data into a tmp
table with a nuneric value field but that to failed at the (null) value. Any
suggestion how to fix that.

Ajay

[quoted text, click to view]

Re: Case statement Steve Kass
3/15/2004 12:24:13 AM
Ajay,

NULL shouldn't be a problem. Is it possible that what's in the table
is the string 'NULL' or '(null)' ? If that's the case, how about
running this before processing the data?

update tmp_table set
value = null
where upper(value) like '%NULL%'

SK

[quoted text, click to view]
AddThis Social Bookmark Button