Click here to Skip to main content
15,905,867 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hello friends...

i have following query.


SQL
declare @month int
declare @year int
set @month=8
set @year =2014

declare @startdate datetime
declare @enddate datetime
set @startdate=CONVERT(datetime,convert(varchar,@year)+'-'+convert(varchar,@month)+'-'+'01')

if (@month>= MONTH(GETDATE()) and @year>=YEAR(GETDATE()))
begin
	set @enddate=(select GETDATE()) 
end
else
begin
	set @enddate=DATEADD(day,-1,DATEADD(month,1,@startdate))
end

create table #suspended_alumina_gpl
(
	edate datetime,
	avgliq decimal(18,5)
)

while (@startdate<=@enddate)
begin
	
	insert into #suspended_alumina_gpl 
	select convert(datetime,@startdate),isnull(AVG(spent_liquor),'') from suspended_alumina_gpl
	where CONVERT(varchar,edatetime,103)=CONVERT(varchar,@startdate)
		
	set @startdate=DATEADD(day,1,@startdate)
end
select * from #suspended_alumina_gpl



but it throws an error as follows while executing it.

Msg 8114, Level 16, State 5, Line 29
Error converting data type varchar to numeric.
Posted
Updated 18-Aug-14 23:36pm
v2

Hi
May be Issue is here
isnull(AVG(spent_liquor),'')


if the AVG value is NULL then you are trying insert Empty into decimal field (avgliq), So set it to "0" or some other numeric value. Do not set Empty character.

So use like below:-
SQL
insert into #suspended_alumina_gpl
    select convert(datetime,@startdate),isnull(AVG(spent_liquor),0) from suspended_alumina_gpl
    where CONVERT(varchar,edatetime,103)=CONVERT(varchar,@startdate)


hope it helps.
 
Share this answer
 
Comments
TAUSEEF KALDANE 19-Aug-14 6:04am    
thank you sir
if you are inserting avg value in numeric field then use
SQL
isnull(AVG(spent_liquor),0)
 
Share this answer
 
Comments
TAUSEEF KALDANE 19-Aug-14 6:04am    
thank you vinay sir
sasyu 27-Oct-14 15:08pm    
Hi,

MY scenario is In staging the the data type of field is varchar for example
select Equipment_Size from dwpsa..STG_CAP_QUOTE_DTL
result :2.0yd(S)

IN core the column is loading by changing to numeric
as 2.00000

so my question is how can change the varchar to int (2.0yd(S) to 2.00000)
i tried many examples but no use can any one help me please....
Mr. Karthik and Vinay suggested well
but if u want to put '' when your AVG(spent_liquor) is null then just change the datatype of your
"avgliq" field in the temp table to "FLOAT"
problem solved
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900