Groups | Blog | Home
all groups > sql server clients > december 2004 >

sql server clients : Problems with Dateformat



Tomilee
12/19/2004 2:13:03 AM
Hi all

I have 2 MS SQL Servers (one in the English Language version, one in
German). On each server I have the same database (one for testing, one for
work).

When I'm queriing a datatable from vb.net I'm getting problems with a a
datetime parameter:

The English Language Version accepts: yyyy.MM.dd HH:mm
The German Language Version accepts: dd.MM.yyyy HH:mm

How can I change the DateFormat from the German Version to the English
Version, without reinstalling MS-SQL-Server in English and without changing
all my code to the two dateversions?

Any help would be very appreciated

Tom

Andrew J. Kelly
12/19/2004 9:27:58 AM
You might want to have a look at these:

http://www.karaszi.com/SQLServer/info_datetime.asp Guide to Datetimes
http://www.sqlservercentral.com/columnists/bsyverson/sqldatetime.asp
Datetimes
http://www.murach.com/books/sqls/article.htm Datetime Searching

--
Andrew J. Kelly SQL MVP


[quoted text, click to view]

Tomilee
12/27/2004 2:37:02 AM
Thanks Andrew for your links - unfortunately only the second works. I try to
explain my problem a bit more detailed:

I developed a vb.net client running on desktops. This client is running
queries on a MS SQL Server 2000. After starting the client it asks, witch
server the user wants to choose.

I’ve installed MS SQL servers 2000 twice:

One on my desktop in German.
..
One on a server with my client. This is an English version.

When I query from vb.net it works well on the English MS SQL Server 2000
with the following sample:

"select * from session where GuiAktiv > '" & _
Format(Now.AddSeconds(-60), "yyyy.MM.dd HH:mm") & "' order by id desc"


When I query from vb.net on the German MS SQL Server I receive the following
exception:

"Bei der Konvertierung eines char-Datentyps in einen datetime-Datentyp
liegt der datetime-Wert außerhalb des gültigen Bereiches „

Or in English (my translation):
During the conversion of a char-datatype in a datetime-datatype, the
datetime-value is outside the allowed values

What is going wrong?

Thanks in advance

Tom




[quoted text, click to view]
Erland Sommarskog
12/27/2004 10:55:48 PM
Tomilee (Tomilee@discussions.microsoft.com) writes:
[quoted text, click to view]

The link http://www.karaszi.com/SQLServer/info_datetime.asp works for me.

[quoted text, click to view]

This is where you go wrong. When you build your SQL statements, do always
use parameters, never build complete strings. What you should do is this:

cmd.CommandText = &_
"SELECT * FROM session WHERE GuiAktiv > @oneminuteago ORDER BY id DESC"
cmd.Parameters.Add("@oneminuteago", SqlDbType.DateTime)
cmd.Parameters(0).Value = Now.AddSeconds(-60))

In this particular case, this has the effect that the date is passed as a
binary value to SQL Server, and there can never be any misunderstanding
about the format.

The above is even more important if your data comes from user input. If
you build strings, a malicious user could enter data with a single quote
in it which would close the string. Then the user can add a completely
other SQL statement that does evil things. Don't laugh. This is called
SQL injection, and is a commonly used by intruders.

What happens now is that you pass a formatted date to SQL Server. Most
date formats in SQL Server are interpreted according to dateformat and
language settings. YYYY-MM-DD does not work with a German dmy setting.
If you are absolute dead set on using strings, you should use any of the
formats:

YYYYMMDD [HH:MM:SS.mmm]
YYYY-MM-DDTHH:MM:DD[.mmm]

T here stands for itself. Brackets show optional components. These are
the only two format that SQL Server will always understand.
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server SP3 at
Tomilee
12/28/2004 7:31:04 AM
Thank you for this useful information!

Tom

[quoted text, click to view]
RADl0PASlV
12/28/2004 9:03:51 AM
[quoted text, click to view]

Only note:

format ISO 8601 YYYY-MM-DDTHH:MM:DD[.mmm] isn't supported on MSSQL7.

AddThis Social Bookmark Button