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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Date str handled differently by SQL / Windows user

Author  Topic 

gavinharriss
Starting Member

17 Posts

Posted - 2010-03-03 : 22:59:34
Hi,

I've just encountered a problem that only occurs on our live server and not the dev server (both SQL Server 2008).

It seems that running T-SQL that converts a varchar to a datetime executes differently depending upon the user you've currently logged in as.

Logged in using the account created in SQL I get the following error:
The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.

But if logged in using a Windows account the sql executes without issue:
Command(s) completed successfully.

The following t-sql was used to test the issue:

DECLARE @TimespanTo datetime
SET @TimespanTo = CAST('2009 jul 31 23:00:00.000' AS DATETIME)

DECLARE @TimestampTo datetime;
SET @TimestampTo = CAST(
CAST(DATEPART(year, @TimespanTo) AS varchar(4))
+ '-' + CAST(DATEPART(month, @TimespanTo) AS varchar(2))
+ '-' + CAST(DATEPART(day, @TimespanTo) AS varchar(2))
+ ' 00:00:00'
AS datetime);


Anyone have any idea what is happening with the SQL account and how to resolve the issue?

Cheers,
Gavin

www.gavinharriss.com

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-04 : 01:51:13
Check the default language selected for SQL login, that may be different. Based on language interpretation of dates can vary and it can error like for eng uk its dd/mm/yyyy for eng us its mm/dd/yyyy etc
In any case passing dates in ccyy-mm-ddThh:mi:ss should work fine

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

gavinharriss
Starting Member

17 Posts

Posted - 2010-03-04 : 15:40:04
Thanks for that... indeed one account was "English" and the other "British English".

I always thought the format YYYY-MM-DD HH:MM:SS" was pretty universal. I guess I'll be revising that opinion now! :P

www.gavinharriss.com
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-05 : 10:23:32
quote:
Originally posted by gavinharriss

Thanks for that... indeed one account was "English" and the other "British English".

I always thought the format YYYY-MM-DD HH:MM:SS" was pretty universal. I guess I'll be revising that opinion now! :P

www.gavinharriss.com


YYYY-MM-DDTHH:MM:SS is universal

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

gavinharriss
Starting Member

17 Posts

Posted - 2010-03-05 : 15:48:57
That's what I thought. No so sure - even with the extra "T" between the date and time I was still getting an error :(

Perhaps my code was resulting in 2009-7-31T23:00:00 instead of 2009-07-31T23:00:00 and this was causing the issue. Will have a play another time.

Cheers,
Gavin

www.gavinharriss.com
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-06 : 01:36:50
ok.. let us know if you face any issues

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -