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
Solved

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

Posted on 2013-02-03
11
705 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 

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 100 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 250 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 92

Assisted Solution

by:Patrick Matthews
Patrick Matthews earned 150 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

856 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