转换到日期时间,只有日期和小时通过

时间:2022-08-25 10:56:44

I am using SQL server, i want to insert datetime into my datetime column. The parameters i recieved as follows

我正在使用SQL server,我想将datetime插入到datetime列中。我收到的参数如下

@hour int,
@date varchar(10) -- format YYYY-MM-DD

How do I convert to this datetime format:

如何转换为这个datetime格式:

YYYY-MM-DD HH:MM:SS

MM will be 00 as we only consider the hour.
HH:MM will be in 24 hours format

MM是00,因为我们只考虑时间。HH:MM将采用24小时格式

Example :

例子:

@date = '2014-10-2'
@hour = '8'

Should be converted to

应该转换为

2014-10-2 08:00:00

How do I do this?

我该怎么做呢?

2 个解决方案

#1


4  

I consider that both the variables are of varchar type since you enclosed both the variables with quotes

我认为这两个变量都是varchar类型的,因为您用引号括住了这两个变量

  SELECT CONVERT(DATETIME, @date + ' ' + convert(varchar(10),@hour) + ':00:00') 

#2


0  

here is one another way to do this:-

这是另一种方法:-

declare @hour int=8,
@date varchar(10)='2014-10-2'
select dateadd(hh,@hour,CONVERT(datetime,@date))

#1


4  

I consider that both the variables are of varchar type since you enclosed both the variables with quotes

我认为这两个变量都是varchar类型的,因为您用引号括住了这两个变量

  SELECT CONVERT(DATETIME, @date + ' ' + convert(varchar(10),@hour) + ':00:00') 

#2


0  

here is one another way to do this:-

这是另一种方法:-

declare @hour int=8,
@date varchar(10)='2014-10-2'
select dateadd(hh,@hour,CONVERT(datetime,@date))