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
 Problerm On Casting Datetime

Author  Topic 

aoriju
Posting Yak Master

156 Posts

Posted - 2009-04-08 : 02:11:29
Dear All,

declare @strDt VARCHAR(20);
SET @strDt = '31/1/2009'; --dd/MM/YYYY

SELECT CAST(@strDt AS DATETIME) --Error Like out of range


In Procedure I want to cast that data into datetime type with MM/dd/YYYY format

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-08 : 02:22:49
[code]declare @strDt VARCHAR(20);
SET @strDt = '31/1/2009'; --dd/MM/YYYY

SELECT CONVERT(datetime, @strDt, 103) --Error Like out of range[/code]


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

aoriju
Posting Yak Master

156 Posts

Posted - 2009-04-08 : 02:28:45
Any Another Way
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-04-08 : 02:32:36
You are not satisfied with a suggestion that works?



E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-04-08 : 03:04:46
quote:
Originally posted by aoriju

Any Another Way


Express date in YYYYMMDD format

Madhivanan

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

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-04-10 : 06:44:52
quote:
Originally posted by aoriju

Dear All,

declare @strDt VARCHAR(20);
SET @strDt = '31/1/2009'; --dd/MM/YYYY

SELECT CAST(@strDt AS DATETIME) --Error Like out of range


In Procedure I want to cast that data into datetime type with MM/dd/YYYY format


why use varchar variable to datetime value? also if you want pass date values always try to use iso format as Madhi suggests.
Go to Top of Page
   

- Advertisement -