x

Converting error in having clause

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
more ▼

asked Aug 27, 2010 at 08:02 AM in Default

bvolk gravatar image

bvolk
33 2 2 2

(comments are locked)
10|1200 characters needed characters left

2 answers: sort voted first

You need to use dynamic SQL for this.

When you say [2010-08-01] > 0 you are saying 'compare the value in the column named 2010-08-01 to 0'.

When you say '['+ replace(convert(char(8),getdate(),111),'/','-') + '01]' > 0 you are saying 'compare the string value '[2010-08-01]' to 0'.

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 sp_executesql or EXEC.

Alternatively, you can use UNPIVOT to get the tabular results into a more natural table result, and query it using normal SQL.
more ▼

answered Aug 27, 2010 at 08:20 AM

Matt Whitfield gravatar image

Matt Whitfield ♦♦
29.4k 61 65 87

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, 2010 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 -

DECLARE @sql varchar(MAX)
SET @Sql = 'SELECT ... HAVING ' + @Date + ' > 0'
EXEC (@sql)
Aug 27, 2010 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, 2010 at 01:02 PM bvolk
(comments are locked)
10|1200 characters needed characters left

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')
more ▼

answered Aug 27, 2010 at 01:24 PM

bvolk gravatar image

bvolk
33 2 2 2

(comments are locked)
10|1200 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

New code box

There's a new way to format code on the site - the red speech bubble logo will automatically format T-SQL for you. The original code box is still there for XML, etc. More details here.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

SQL Server Central

Need long-form SQL discussion? SQLserverCentral.com is the place.

Topics:

x986
x29

asked: Aug 27, 2010 at 08:02 AM

Seen: 1181 times

Last Updated: Aug 27, 2010 at 08:02 AM