views:

82

answers:

4

hey

i have the following query:

select * from table where table.DateUpdated >='2010-05-03 08:31:13:000'

all the rows in the table being queried have the following DateUpdated:

2010-05-03 08:04:50.000

it returns all of the rows in the table - even though it should return none.

I am pretty sure this is because of some crappy date/time regional thing.

if i swap the date to be

select * from table where table.DateUpdated >='2010-03-05 08:31:13:000'

then it does as it should.

How can i force everything to be using the same settings? this is doing my head in :)

This is sql generated by NHIbernate from my WCF service if that matters.

w://

A: 

You could try:

select * from table 
where Convert(DateTime, table.DateUpdated,103) >= Convert(DateTime, '2010-05-03 08:31:13:000',103)
Barry
hey - cheers - i'm looking at that now - problem is this is coming from nhibernate. I don't understand why it doesn't work as striaght sql.
cvista
also - as this is getting a delta from when the app was last updated - it needs to be done using time as well as date...
cvista
+3  A: 

Use this format "yyyymmdd hh:nn:ss.mmmm" which is locale independent in SQL Server, all versions

Somewhere, it's 5th Feb rather then 3rd May

Why:

  • "yyyy-mm-dd" is not locale independent in SQL Server with datetime columns
  • this anomaly is fixed with datetime2 in SQL Server 2008

References:

Example:

SET DATEFORMAT DMY  --UK
SELECT
    MONTH(CAST('2010-03-05 08:31:13:000' AS datetime)), --gives 5
    MONTH(CAST('20100305 08:31:13:000' AS datetime)) --gives 3


SET DATEFORMAT MDY  --default, USA
SELECT
    MONTH(CAST('2010-03-05 08:31:13:000' AS datetime)), --gives 3
    MONTH(CAST('20100305 08:31:13:000' AS datetime)) --gives 3
gbn
hmph - that does answer the sql part of the question but i have no idea how to use this in nhobernate :(
cvista
is there a locale or region setting, or can you format the date yourself?
gbn
seems there was the datetime2 type added to nhibernate to go with the MsSqlServer2008 dialect - going to try this...
cvista
A: 

Could you show us which technique you are using to have NH query the DB

ondesertverge
what do you mean?
cvista
Are you using CreateQuery(), CreateCriteria(), CreateSqlQuery(), NHibernate.Linq or some other method?
ondesertverge
I was wondering how you were passing the dates in. Have you tried setting the Thread.CurrentThread.CurrentCulture to the culture you are after. Could your app and sql server be on machines with different default cultures?
ondesertverge
on the same machine.
cvista
@ondesertverge: the SQL Server internal settings are independent of the OS
gbn
A: 

The answer to this was to upgrade to 2008 and use datetime2

what a PITA!!!

cvista