Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Adding 1 hour

Author  Topic 

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2010-02-15 : 22:26:49
hi

I am not able to add 1 hour. Here is the code:
Declare @Appt_EndDate smalldatetime,@Appt_StartDate smalldatetime
set @Appt_StartDate = '2010-02-17 14:30:00'
Set @Appt_EndDate = @Appt_StartDate + datepart(hh,@Appt_StartDate)+1
select @Appt_StartDate,@Appt_EndDate

The end result should be '2010-02-17 15:30:00' but i am getting 2010-03-04 14:30:00

What should i change in the code? Thanks a lot.

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2010-02-15 : 22:57:05
[code]
set @Appt_EndDate = dateadd(hour, 1, @Appt_StartDate)
[/code]


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

sg2255551
Constraint Violating Yak Guru

274 Posts

Posted - 2010-02-15 : 23:05:19
Thanks a lot
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-16 : 01:01:35
what @Appt_StartDate + datepart(hh,@Appt_StartDate)+1
does is to retrive hour part of date (which is 14) and add it as integer value to date and again another 1

so result will be 14+1 15 days ahead of @Appt_StartDate which is what you got as 2010-03-04 14:30:00

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -