all groups > sql server programming > february 2007 >
You're in the

sql server programming

group:

Date Time format for Latvian Settings


Date Time format for Latvian Settings SM
2/28/2007 11:21:19 PM
sql server programming: I am trying to fire a query having the WHERE clause on Date Time field. But
with Latvian Regional settings the date value comes as 'yyyy.mm.dd.'. The
last dot (.) in the string creates problem. Does anyone knows how to tackle
Re: Date Time format for Latvian Settings Adi
2/28/2007 11:55:12 PM
[quoted text, click to view]

Why can't you make sure that the GUI that you are using will remove
the last dot?

Adi
Re: Date Time format for Latvian Settings Erland Sommarskog
3/1/2007 12:00:00 AM
SM (SM@discussions.microsoft.com) writes:
[quoted text, click to view]

I don't know you exact context, but there should rarely be any reason to
have date strings in queries at all. Use a parameterised query. Then the
conversion will happen client-side with knowledge of your regional settings,
and SQL Server will see a binary value.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: Date Time format for Latvian Settings Uri Dimant
3/1/2007 12:00:00 AM
SM
SQL Server "loves" YYYYMMDD format


CREATE TABLE #t (dt DATETIME)
GO
DECLARE @dt DATETIME
SET @dt=CONVERT(VARCHAR(30),REPLACE('2007.03.01. 11:20:20','.','') ,112)

INSERT INTO #t SELECT @dt


SELECT * FROM #t

DELETE FROM #t





[quoted text, click to view]

Re: Date Time format for Latvian Settings Erland Sommarskog
3/1/2007 12:00:00 AM
SM (SM@discussions.microsoft.com) writes:
[quoted text, click to view]

As I said, best practice is to pass dates as binary values, and you should
seriously reconsider your design. A generic middle layer that passes SQL
statements with embedded parameters values causes several problems, and
this is one of the smallest.

A more serious issue is performance: if you use parameterised queries, the
query plans can be reused. With embedded parameters values, you get a new
cache entry for little change in the query string. This can have a serious
impact on the performance on the system.

An even more seroius issue is security. Have you heard about SQL injection?
This is a technique whereby intruder can enter text that results in
commands being executed that you did not intend to. For instance instead
of a date, the entry reads:

2007 ' DROP TABLE important --

If this fits in syntactically, you have just lost a table if permissions
permit.

In essence, you have a broken design, and you need to repair that. I guess
you can address the particular problem with the date format by adding code
to convert the date to a format that is good for SQL Server, that is
YYYY-MM-DDThh:mm.ss. SQL Server does not look at regional settings, but
has its own setting for date formats. For instance try:

SET DATEFORMAT dmy
SELECT convert(datetime, '1999-12-23')

This will fail.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
Re: Date Time format for Latvian Settings SM
3/1/2007 12:36:12 AM
Thanks Adi,
But the problem is that after checking the date format for few more regional
settings I have found that this issue would arise with some more of them.
I.e. with regional settings as Bulgarian, I'd have to write code to remove
those other characters at the end of date value as well. Thats why I wanted
to have a generic solution. May be at the DB level.

[quoted text, click to view]
Re: Date Time format for Latvian Settings SM
3/1/2007 12:43:10 AM
Thanks Erland,
First of all sorry for posting incomplete or confusing query. Actually I am
passing parameter as string to the Middle layer in my application. Because
this layer is generic hence I cannot explicitly type cast the values. Hence I
take the value passed through UI as it is & then fire it at the DB level. Now
when I fire the mentioned query with date value as '2007.03.01. 11:20:20'
then SQL Server throws an error. The error is of type casting, stating
converion of varchar value to datetime resulted in an over flow. And it is
understandeable that the last dot (.) in the date value is confusing SQL
Server. But why SQL Server does not understand this date format? And even if
it does not, is there an alternate or a provision for achieving this?

[quoted text, click to view]
AddThis Social Bookmark Button