SQL 2005 DateTime syntax for WHERE clause

I have the following query:

SELECT dbo.CONTENT_Event.eventID, dbo.CONTENT_Event.EventName + ' (' + EventOrgName + ')'
FROM dbo.View_CMS_Tree_Joined_Versions
INNER JOIN dbo.CONTENT_Event
ON View_CMS_Tree_Joined_Versions.DocumentForeignKeyValue = dbo.CONTENT_Event.[eventID]
WHERE ...

I want a WHERE condition to return events for the current date and all future dates (ie. exclude past events), based on the dbo.CONTENT_Event.EventDate field (datetime).

Can anyone help out with the syntax?

Thanks.
asrisbeyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ashish PatelCommented:
SELECT dbo.CONTENT_Event.eventID, dbo.CONTENT_Event.EventName + ' (' + EventOrgName + ')'
FROM dbo.View_CMS_Tree_Joined_Versions
INNER JOIN dbo.CONTENT_Event
ON View_CMS_Tree_Joined_Versions.DocumentForeignKeyValue = dbo.CONTENT_Event.[eventID]
WHERE dbo.CONTENT_Event.EventDate >= Cast(Convert(varchar(10), GetDate(), 103) as DateTime)
0
Ashish PatelCommented:
Along with your previous question the whole query should look like this.

SELECT dbo.CONTENT_Event.eventID, dbo.CONTENT_Event.EventName + ' (' + EventOrgName + ')' + Convert(varchar(10), dbo.CONTENT_Event.EventDate, 103)
FROM dbo.View_CMS_Tree_Joined_Versions
INNER JOIN dbo.CONTENT_Event
ON View_CMS_Tree_Joined_Versions.DocumentForeignKeyValue = dbo.CONTENT_Event.[eventID]
WHERE dbo.CONTENT_Event.EventDate >= Cast(Convert(varchar(10), GetDate(), 103) as DateTime)
0
asrisbeyAuthor Commented:
OK, I've added the WHERE clause and the query now returns no results. However, if I change 103 to 101 it returns the results I would expect. This is how the data is stored in the table '13/04/2008 10:00:00' ie. UK date format, non of this American stuff!

Does that make sense? And is that an acceptable change to the WHERE clause?

Thanks again.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

ee_rleeCommented:
try this

SELECT dbo.CONTENT_Event.eventID, dbo.CONTENT_Event.EventName + ' (' + EventOrgName + ')'
FROM dbo.View_CMS_Tree_Joined_Versions
INNER JOIN dbo.CONTENT_Event
ON View_CMS_Tree_Joined_Versions.DocumentForeignKeyValue = dbo.CONTENT_Event.[eventID]
WHERE dbo.CONTENT_Event.EventDate >= CAST(FLOOR(CAST( GETDATE() AS FLOAT)) AS DATETIME)
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
what is the data type of the field? when it is datetime:
WHERE dbo.CONTENT_Event.EventDate >= convert(datetime, Convert(varchar(10), GetDate(), 120) , 120)

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ashish PatelCommented:
Oh yes, that should not make any difference
so either use either of the query

SELECT dbo.CONTENT_Event.eventID, dbo.CONTENT_Event.EventName + ' (' + EventOrgName + ')' + Convert(varchar(10), dbo.CONTENT_Event.EventDate, 103)
FROM dbo.View_CMS_Tree_Joined_Versions
INNER JOIN dbo.CONTENT_Event
ON View_CMS_Tree_Joined_Versions.DocumentForeignKeyValue = dbo.CONTENT_Event.[eventID]
WHERE dbo.CONTENT_Event.EventDate >= Cast(Convert(varchar(10), GetDate(), 101) as DateTime)

Or

SELECT dbo.CONTENT_Event.eventID, dbo.CONTENT_Event.EventName + ' (' + EventOrgName + ')' + Convert(varchar(10), dbo.CONTENT_Event.EventDate, 103)
FROM dbo.View_CMS_Tree_Joined_Versions
INNER JOIN dbo.CONTENT_Event
ON View_CMS_Tree_Joined_Versions.DocumentForeignKeyValue = dbo.CONTENT_Event.[eventID]
WHERE Cast(Convert(varchar(10), dbo.CONTENT_Event.EventDate, 103) as DateTime) >= Cast(Convert(varchar(10), GetDate(), 103) as DateTime)
0
asrisbeyAuthor Commented:
asvforce

Sorry, the 2nd solution gives me...
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

angellll, I'll go with that, seems to work fine. Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.