Quantcast
Viewing latest article 8
Browse Latest Browse All 10

sql convert date

Question:

I’m having trouble with the following WHERE clause;

WHERE CONVERT(varchar, SessionDateTime, 101) = CONVERT(varchar, #tmdate2#, 101)

The value of tmdate2 is “04/23/2010″ and the value of SessionDateTime is “4/23/2010 8:37:31″.

I generate tmdate2 with the following;
<cfset tmdate2=”#dateformat(DateOfCall,”mm/dd/yyyy hh:mm:ss”)#”>

I just added the time to the end of it and get the following error;
[Macromedia][SequeLink JDBC Driver][ODBC Socket][Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near ‘12′.

This is the WHERE clause;
WHERE CONVERT(varchar, SessionDateTime, 101) = CONVERT(varchar, 04/23/2010 12:04:00, 101)

When I leave the time off, I get zero rows returned and I know the data is there.

Solution:

> The value of tmdate2 is “04/23/2010″ and the value of SessionDateTime is “4/23/2010 8:37:31″.
> When I leave the time off, I get zero rows returned and I know the data is there.

That’s because when you leave off the time, it becomes midnight. So your comparison is actually
saying this:

WHERE   4/23/2010 8:37:31 = 04/23/2010 0:00:00

Obviously those don’t equal each other.  If you want to find all records _dated_ 4/23/2010, whatever
the time is, use a range comparison like I posted above.  It translates to

WHERE  SessionDate >= 04/23/2010
AND       SessionDate <   04/24/2010

So it finds all records with a _date_ of 4/23/2010 and ignores the time.


Viewing latest article 8
Browse Latest Browse All 10

Trending Articles