I have a table tblEmpDetail which contains a column CreatedDate.
I want to add 72 hours to each created date, so if the created date is 2012-07-14 07:21:19.180 then I want the output to be 2012-07-17 07:21:19.180.
Can someone let me know how can I accomplish to this?
Actually what I want to do is to go to each row then check if the
getdate() - createddate
is equal to or less than 72 hours; then I need to add 72 hours to createddate. Otherwise I want that column to be null. I am pasting my code that what I have done.
Declare @PassedTime datetime
set @PassedTime= DATEDIFF(HH,Createddate, GETDATE())
if CONVERT(varchar(20),@PassedTime,108)>=CONVERT(varchar(20),'72:00:00',108)
begin
select empno,empName,CreatedDate,dateadd(HH,72,CreatedDate)BD from tblEmpDetail
end
else
begin
select empno,empName,CreatedDate,'' as BD from tblEmpDetail
end
No looping is required.
SQL Server excels at doing “set based” queries.
To get a projection:
To update the table permanently:
If you MUST have a loop, you can use a cursor:
More information on cursors here: http://msdn.microsoft.com/en-us/library/ms180169.aspx
EDIT
Your query above can be done in a set based way like this: