SQL Server 日期时间问题。 美国与英国?

发布于 2024-07-29 08:13:06 字数 199 浏览 5 评论 0原文

在我的测试数据库上,日期以 DD/MM/YYYY 格式显示。 我所说的显示是指当您右键单击并在 Management Studio 中打开表时,返回的数据以 DD/MM/YYYY 格式显示。

有趣的是,当我编写 T-SQL 来检索记录时,我必须输入 MM/DD/YYYY 格式才能获取正确的数据。 无论如何,我可以将其与 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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

酒绊 2024-08-05 08:13:06

您可以使用设置语言来选择 SQL Server 预期在查询中(我认为 Management Studio 使用客户端计算机的区域设置进行显示,但不确定)。 但是,我建议使用参数传递值,而不是将它们嵌入到查询语句中。 如果使用参数,则不会遇到任何问题。 一切都得到照顾。

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

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

To change the server default language:

declare @langid int = (select langid from syslanguages where name = 'british')
exec sp_configure 'default language', @langid
reconfigure with override
最舍不得你 2024-08-05 08:13:06

就我个人而言,我总是使用 YYYY-MM-DD 格式(或 YYYYMMDD),因为它不是特定于文化的,而且,我想它对我很有吸引力,因为它是“合乎逻辑的”(特别是当后面跟着一个时间)。

[编辑:我只是在谈论我在 SQL 脚本中放入的内容以确保兼容性,而不考虑服务器设置,而不是 SQL Server“显示”的内容]

Personally, I always use YYYY-MM-DD format (or YYYYMMDD) since it's not culture-specific, and, well, I guess it appeals to me because it's "logical" (especially when followed by a time).

[Edit: I'm just talking about what I put in my SQL scripts to ensure compatibility regardless of the server settings, not what SQL Server "displays"]

霞映澄塘 2024-08-05 08:13:06

您可以为每个SQL Server 登录设置默认语言。 不太记得了,但大概是这样的:

sp_defaultlanguage @loginame = 'LoginName', @language = 'Language'

You can set the default language for each indvidual SQL Server login. Can't quite remember, but something like this:

sp_defaultlanguage @loginame = 'LoginName', @language = 'Language'
书信已泛黄 2024-08-05 08:13:06

如果您以 DATETIME 格式传递,

dd MMM yyyy

例如,

"11 JUL 2009"

则月份和日期永远不会有任何歧义,因此您永远不会遇到问题

If you pass in DATETIME in the format

dd MMM yyyy

for example

"11 JUL 2009"

there is never any ambiguity around month and date and therefore you should never have a problem

丶视觉 2024-08-05 08:13:06

在几乎所有情况下,解决此问题的正确方法就是永远不要将日期视为字符串。 如果您传入参数或使用(类型化)列值,则服务器的文本转换根本就不是一个因素。 除了避免 i18n 问题之外,这还可以减少注入攻击面。 而且它也节省了一些 CPU 周期;-p

如果您使用 EXEC 来执行动态 SQL,那么这同样应该通过 sp_ExecuteSQL 进行参数化。

In almost all cases, the correct way to solve this is simply to never treat the date as a string. If you pass in a parameter, or use the (typed) column value, then the server's text conversion simply isn't a factor. In addition to avoiding the i18n issue, this also reduces your injection attack surface. And it saves a few CPU cycles, too ;-p

If you are using EXEC for dynamic SQL, then this should likewise be parameterised via sp_ExecuteSQL.

梦断已成空 2024-08-05 08:13:06

我尝试尽可能使用 ODBC 规范形式的日期
{d 'yyyy-mm-dd'}
这样我就知道sql server将如何解释它。
它在 TSQL 中工作得很好。

I try to use the ODBC canonical form of a date wherever possible
{d 'yyyy-mm-dd'}
This way I know how sql server will interpret it.
It works in TSQL just fine.

护你周全 2024-08-05 08:13:06

将其添加到您的 web.config 文件中:

</system.web>
    <globalization culture="en-US" uiCulture="en-US" />
</system.web>

或者您可以在页面上添加此语句:

<%@ Page uiCulture="en-US" culture="en-US" %>

希望这有帮助。

Either add this to your web.config file:

</system.web>
    <globalization culture="en-US" uiCulture="en-US" />
</system.web>

or you can add this statement on the page:

<%@ Page uiCulture="en-US" culture="en-US" %>

Hope this help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文