Solved

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

Posted on 2013-02-03
11
699 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now