Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-02-03
11
Medium Priority
?
725 Views
Last Modified: 2013-02-03
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?
0
Comment
Question by:al4629740
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 9

Expert Comment

by:mimran18
ID: 38849022
Because [EntryTime] field is int and you are comparing it with 'December'

Make it like Month(EntryTime) =12 then it will work
0
 

Author Comment

by:al4629740
ID: 38849026
How can I change Month(EntryTime)?

I would like to keep it as ='December'
0
 
LVL 9

Expert Comment

by:mimran18
ID: 38849034
Then you can do it like this

Datename (mm,[EntryTime]) ='December'
0
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

Author Comment

by:al4629740
ID: 38849047
Does that work if I do this, using the < symbol

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

Expert Comment

by:mimran18
ID: 38849051
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
 

Author Comment

by:al4629740
ID: 38849053
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
 
LVL 9

Expert Comment

by:Aeriden
ID: 38849055
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
 

Author Comment

by:al4629740
ID: 38849057
Case statement?

Do you have an example?
0
 
LVL 9

Assisted Solution

by:Aeriden
Aeriden earned 400 total points
ID: 38849062
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
 
LVL 9

Accepted Solution

by:
mimran18 earned 1000 total points
ID: 38849072
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
 
LVL 93

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 600 total points
ID: 38849074
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

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question