Omitting the Milliseconds in a Date(省略日期中的毫秒数)
问题描述
当我从 SQL Server 中选择时,我想获取一个日期,但省略了毫秒值,我希望它作为日期类型.因此,如果我有一个值 1/1/2009 1:23:11.923,我想省略毫秒但保留日期类型,以便它是值 1/1/2009 1:23:11.000(我知道你真的不能省略日期的毫秒值,只希望它为零).
When I select from SQL Server, I want to get a date, but omit the millisecond value, and I want it to be as a date type. So if I have a value 1/1/2009 1:23:11.923, I want to omit the millisecond but retain the date type, so that it will be the value 1/1/2009 1:23:11.000 (I know you really can't omit the millisecond value with a date, just want it to be zero).
SQL Server 中是否有函数可以执行此操作?还是我必须编写自己的函数?同样,我不希望它是 varchar 类型,而是 datetime 类型.
Is there a function in SQL Server to do this? Or do I have to write my own function? Again, I don't want it as a varchar type, but a datetime type.
推荐答案
如果你不想使用字符串转换,这里有一个解决方案:
If you don't want to use string conversions, here's a solution:
DECLARE @TheDate datetime, @Today datetime
SET @TheDate = GetDate()
SET @Today = DateAdd(dd, DateDiff(dd, 0, @TheDate), 0)
SELECT DateAdd(s, DateDiff(s, @Today, @TheDate), @Today)
这篇关于省略日期中的毫秒数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:省略日期中的毫秒数
基础教程推荐
- 无法解决整理冲突 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- 是否可以执行按位分组功能? 2021-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
