简体   繁体   English

如何仅将日期时间转换为日期(时间设置为00:00:00.000)

[英]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 your string to a datetime, then back again using the optional "style" parameter to convert to convert your datetime to a string using just the date portion 将您的字符串转换为日期时间,然后使用可选的“style”参数再次convert为仅使用日期部分将日期时间转换为字符串
  • use substring to chop off the end 使用substring来切断结束
  • round the datetime using floor 使用floor围绕日期时间

剥去时间,并将其投射到日期:

select cast(left(yourstring, 10) as datetime)

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

相关问题 SQL Server:DateTime列,时间00:00:00.000 - SQL Server : DateTime column, time 00:00:00.000 如何将14年10月15日varchar转换为日期时间格式2014-10-23 00:00:00.000 - How to convert 15-Oct-14 varchar to datetime format 2014-10-23 00:00:00.000 将日期中的时间转换为 00:00:00 - Convert time in date as 00:00:00 如何将 SQLite 中的 0:00:00 转换为 00:00:00 时间格式? - How to convert 0:00:00 to 00:00:00 time format in SQLite? 按日期搜索Gridview,但日历控件选择器(即DateTime)同时包含日期和时间,例如:2013-01-01 00:00:00.000 - Searching Gridview by Date, but the Calendar control picker (that is DateTime) includes the date and also the time, example: 2013-01-01 00:00:00.000 SQL 日期时间需要读取 00:00:00.000 - SQL datetime needs to read 00:00:00.000 是否可以在datetime列中仅添加Date而不能附加到00:00.000 - Is it possible to add just Date in a datetime column and not get appended 00:00.000 将DateTime时间从00:00:00转换为23:59:59 - Convert DateTime Time from 00:00:00 to 23:59:59 TSQL:如何将2013年10月17日星期四00:00:00 GMT转换为日期或日期时间 - TSQL: How to convert Thu Oct 17 00:00:00 GMT 2013 to date or datetime 如何在 Presto SQL(AWS Athena)“2021-05-03T00: 00: 00.000Z”格式中将时间戳转换为日期? - How to convert timestamp to date in Presto SQL (AWS Athena), "2021-05-03T00: 00: 00.000Z" format?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM