Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Converting to Datetime on Insert

Author  Topic 

DavidChel
Constraint Violating Yak Guru

474 Posts

Posted - 2009-02-18 : 12:51:53
I'm receiving a date from a program and want to change it to datetime for SQL 2000.

The following statement:

select CONVERT(datetime, 10 / 10 / 2009)

Gives the value : 1/1/1900 12:00:00 AM

However this works:

select CONVERT(datetime, ' 10 / 10 / 2009')

Why?

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-18 : 12:56:02
because passing as 10/10/2009 it actually does division of 10 by 10 and then by 2009 result of which is 0 which is equivalent to date value 01/01/1900 00:00:00 and converts it to datetime which is what you see.
in second case it interprets it as date value from varchar and gives you correct result
Go to Top of Page

DavidChel
Constraint Violating Yak Guru

474 Posts

Posted - 2009-02-18 : 14:12:55
Ah, thanks Visahk.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-02-19 : 03:38:36
welcome
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-02-19 : 04:06:35
quote:
Originally posted by DavidChel

I'm receiving a date from a program and want to change it to datetime for SQL 2000.

The following statement:

select CONVERT(datetime, 10 / 10 / 2009)

Gives the value : 1/1/1900 12:00:00 AM

However this works:

select CONVERT(datetime, ' 10 / 10 / 2009')

Why?




Becuase date values should always be expressed within single quotes

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -