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
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] "ajmiester" <ajmister@opyonline.net> wrote in message news:%23tCWkTjCEHA.3344@tk2msftngp13.phx.gbl... > 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 > >
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] "ajmiester" <ajmister@opyonline.net> wrote in message news:%23tCWkTjCEHA.3344@tk2msftngp13.phx.gbl... > 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 > >
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] ajmiester wrote: >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 > > > >
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] "Steve Kass" <skass@drew.edu> wrote in message news:#tO2x#jCEHA.3064@tk2msftngp13.phx.gbl... > 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 > > ajmiester wrote: > > >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 > > > > > > > > >
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] ajmiester wrote: >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 > >"Steve Kass" <skass@drew.edu> wrote in message >news:#tO2x#jCEHA.3064@tk2msftngp13.phx.gbl... > > >>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 >> >>ajmiester wrote: >> >> >> >>>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 >>> >>> >>> >>> >>> >>> > > > >
Don't see what you're looking for? Try a search.
|