MS SQL Tips & Tricks

How to replace time part in datetime variable

The simpliest way of replacing the time in datetime variable without changeing the actual date is to do a trick as bellow:

declare @cDateTime datetime;
declare @cHour integer;
declare @cMinute integer;
declare @cSecond integer;

set @cDateTime=getdate();
set @cHour = 12;
set @cMinute = 0;
set @cSecond = 0;

select  dateadd(dd,0, datediff(dd,0,@cDateTime))+
	dateadd(ss,(@cHour*3600)+(@cMinute*60)+@cSecond,0)	

See also


    Learn more tricks

    Add Comment

    Comments