作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下查询
SELECT GETDATE()
返回的结果为 2013-11-16 03:31:07.740我只想要日期部分并省略时间部分。
我用过
select convert(varchar, getdate(), 100) convertResult,100 style union
select convert(varchar, getdate(), 101),101 union
select convert(varchar, getdate(), 102),102 union
select convert(varchar, getdate(), 103),103 union
select convert(varchar, getdate(), 104),104 union
select convert(varchar, getdate(), 105),105 union
select convert(varchar, getdate(), 106),106 union
select convert(varchar, getdate(), 107),107 union
select convert(varchar, getdate(), 108),108 union
select convert(varchar, getdate(), 109),109 union
select convert(varchar, getdate(), 110),110 union
select convert(varchar, getdate(), 111),111 union
select convert(varchar, getdate(), 112),112 union
select convert(varchar, getdate(), 113),113 union
select convert(varchar, getdate(), 114),114 union
select convert(varchar, getdate(), 120),120 union
select convert(varchar, getdate(), 121),121 union
select convert(varchar, getdate(), 126),126 union
select convert(varchar, getdate(), 127),127 union
select convert(varchar, getdate(), 130),130 union
select convert(varchar, getdate(), 131),131
order by 2
但不是任何组合都只给我日期部分。
最佳答案
最有效的方法是保持它是日期类型,而不是将其转换为字符串:
SELECT CONVERT(DATE, GETDATE());
如果您出于某种原因确实想要字符串输出:
SELECT CONVERT(CHAR(10), GETDATE(), 120);
(没有理由使用VARCHAR
,获取字符串特定长度的方法是指定字符串的长度。Please read this post。)
accepted and highly up-voted answer on the proposed duplicate - 我确信由于旅鼠因素,这最终会结束这个问题 - 并没有充分解决这个问题。
它使用的技术对于 SQL Server 2000 和 SQL Server 2005 来说是一种有效的方法,但对于 SQL Server 2008+ 则不然,因为上述转换比执行日期数学更有效(差异可能可以忽略不计) ,但这就是事情的原理)。
它仍然在输出中返回时间(只是在午夜而不是现在)。这里的操作不希望输出中包含时间(无论是实时时间还是午夜等)。
关于sql-server - 如何仅从日期时间列获取日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20011189/
我是一名优秀的程序员,十分优秀!