简体   繁体   中英

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. 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.

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.

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:

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

You can't stay in datetime2 format and truncate a part of it.
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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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