SQL Server 日期时间问题.美国VS英国?
在我的测试数据库中,日期以 DD/MM/YYYY 格式显示.显示是指右键单击,在Management Studio中打开表格,返回的数据以DD/MM/YYYY格式显示.
On my test DB, the dates are displayed in a DD/MM/YYYY format. By displayed I mean when you right click, open table in Management Studio, the returned data are displayed in a DD/MM/YYYY format.
有趣的是,当我编写 T-SQL 来检索记录时,我必须输入 MM/DD/YYYY 格式才能取回正确的数据.无论如何我可以将其与 DD/MM/YYYY 格式对齐吗?
Funny thing is, when I write T-SQL to retrieve records, I have to input a MM/DD/YYYY format to get back the right data. Is there anyway I can align this to a DD/MM/YYYY format?
推荐答案
您可以使用 SET LANGUAGE 以选择 SQL Server 期望 在查询中 的日期格式(我认为管理工作室使用客户端计算机的区域设置用于显示目的,但不确定).但是,我建议使用参数传递值而不是将它们嵌入到查询语句中.如果您使用参数,您将不会遇到任何问题.一切都得到了照顾.
You can use SET LANGUAGE to choose the date format that SQL Server expects in queries (I think management studio uses client computer's regional settings for display purposes, not sure though). However, I suggest passing values using parameters instead of embedding them in query statement. You won't encounter any issues if you use parameters. Everything is taken care of.
set language us_english
declare @d datetime = '1929/12/18'
set language british
declare @d datetime = '1929/12/18' -- fails
更改服务器默认语言:
declare @langid int = (select langid from syslanguages where name = 'british')
exec sp_configure 'default language', @langid
reconfigure with override
相关文章