Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Conversion failed when converting date and/or time from character string

Posted on 2011-05-11
3
Medium Priority
?
943 Views
Last Modified: 2012-05-11
Hey- I am yet to figure out the syntax for the date issue with collation.

declare @beginDate datetime, @endDate datetime
set @beginDate = '04/01/2011' ---This is always a rolling 2-years.
set @endDate = '05/01/2011'
Select
a.DischargeDateTime,
a.AbstractID,
a.VisitID,
a.AccountNumber
from dbo.AbstractData as a
where a.DischargeDateTime Collate SQL_Latin1_General_CP1_CS_AS  >= '@beginDate'  and a.DischargeDateTime Collate SQL_Latin1_General_CP1_CS_AS  < '@beginDate'
Order By
a.DischargeDateTime Collate SQL_Latin1_General_CP1_CS_AS

I have tried MANY different statements nothing has worked.  This errors as ‘Msg 447, Level 16, State 0, Line 4
Expression type datetime is invalid for COLLATE clause.’

Original Statement:
/****  More testing   ****/
Drop table ##AbstractData
declare @beginDate datetime, @endDate datetime
set @beginDate = '04/01/2009'
set @endDate = '05/01/2011'
Select
a.DischargeDateTime,
a.AbstractID,
a.VisitID,
a.AccountNumber
into ##AbstractData
from dbo.AbstractData as a
where (a.DischargeDateTime  >= '@beginDate'  and a.DischargeDateTime < '@endDate')
Order By a.VisitID

Without a collation statement I get the error of ‘Conversion failed when converting date and/or time from character string.’

What did you do?  This is driving me crazy.  Sigh.  And yes- I have converted and cast’d with still no luck!  The data will return when I convert, but it is not filtering.  Lot’s of documentation on this- but, nothing with a date example like this.
0
Comment
Question by:jrharton
  • 2
3 Comments
 
LVL 15

Accepted Solution

by:
Aaron Shilo earned 2000 total points
ID: 35738819
try this

Drop table ##AbstractData
declare @beginDate datetime, @endDate datetime
set @beginDate = '04/01/2009' 
set @endDate = '05/01/2011'
Select 
a.DischargeDateTime,
a.AbstractID,
a.VisitID,
a.AccountNumber
into ##AbstractData
from dbo.AbstractData as a 
where (CONVERT(nvarchar(30), a.DischargeDateTime  , 101)>=@beginDate 
 and CONVERT(nvarchar(30), a.DischargeDateTime , 101)< @endDate)
Order By a.VisitID

CONVERT(nvarchar(30), GETDATE(), 126)

Open in new window

0
 
LVL 15

Expert Comment

by:Aaron Shilo
ID: 35738827
disregard this line : CONVERT(nvarchar(30), GETDATE(), 126)
0
 

Author Closing Comment

by:jrharton
ID: 35738926
Thank you!  works perfectly...
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

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…
I have a large data set and a SSIS package. How can I load this file in multi threading?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.
Suggested Courses

810 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