2

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

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

How do I convert to this datetime format:

YYYY-MM-DD HH:MM:SS

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

Example :

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

Should be converted to

2014-10-2 08:00:00

How do I do this?

ETAN
  • 3,152
  • 6
  • 26
  • 35

2 Answers2

4

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

  SELECT CONVERT(DATETIME, @date + ' ' + convert(varchar(10),@hour) + ':00:00') 
Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
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))
user3864233
  • 514
  • 3
  • 12