[英]How to convert datetime to date only (with time set to 00:00:00.000)
I have a string '2009-06-24 09:52:43.000', which I need to insert to a DateTime column of a table. 我有一个字符串'2009-06-24 09:52:43.000',我需要将其插入到表的DateTime列中。
But I don't care about the time, just want to insert it as 2009-06-24 00:00:00.000 但是我不关心时间,只想插入它作为2009-06-24 00:00:00.000
How can I do that in T-SQL? 我怎么能在T-SQL中做到这一点?
For SQL Server 2005 and below: 对于SQL Server 2005及更低版本:
CONVERT(varchar(8), @ParamDate, 112) -- Supported way
CAST(FLOOR(CAST(@ParamDate AS float)) AS DATETIME) -- Unsupported way
For SQL Server 2008 and above: 对于SQL Server 2008及更高版本:
CAST(@ParamDate AS DATE)
declare @originalDate datetime
select @originalDate = '2009-06-24 09:52:43.000'
declare @withoutTime datetime
select @withoutTime = dateadd(d, datediff(d, 0, @originalDate), 0)
select @withoutTime
SELECT CAST(CONVERT(VARCHAR,GETDATE(),102) AS DATETIME)
SELECT CAST(CONVERT(VARCHAR(10),'2009-06-24 09:52:43.000',102) AS DATETIME)
James is correct. 詹姆斯是对的。 If you're starting off with a string, and the format will always be what you say it is, then you keep it simple and efficient.
如果您从一个字符串开始,并且格式将始终是您所说的,那么您可以保持简单高效。 Use
LEFT( @StrDate, 10)
and CONVERT
that to your datetime value. 使用
LEFT( @StrDate, 10)
并将其CONVERT
为日期时间值。 Done. 完成。
If your input string could be any valid date/time format, then you have to use CONVERT(datetime, @StrDate)
first. 如果您的输入字符串可以是任何有效的日期/时间格式,那么您必须首先使用
CONVERT(datetime, @StrDate)
。 After that you go with what Bing just said to strip off the time part. 在那之后,你会选择Bing刚才所说的去掉时间部分。
An enhancement to the unsupported version: I am not sure if this may effect any performance. 对不受支持的版本的增强:我不确定这是否会影响任何性能。
getdate()
is an input timestamp in my query. getdate()
是我的查询中的输入时间戳。
select cast(cast(getdate() as DATE) as DATETIME)
cast it to a date, and then you can use CONVERT to get just the date. 将它转换为日期,然后您可以使用CONVERT来获取日期。
INSERT MyTable(Column1)
SELECT CONVERT(CHAR(8), CAST('2009-06-24 09:52:43.000' AS DATETIME), 112)
If you will always have the date in the same format, ie yyyy-MM-DD you can grab the first 10 characters if the value and insert that which is the equivelant of 00:00:00.0000 time for that date. 如果您将始终使用相同格式的日期,即yyyy-MM-DD,您可以获取前10个字符(如果值)并插入该日期00:00:00.0000的等效时间。
select left('2009-12-32 4:32:00',10)
This is a very efficient way to do this as it does't require converting data types HOWEVER, it does require that the date will always be formatted with a four digit year and two digit day & month. 这是一种非常有效的方法,因为它不需要转换数据类型,但它确实要求日期始终格式化为四位数年份和两位数日期和月份。
我发现铸造作为日期,然后到日期时间非常有效和直观。
SELECT CAST(CAST(GETDATE() as date) as datetime)
Probably a cleaner and more portable way to do this, but my years old idiom is: 可能是一个更干净,更便携的方式,但我多年的习语是:
insert into tbl (date_column)
select convert(varchar, convert (datetime, '2009-06-24 09:52:43.000'), 101)
A variety of hacks: 各种黑客:
convert
to convert your datetime to a string using just the date portion convert
为仅使用日期部分将日期时间转换为字符串 substring
to chop off the end substring
来切断结束 floor
floor
围绕日期时间 剥去时间,并将其投射到日期:
select cast(left(yourstring, 10) as datetime)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.