If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ASP > date Convertion

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-05-04, 00:24
michaelfg81 michaelfg81 is offline
Registered User
 
Join Date: Mar 2004
Posts: 202
date Convertion

Is this kind of coding correct?

SELECT *
FROM mainAim
WHERE CONVERT(varchar,Create_time, 120) = '2004-004-23 07:00:05'

I try to bring out the data related to the where clause but nothing showed up.
Is my where clause there got problem?
the data type for the field Create_time is datetime.
Please help.
Thanx
Reply With Quote
  #2 (permalink)  
Old 05-05-04, 00:46
MrWizard MrWizard is offline
Registered User
 
Join Date: Mar 2003
Location: Atlanta, GA
Posts: 191
Not too sure if this is your only issue, but I think CONVERT returns date data under the "120" style spec as yyyy-mm-dd hh:mi:ss

Your hardcoded date is in the form yyyy-mmm-dd hh:mi:ss (note that you have 3 digits for the month.

Hope this helps.

Tim
__________________
Tim
Reply With Quote
  #3 (permalink)  
Old 05-05-04, 01:16
michaelfg81 michaelfg81 is offline
Registered User
 
Join Date: Mar 2004
Posts: 202
Hehe.................
thanx..........................i'll go try and see.
Thanx again
Reply With Quote
  #4 (permalink)  
Old 05-05-04, 02:07
Seppuku Seppuku is offline
Useless...
 
Join Date: Jul 2003
Location: SoCal
Posts: 721
datetime datatype converts implicitly to nvarchar. You shouldn't need to CAST or CONVERT to compare a datetime field, and is probably going to cause more problems then it's worth. By comparing a string containing a date/time to a datetime field, the string is converted implicitly to datetime and compared (this allows you to do greater-than, greater-than-or-equal-to, less-than, etc comparisons). But if you convert the datetime field to a string, then your "=" is doing a string level comparison, not a datetime level comparison. So a string level comparison of "2004-03-05" is different than "3/5/2004". But a datetime level comparison "2004-03-05" is equivilant to "3/5/2004".
__________________
That which does not kill me postpones the inevitable.
Reply With Quote
  #5 (permalink)  
Old 05-05-04, 02:39
michaelfg81 michaelfg81 is offline
Registered User
 
Join Date: Mar 2004
Posts: 202
Thanx for your advice.
I really appreciate it.
Thanx again
Reply With Quote
  #6 (permalink)  
Old 05-05-04, 11:01
Seppuku Seppuku is offline
Useless...
 
Join Date: Jul 2003
Location: SoCal
Posts: 721
As a side note, the formats for CONVERT and CAST (which are functionally similar) are:

CAST ( expression AS data_type )
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )

So converting a datetime to an nvarchar might look like:
CAST( myDate AS nvarchar(30) )
or
CONVERT( nvarchar(30), myDate )

Or as MrWizard mentioned, you can add a style to CONVERT for datetime conversions:

CONVERT( nvarchar(30), myDate, 120 )
Style 120 = ODBC canonical: yyyy-mm-dd hh:mi:ss(24h)
__________________
That which does not kill me postpones the inevitable.
Reply With Quote
  #7 (permalink)  
Old 05-05-04, 19:47
michaelfg81 michaelfg81 is offline
Registered User
 
Join Date: Mar 2004
Posts: 202
Thanx Seppuku.
I appreciate it
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On