Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 月从日期返回不正确的值_Sql_Sql Server_Tsql_Datetime_Sql Server 2005 - Fatal编程技术网

Sql 月从日期返回不正确的值

Sql 月从日期返回不正确的值,sql,sql-server,tsql,datetime,sql-server-2005,Sql,Sql Server,Tsql,Datetime,Sql Server 2005,下午, 我有一个SQL server问题,需要一些帮助才能解决。 当我在中运行日期范围为的select语句时,它返回错误日期范围中的行 e、 g.如果我执行以下命令: select * from theTable where startDate between '2016-09-01 00:00:00' and '2016-09-08 23:59:59' 我预计startDate在9月1日至8日之间的行,我实际得到的是startDate在1月9日至8月9日之间的行 我已使用以下语言检查了数据库

下午,

我有一个SQL server问题,需要一些帮助才能解决。 当我在中运行日期范围为的select语句时,它返回错误日期范围中的行

e、 g.如果我执行以下命令:

select * from theTable where startDate between '2016-09-01 00:00:00' and '2016-09-08 23:59:59'
我预计
startDate
在9月1日至8日之间的行,我实际得到的是
startDate
在1月9日至8月9日之间的行

我已使用以下语言检查了数据库的设置语言:

select * from sys.syslanguages order by name where name = @@language
这返回英国

DBCC用户选项返回

textsize             2147483647
language             British
dateformat           dmy
datefirst            1
lock_timeout         -1
quoted_identifier    SET
arithabort           SET
ansi_null_dflt_on    SET
ansi_warnings        SET
ansi_padding         SET
ansi_nulls           SET
concat_null_yields_null  SET
isolation level          read committed
我已经检查了登录的默认语言,这也是英国语言

当我执行打印月份('2016-09-01 00:00:00')

它返回1而不是9。除非我弄错了,否则上面的日期和时间应该是ODBC规范的
yyyy-mm-dd hh:mi:ss(24小时)

为什么它打印的是1而不是9,更重要的是我如何修复它

打印
@@version
返回

Microsoft SQL Server 2005 - 9.00.5057.00 (X64) 
    Mar 25 2011 13:33:31 
    Copyright (c) 1988-2005 Microsoft Corporation
    Enterprise Edition (64-bit) on Windows NT 5.2 (Build 3790: Service Pack 2)
谢谢你的帮助。
如果这个问题已经得到了回答,你能告诉我答案吗?只是我找不到答案。

你似乎有一个相对不寻常的国际化设置,默认设置是YDM而不是YMD

始终可以使用不带连字符的日期:

select *
from theTable
where startDate >= '20160901' and
      startDate < '20160909';
选择*
从表格
其中startDate>='20160901'和
起始日期<'20160909';

在SQL Server中,这些始终被解释为YYYYMMDD。

如果您希望在评估where条件时也考虑时间戳,请尝试将日期转换为:

CONVERT(datetime,'2016-09-01 00:00:00',101)
因此,您将成为:

select * from theTable 
where startDate between 
CONVERT(datetime,'2016-09-01 00:00:00',101) 
and 
CONVERT(datetime,'2016-09-08 23:59:59',101)

这样,您的月份将是[9]而不是[1],时间戳也将被考虑。

使用明确的格式(
YYYYMMDD
):
其中startDate>='20160901'和startDate<'20160909'
。法语环境,选择一个常见的例子,将“2016-09-01 00:00:00”解释为2016年1月9日。