Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2013-02-03
11
Medium Priority
?
718 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 

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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

721 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