|
Hello, I'm receiving a conversion error in my having clause. "Conversion failed when converting the varchar value '[2010-08-01]' to data type int" I have columns named [2010-08-01] respectively for each month... I'm trying to create a having clause that will only return rows greater than zero in the most current month. This works... having [2010-08-01] > 0 but when I write it like this (below), to make sure I always get the current month, I get a conversion error: having '['+ replace(convert(char(8),getdate(),111),'/','-') + '01]' > 0 I think...(from reseaching the problem) that SQL is converting the char to int when it sees '+'. I've tried converting the entire string but that didn't work either. Any ideas how I can write this where I won't get the error? Thanks in advance for any help!! Brian
(comments are locked)
|
|
You need to use dynamic SQL for this. When you say When you say To get this to work as you want, you need to use dynamic SQL whereby you build up the string and then execute it using Alternatively, you can use thanks for help! I tried dynamic SQL and I still get the same error. this is what I did: ALTER procedure [dbo].[sp_InvCubeWeightByDate] @Whse varchar(10), @Date varchar(12) -- select, from, group having @Date > 0 and ran it like this: exec sp_InvCubeWeightByDate 'chi', [2010-08-01] I still get he same error... I'm guessing I'm still doing something wrong?
Aug 27 '10 at 08:51 AM
bvolk
What you have done there is exactly the same - you have declared a string variable, and asked SQL Server to compare a string to an int. You need something more like -
Aug 27 '10 at 08:59 AM
Matt Whitfield ♦♦
thank you! I did figure it out ... you were correct, I just needed to get my head around it. ..it's not pretty, but it works! :-) I'll post my work. Thanks again for you help!
Aug 27 '10 at 01:02 PM
bvolk
(comments are locked)
|
|
below is my solution... declare @tmpColumn varchar(12); set @tmpColumn = replace(convert(char(8),getdate(),111),'/','-') + '01' exec (' declare @Whse varchar(10) set @Whse = ''chi'' select i.itemclassdesc , i.itemsubclassdesc , #i.itemnum , #i.strat , i.itemdesc, #i.uom , sceweight , scewidth , secdepth , sceheight , cube , onhandweight , onhandcube , isnull([2009-11-01],0) as [2009-11-01] , isnull([2009-12-01],0) as [2009-12-01] , isnull([2010-01-01],0) as [2010-01-01] , isnull([2010-02-01],0) as [2010-02-01] , isnull([2010-03-01],0) as [2010-03-01] , isnull([2010-04-01],0) as [2010-04-01] , isnull([2010-05-01],0) as [2010-05-01] , isnull([2010-06-01],0) as [2010-06-01] , isnull([2010-07-01],0) as [2010-07-01] , isnull([2010-08-01],0) as [2010-08-01] , isnull([2010-09-01],0) as [2010-09-01] , isnull([2010-10-01],0) as [2010-10-01] , isnull([2010-11-01],0) as [2010-11-01] , isnull([2010-12-01],0) as [2010-12-01] from #i left outer join ditem i on #i.itemnum = i.itemnum inner join ax_mbsfcreordertable r on i.itemnum = r.itemid and r.inventlocationid = @Whse and r.active = 1 where i.itemnum not like ''@%'' group by i.itemclassdesc , i.itemsubclassdesc , #i.itemnum , #i.strat , i.itemdesc , #i.uom , sceweight , scewidth , secdepth , sceheight , cube , onhandweight , onhandcube , [2009-11-01] , [2009-12-01] , [2010-01-01] , [2010-02-01] , [2010-03-01] , [2010-04-01] , [2010-05-01] , [2010-06-01] , [2010-07-01] , [2010-08-01] , [2010-09-01] , [2010-10-01] , [2010-11-01] , [2010-12-01] having ['+ @tmpColumn +'] > 0 order by 1,2')
(comments are locked)
|

