December 17, 2009 at 6:24 pm
Hi...
I got a problem with comparing date on WHERE part of my query even the date is in exact format...
Table Name : cash_up
Field: date_out
date_out value: '2009-12-17 10:45:52.5900000'
Note: CAST('2009-12-17 10:45:52.5900000' as datetime) = '12/17/2009 10:45:52 AM'
My Query:
SELECT
CAST(date_out as datetime) as date_out
FROM cash_up
WHERE
CAST(date_out as datetime) = '12/17/2009 10:45:52 AM'
I got no result.....
somebody know how can i fix this.... please ...
many thanks for any reply.....
December 17, 2009 at 8:08 pm
Please read the Best practice article below and post some test data, since that provides us to see what error you are getting. It enables us to test a readily available code instead of we building it from scratch.
http://www.sqlservercentral.com/articles/Best+Practices/61537/[/url]
It makes the responses come quicker.
Blog -- LearnSQLWithBru
Join on Facebook Page Facebook.comLearnSQLWithBru
Twitter -- BruMedishetty
December 18, 2009 at 7:37 am
What data type is date_out? Can you post some sample data from it?
BTW, running that function on your column in the WHERE clause will prevent indexes from being used.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
December 18, 2009 at 8:34 am
I just tried running your query on one of my own tables that has dates, and didn't have a problem.
BTW, Grant is correct . . . you should avoid including functions (such as CAST) in your WHERE clause.
+--------------------------------------------------------------------------------------+
Check out my blog at https://pianorayk.wordpress.com/
December 18, 2009 at 4:07 pm
I can't even get the value, '2009-12-17 10:45:52.5900000', to CAST to datetime, I get this error:
Msg 241, Level 16, State 1, Line 12
Conversion failed when converting date and/or time from character string.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
December 18, 2009 at 4:46 pm
Jack Corbett (12/18/2009)
I can't even get the value, '2009-12-17 10:45:52.5900000', to CAST to datetime, I get this error:Msg 241, Level 16, State 1, Line 12
Conversion failed when converting date and/or time from character string.
Should probably work with new DATETIME2 datatype, Jack.
--Jeff Moden
Change is inevitable... Change for the better is not.
December 18, 2009 at 5:22 pm
Jeff,
The OP specifically casts to datetime in the code provided so that's what I ran.
Jack Corbett
Consultant - Straight Path Solutions
Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply