Conversion failed when converting the varchar value to data type int.

I get the following error:

Conversion failed when converting the varchar value 'December' to data type int.

for the following sql string


select * from frmRegistrationCSW where Fiscal = 2013 and Month(EntryTime) ='December'
and frmRegistrationCSWCheck11 = 1

How do I convert it?
al4629740Asked:
Who is Participating?
 
mimran18Commented:
You can make it like this

Declare @Month as int

Set @Month =
Case When Datename(mm, Month(EntryTime))='December' Then 12
When Datename(mm, Month(EntryTime))='February' Then 2

END
Select @Month

And then you can use Month(EntryTime) =@Month
0
 
mimran18Commented:
Because [EntryTime] field is int and you are comparing it with 'December'

Make it like Month(EntryTime) =12 then it will work
0
 
al4629740Author Commented:
How can I change Month(EntryTime)?

I would like to keep it as ='December'
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
mimran18Commented:
Then you can do it like this

Datename (mm,[EntryTime]) ='December'
0
 
al4629740Author Commented:
Does that work if I do this, using the < symbol

Datename (mm,[EntryTime]) <= 'December'
0
 
mimran18Commented:
No, it will not wotk, Better you need to convert it into int and use it.
you can make a case statement if it is december then return 12 and can use it here.
0
 
al4629740Author Commented:
Why do both of this statements produce different results?


select * from frmRegistrationCSW where Fiscal = 2013 and EntryTime <= '12/31/2012' and frmRegistrationCSWCheck11 = 1



select * from frmRegistrationCSW where Fiscal = 2013 and Datename (mm,[EntryTime]) <= 'December' and frmRegistrationCSWCheck11 = 1

Open in new window

0
 
AeridenCommented:
I don't think that would make sense (because doing a character comparison).  Month(EntryTime)<=12 would normally make more sense except that generally that should always be true (I am unaware of any month being greater than 12 :-)).  Perhaps you would like to describe what it is you are trying to achieve?
0
 
al4629740Author Commented:
Case statement?

Do you have an example?
0
 
AeridenCommented:
Your two select statements differ because one is comparing dates (EntryTime <= '12/31/2012').  The '12/31/2012' is converted to a datetime automatically.

The select select statement doesn't make sense because you are trying to compare text (e.g. is 'January' <= 'December' in terms of text?).
0
 
Patrick MatthewsCommented:
You asked:

Why do both of this [sic] statements produce different results?

Looking just at the part that differs...

EntryTime <= '12/31/2012'

SQL Server is implicitly converting '12/31/2012' into a datetime value, and that is why it works.

Datename (mm,[EntryTime]) <= 'December'

Here, SQL Server is doing a text comparison.  In a text comparison, 'April' is less than 'December', but 'July" is greater than 'Decemeber' (textual inequality operators use alphabetical order).
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.