How can I get SQL Server to use User Locale for Date format for a Query?

六眼飞鱼酱① 提交于 2019-12-11 19:22:14

问题


I have created the following simple table for this test:

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'

All the queries except the last one execute. The problem I have is that my different users have different date formats, and some users date format is dd/mm/yyyy, not the standard US/Canada of mm/dd/yyyyy.

I know I can use the convert function to cast a date, but I am working with older queries and software that is simply using the date entered by the user. I want to know how to instruct SQL Server 2005+ to use the user's Short Date format in their locale.


回答1:


You can set the date format using SET DATEFORMAT XXX

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

http://msdn.microsoft.com/fr-fr/library/ms189491.aspx



来源:https://stackoverflow.com/questions/17491459/how-can-i-get-sql-server-to-use-user-locale-for-date-format-for-a-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!