更改数据库中存储的默认日期格式
我看到我的日期以这种格式存储在数据库中的列(日期时间数据类型) 2011-01-14 10:15:41.787 即 YYYY-MM-DD 方式。如何将默认存储设置为 YYYY-DD-MM 格式。我是否需要为所有 DBS 设置它,或者我可以为单个 DB 设置它,如何设置?
I am seeeing my dates are stored in database in this format for a column (datetime datatype) 2011-01-14 10:15:41.787 i.e YYYY-MM-DD way . How could I make the default storage in YYYY-DD-MM format . Do I need to set that for all the DBS, or I can set it for single DB and how ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这就是混乱的症结所在。仅仅因为 SQL Server Management Studio 显示该格式的日期时间列并不意味着它存储为 TEXT YYYY-MM-DD hh:mm:ss.zzz。它以二进制形式存储,类似于 0000101000001010..
您的日期以一系列字节(实际上是位)的形式存储在 SQL Server 中,这些字节组成了一些数值,该数值是距 1900-01-01 的偏移量。日期没有固有的格式。您指的是SSMS默认将[显示]日期时间列显示为YYYY-MM-DD hh:mm:ss.zzz。如果您使用前端编程工具,它也可能会强制使用默认的[显示]格式,除非您要求使用另一种格式。
绝对没有办法通过选项或配置使 SSMS 以另一种格式显示日期时间数据。如果必须,则必须更新 SQL 查询以将日期时间列转换为包含特定格式的 TEXTual 等效项的 VARCHAR 列。这在 SSMS 中可能很有用,但当用作前端 GUI/Web 应用程序的数据源时会很糟糕 - 因为这些值不是日期时间,不能用于间隔计算、绘图、绑定到日期控件等。
请参阅此将时间 (getdate()) 显示为 YYYY-DD-MM 的示例,这是一种非常不寻常的格式。请注意,日期字段/变量必须使用两次:
That is the crux of the confusion. Just because SQL Server Management Studio displays a datetime column in that format does not mean that it is stored AS TEXT YYYY-MM-DD hh:mm:ss.zzz. It is stored as binary, something like 0000101000001010..
Your dates are stored in SQL Server as a series of bytes (bits really) that make up some numeric value that is an offset from 1900-01-01. There is no inherent format the the dates. What you are referring to is that SSMS by default shows [display] datetime columns as YYYY-MM-DD hh:mm:ss.zzz. If you use a front-end programming tool, that too may impose a default [display] format unless you have asked for another one.
There is absolutely NO way to make SSMS show datetime data in another format through options or configuration. If you must, you would have to update the SQL query to convert the datetime column to a VARCHAR column containing the TEXTual equivalent in a particular format. That may be useful in SSMS, but would be bad when used as a data source to front-end GUI/web apps - since the values are not datetime and cannot be used for interval calculation, graphing, bound to date controls etc.
See this example of displaying time (getdate()) as YYYY-DD-MM, a very unusual format. Notice the date field/variable has to be used twice:
DATETIME 在内部存储为两个 4 字节整数,因此首先您会看到 UI 的格式化表示形式 - 它实际上并未以特定的日期/时间格式存储。
例如,如果您只插入像“2010-01-01”这样的日期,那么它仍然会保留时间元素:2010-01-01 00:00:00.000
如果您只对 DATE 部分感兴趣,那么您可以格式化DATETIME 用于在前端代码中或通过查询输出:
例如
,因此即使您插入的日期包含时间,返回时也会被忽略。您还可以确保只插入没有指定时间的日期 - 您需要在执行插入的任何代码中处理该问题。例如,在 .NET 中,您可以传入
DateTime.Now.Date,而不是传入
DateTime.Now
。在SQL Server 2008中,有一个DATE数据类型,它只存储DATE(没有时间),这正是您在这种情况下想要的。
DATETIMEs are stored internally as two 4 byte integers, so firstly you are seeing a formatted representation for the UI - it's not actually stored in a particular date/time format as such.
e.g. if you insert just a date like "2010-01-01" then it will still hold the time element: 2010-01-01 00:00:00.000
If you're only interested in the DATE part, then you can format the DATETIME for output either in your front-end code or via your query:
e.g.
So even if the DATEs you insert contain a time, that will be ignored when returned. You could also ensure you only insert dates without the time specified - you need to handle that in whatever code is doing the INSERTs. e.g. from .NET, instead of passing in
DateTime.Now
you could pass inDateTime.Now.Date
.In SQL Server 2008, there is a DATE datatype which is there to only store a DATE (without time) which is really what you want in this kind of scenario.