?
Solved

SQL 2005 DateTime syntax for WHERE clause

Posted on 2008-02-07
7
Medium Priority
?
5,895 Views
Last Modified: 2008-02-07
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.
0
Comment
Question by:asrisbey
7 Comments
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20839642
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
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20839665
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
 

Author Comment

by:asrisbey
ID: 20839690
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
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
LVL 26

Expert Comment

by:ee_rlee
ID: 20839698
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
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 20839702
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
 
LVL 23

Expert Comment

by:Ashish Patel
ID: 20839762
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
 

Author Comment

by:asrisbey
ID: 20839789
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

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

Question has a verified solution.

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

Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
This shares a stored procedure to retrieve permissions for a given user on the current database or across all databases on a server.
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 INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Suggested Courses

601 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