December 28, 2011 at 8:12 am
declare @tbl table(stdate datetime)
insert @tbl select '12-31-2011'
declare @date datetime
set @date = '2011-31-12'
select * from @tbl where stdate = @date
Giving the following error
Msg 242, Level 16, State 3, Line 6
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
So I need to convert the variable format to sql format. Please help me out of this.
December 28, 2011 at 8:19 am
Maybe always set the Dateformat explicitly:
SET DATEFORMAT MDY
declare @tbl table(stdate datetime)
insert @tbl select '12-31-2011'
SET DATEFORMAT YDM
declare @date datetime
set @date = '2011-31-12'
select * from @tbl where stdate = @date
December 28, 2011 at 8:24 am
Or use the universal format yyyymmdd:
SET @date = '20111231'
This will work no matter what your local settings are.
John
December 29, 2011 at 12:59 am
The solution u gave will work fine, but how to know the current date format in sql, so that i can give that dateformat as
SET DATEFORMAT Current Date Format In Sql
December 29, 2011 at 3:06 am
http://www.sql-server-helper.com/tips/date-formats.aspx
This link may help u
December 29, 2011 at 6:07 am
mandirkumar (12/29/2011)
The solution u gave will work fine, but how to know the current date format in sql, so that i can give that dateformat asSET DATEFORMAT Current Date Format In Sql
You don't need to know it. The command will override the default behavior for the session.
SET DATEFORMAT YDM --'2011-31-12'
SET DATEFORMAT (Transact-SQL)
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply