你如何从 T-Sql 中的日期时间中提取日期? [英] How do you extract just date from datetime in T-Sql?

查看:33
本文介绍了你如何从 T-Sql 中的日期时间中提取日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对 SQL Server 2005 中的日期时间列运行选择.我只能从此日期时间列中选择日期?

I am running a select against a datetime column in SQL Server 2005. I can select only the date from this datetime column?

推荐答案

最好的方法是:

   SELECT DATEADD(day, DATEDIFF(Day, 0, @ADate), 0) 

这是因为在内部,SQL Server 将所有日期存储为两个整数,其中第一个是 **** 1900 年 1 月 1 日以来的天数***.(第二个是时间部分,存储为自午夜以来的秒数.(SmallDateTimes 为秒,DateTimes 为毫秒)
使用上面的表达式更好,因为它避免了所有转换,直接读取和访问日期内部表示中的第一个整数而无需执行任何处理......上面表达式中的两个零(代表 1 Jan 1900),也是直接使用而无需处理或转换,因为它们与 1900 年 1 月 1 日的 SQL 服务器内部表示完全匹配(作为整数).

This is because internally, SQL Server stores all dates as two integers, of which the first one is the ****number of days*** since 1 Jan 1900. (the second one is the time portion, stored as the number of seconds since Midnight. (seconds for SmallDateTimes, or milleseconds for DateTimes)
Using the above expression is better because it avoids all conversions, directly reading and accessing that first integer in a dates internal representation without having to perform any processing... the two zeroes in the above expression (which represent 1 Jan 1900), are also directly utilized w/o processing or conversion, because they match the SQL server internal representation of the date 1 jan 1900 exactly as presented (as an integer)..

*注意.实际上,从一个日期到另一个日期必须跨越的日期边界(午夜)的数量.

*NOTE. Actually, the number of date boundaries (midnights) you have to cross to get from the one date to the other.

这篇关于你如何从 T-Sql 中的日期时间中提取日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆