views:

403

answers:

4

I'm pretty frustrated here, been trying this for days... All I want is for German users to be able to enter dates that should be stored in SQL Server. The dates are in the following format "dd.mm.yyyy". I always get error messages like this one: Syntax error in date in query expression '#23.01.2004 00:07:00#'. - using Access for now.

I've tried lots of methods (Thread.CultureInfo, CDate(), Formatting DateTimeStyles) but here is my latest:

Dim dTermin As DateTime
dTermin = DateTime.Parse(txtTermin.Text)

sSQL = "INSERT INTO Termin (Termin) VALUES ("
sSQL = sSQL & "#" & dTermin & "#)"

That also gives me an error. If I hardcode a date like "10-04-2004" then it works, however. It's an Access 2000 db and there are two fields: ID (Autonumber) and Termin (ShortDate). Using Jet OLEDB 4.0 Provider.

+2  A: 

If I understand correctly, SQL Server datetime delimiter is ' eg '23.03.2009'

Then you have a 2nd problem, if SQL Server has the default us_enlish locale.

SET LANGUAGE us_english
GO
DECLARE @dt datetime
SET @dt = '23.03.2009' --error
GO
SET LANGUAGE german
GO
DECLARE @dt datetime
SET @dt = '23.03.2009' --ok
GO

Use yyyydddd or this

SET LANGUAGE us_english
SET DATEFORMAT DMY
GO
DECLARE @dt datetime
SET @dt = '23.03.2009' --ok
GO
gbn
+5  A: 

I suggest to insert non localized dates in the DB, and localize them only at display time.

AlberT
+2  A: 

I would use string dates in ISO format: YYYY/MM/DD Pretty much every system (and in every locale) unabiguously understands ISO format dates.

See Tibor Karaszi's excellent reference: The ultimate guide to the datetime datatypes

Mitch Wheat
yyyy/mm/dd is not ISO. See this where I showed SQL 2005 does not recognise ISO yyyymmdd too http://stackoverflow.com/questions/1138142/best-way-to-convert-and-validate-a-date-string/1138203#1138203
gbn
Agree with gbn: ISO 8601 is, even according to the linked article, YYYY-MM-DD.
OregonGhost
strictly speaking it's not ISO, but you know what I mean...
Mitch Wheat
SQL Server 2008 does recognise 'yyyymmdd' as well as 'yyyy/mm/dd'
Mitch Wheat
SQL Server 2008 also recognizes ISO 8601. It's a lot safer to use it, since it's, well, an ISO. There's just no point in using something else that some software happens to accept, when it also accepts the standard solution. It's true though that ISO 8601 also requires a time, which I omitted in my previous comment.
OregonGhost
+5  A: 

You should not be storing localized dates in the database. Save them in a standard format then let the ODBC connector or SQL client or whatever you are using - localize it according to the culture of whoever is viewing them.

rkarajan
that link is exactly what i needed! thanks so much!
pinku