简体   繁体   中英

DateTime and culture

SQL:

select * 
from tvideoconference 
where del = 'false' 
and iduserpatient = 0 and startdate >= N'18.02.2013 20:37:07'  
order by startdate 

Error:

The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.

I got this error, when I tried to use method below in de-DE session culture. What is more, in pl-PL and en-US culture, this methods works perfect.

public static DataSet getSpecialistConfs(int iduserspecialist)
{
    DateTime? today = DateTime.Now;
    today = today.Value.AddHours(-today.Value.Hour).AddMinutes(-(today.Value.Minute + 1));
    string sql = "select * from tvideoconference where del='false' and startdate >=N'" + today.Value + "' and iduserspecialist=" + iduserspecialist;
    sql += " order by startdate ";

    return Tools.SQLTools.getDataSet(sql);
}

How can I resolve it? I tried with many solutions (substrings, date format), with the same effect..

How can I resolve it?

Use parameterized SQL in the first place. Don't include the values in your query itself; use parameters and set the values of those parameters.

It's unclear what Tools.SqlTools is, but you really, really should use parameterized SQL. That way you won't be converting the DateTime value into a string to start with, so cultures and formatting can't get in the way. Additionally, you won't be vulnerable to SQL injection attacks ...

(Additionally, it's not at all clear why you're using DateTime? instead of DateTime - it's not like DateTime.Now can be null... and you should consider using DateTime.Today instead... or DateTime.UtcNow.Date .)

The various settings (language, date format) only influence how the DateTime is shown to you in SQL Server Management Studio - or how it is parsed when you attempt to convert a string to a DateTime .

There are many formats supported by SQL Server - see the MSDN Books Online on CAST and CONVERT . Most of those formats are dependent on what settings you have - therefore, these settings might work some times - and sometimes not.

The way to solve this is to use the (slightly adapted) ISO-8601 date format that is supported by SQL Server - this format works always - regardless of your SQL Server language and date format settings.

The ISO-8601 format is supported by SQL Server comes in two flavors:

  • YYYYMMDD for just dates (no time portion); note here: no dashes! , that's very important! YYYY-MM-DD is NOT independent of the dateformat settings in your SQL Server and will NOT work in all situations!

or:

  • YYYY-MM-DDTHH:MM:SS for dates and times - note here: this format has dashes (but they can be omitted), and a fixed T as delimiter between the date and time portion of your DATETIME .

This is valid for SQL Server 2000 and newer.

If you use SQL Server 2008 or newer and the DATE datatype (only DATE - not DATETIME !), then you can indeed also use the YYYY-MM-DD format and that will work, too, with any settings in your SQL Server.

Don't ask me why this whole topic is so tricky and somewhat confusing - that's just the way it is. But with the YYYYMMDD format, you should be fine for any version of SQL Server and for any language and dateformat setting in your SQL Server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM