简体   繁体   English

SQL Server Datetime2:消除datetime2中的秒部分

[英]Sql Server Datetime2: Eliminate the seconds part in datetime2

How can I eliminate the seconds part in my query. 如何消除查询中的秒数部分。 I have used DateADD to show the seconds part as 0 but I just want to ignore it. 我使用DateADD将秒部分显示为0,但我只想忽略它。 I only want to show the date hours: minutes I have used this query and it returns me something like this 我只想显示日期小时:分钟,我已经使用了此查询,它返回的内容是这样的

SELECT DATEADD(minute, DATEDIFF(minute,0,GETDATE()), 0)

2013-09-30 13:03:00.000

But I want only 2013-09-30 13:03 part. 但是我只想要2013-09-30 13:03一部分。

Can anyone help me? 谁能帮我?

If you're dealing with a format, then what you have to end up with is a string , not a datetime value - datetime s (or datetime2 s) don't have a format. 如果你正在处理的格式,那么你有落得什么是字符串 ,而不是datetime值- datetime S(或datetime2为s) 没有一个格式。

So you need to convert to a string. 因此,您需要转换为字符串。 There's no format that exactly matches what you're asking for, but if you do a conversion and don't give CONVERT enough space, it truncates the result: 没有完全符合您要求的格式 ,但是如果您进行转换并且没有给CONVERT足够的空间,则会截断结果:

SELECT CONVERT(varchar(16),GETDATE(),120)

You can't stay in datetime2 format and truncate a part of it. 您不能保持datetime2格式并截断其中的一部分。
What you can try one of the following: 您可以尝试以下操作之一:

SELECT CAST(GETDATE() as date)
SELECT CONVERT(varchar(16),GETDATE(),120)

If this still doesn't give you exactly what you want, you can see many other possible conversions in this post . 如果仍然不能满足您的需求,您可以在本文中看到许多其他可能的转换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM