Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.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 server 如何让SQL Server使用用户区域设置作为查询的日期格式?_Sql Server_Database_Date_Casting - Fatal编程技术网

Sql server 如何让SQL Server使用用户区域设置作为查询的日期格式?

Sql server 如何让SQL Server使用用户区域设置作为查询的日期格式?,sql-server,database,date,casting,Sql Server,Database,Date,Casting,我已为此测试创建了以下简单表格: DROP TABLE DateFieldTest CREATE TABLE DateFieldTest ( DateField Date NOT NULL ) INSERT INTO DateFieldTest VALUES ('2013-07-15') INSERT INTO DateFieldTest VALUES ('2013-07-16') INSERT INTO DateFieldTest VALUES ('2013-07-17') INS

我已为此测试创建了以下简单表格:

DROP TABLE DateFieldTest
CREATE TABLE DateFieldTest
(
    DateField Date NOT NULL
)

INSERT INTO DateFieldTest VALUES ('2013-07-15')
INSERT INTO DateFieldTest VALUES ('2013-07-16')
INSERT INTO DateFieldTest VALUES ('2013-07-17')

INSERT INTO DateFieldTest VALUES ('2013-07-05')
INSERT INTO DateFieldTest VALUES ('2013-07-06')
INSERT INTO DateFieldTest VALUES ('2013-07-07')

INSERT INTO DateFieldTest VALUES ('2013-06-05')
INSERT INTO DateFieldTest VALUES ('2013-06-06')
INSERT INTO DateFieldTest VALUES ('2013-06-07')

INSERT INTO DateFieldTest VALUES ('2013-05-05')
INSERT INTO DateFieldTest VALUES ('2013-05-06')
INSERT INTO DateFieldTest VALUES ('2013-05-07')

SELECT * from DateFieldTest where DateField >= '2013-07-05'
SELECT * from DateFieldTest where DateField >= '2013-07-06'
SELECT * from DateFieldTest where DateField >= '2013-07-07'

SELECT * from DateFieldTest where DateField >= '05/07/2013'
SELECT * from DateFieldTest where DateField >= '06/07/2013'
SELECT * from DateFieldTest where DateField >= '07/07/2013'

SELECT * from DateFieldTest where DateField >= '07/05/2013'
SELECT * from DateFieldTest where DateField >= '07/06/2013'
SELECT * from DateFieldTest where DateField >= '07/07/2013'

SELECT * from DateFieldTest where DateField >= '24/04/2013'
执行除最后一个查询之外的所有查询。我的问题是,我的不同用户有不同的日期格式,一些用户的日期格式是dd/mm/yyyy,而不是标准的美国/加拿大的mm/dd/yyyyy


我知道我可以使用convert函数来转换日期,但我使用的是较旧的查询和软件,它们只是使用用户输入的日期。我想知道如何指示SQL Server 2005+在其区域设置中使用用户的短日期格式。

您可以使用
set DATEFORMAT XXX

SET DATEFORMAT DMY
SELECT * from DateFieldTest where DateField >= '24/04/2013'

只是一个想法:您可以通过提取所需字段(日、月和年),创建自己的函数,从客户端接收日期,并使用服务器的区域设置创建一个新函数。我认为这是不可能的。客户端软件需要更改以在参数而不是文本字符串中传递日期值,这样区域设置就不会成为问题。格式化日期是应用程序的责任。SQL Server无法比用户的年龄或性别更了解用户的日期偏好。特定的应用程序,如SSM,可能会出现问题,如果您询问这些问题,我们可能会帮助您。