在SQL中截断秒和毫秒 [英] Truncate seconds and milliseconds in SQL
问题描述
我应该注意我正在使用MS-SQL。
有很多方法可以做到这一点。
例如,您可以将生成的 datetime
从 GetDate()
到 smalldatetime
第一,àla:
CAST(GetDate()AS smalldatetime)
要清楚的是,这将根据当前秒数的值将生成的秒数(或向下)舍入到最接近的分钟。
编辑:
或者,您可以让SQL Server截断 datetime
为更清洁(READ:不舍入,因为值被截断)转换为 smalldatetime
:
CAST(DateAdd(分钟,DateDiff(分钟,0,GetDate()),0)AS smalldatetime)
/ pre>
I'm having a bit of trouble with truncating data. I'm using SQL's GETDATE() function to get the current date and time and enter them into a database. However, I only want to save the date and time up until the minute. In other words, I want dd/mm/yyyy hh:mm:00.000 or dd/mm/yyyy hh:mm to be saved when I input new data. How can I go about doing this?
I should note I'm using MS-SQL.
解决方案There are a number of ways to go about doing this.
For example, you could convert the generated
datetime
fromGetDate()
to asmalldatetime
first, à la:CAST(GetDate() AS smalldatetime)
To be clear, this will round the generated seconds up (or down) to the nearest minute depending up the value of the current second.
EDIT:
Alternatively, you can have SQL Server truncate a
datetime
for you for a "cleaner" (READ: no rounding, since the value is pre-truncated) conversion tosmalldatetime
:CAST(DateAdd(minute, DateDiff(minute, 0, GetDate()), 0) AS smalldatetime)
这篇关于在SQL中截断秒和毫秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!