Solved

increment date by one using sql

Posted on 2012-04-02
5
451 Views
Last Modified: 2012-04-02
hi,
i have column date that is like this
2012-03-30
2012-03-29
2012-03-28
2012-03-27
2012-03-26

all what i want to do is to update the dates in this column by incrementing the date by one date.  Example:  if it is showing 2012-03-26 then it should be updated into 2012-03-27
another example, if it is showing 2012-04-23 then it should be 2012-04-24.  But if the date is on Friday then i want to be updated the date on the following Monday skipping the weekend.  For instance, if it is 2012-03-30 then it should be 2012-04-02.  many thanks
0
Comment
Question by:karinos57
  • 2
  • 2
5 Comments
 
LVL 9

Assisted Solution

by:OCDan
OCDan earned 250 total points
Comment Utility
If your date field is a proper datetime field then you can use the following:

UPDATE t
SET datefield = (case when datename(weekday,datefield) = 'Friday'
THEN datefield+3
ELSE datefield+1
END)
From table1 t

Or if it is stored as a varchar you could use this:

UPDATE t
SET datefield = (case when datename(weekday,convert(datetime,datefield)) = 'Friday'
THEN convert(varchar,convert(datetime,datefield)+3,105)
ELSE convert(varchar,convert(datetime,datefield)+1,105)
END)
From table1 t
0
 
LVL 23

Accepted Solution

by:
wdosanjos earned 250 total points
Comment Utility
Here is another option:

declare @test table (mydate date)

insert into @test values ('2012-03-30')
insert into @test values ('2012-03-29')
insert into @test values ('2012-03-28')
insert into @test values ('2012-03-27')
insert into @test values ('2012-03-26')

select * from @test

update @test
   set mydate = case datepart(dw, mydate)
                when 6 then dateadd(day, 3, mydate) -- Friday
                when 7 then dateadd(day, 2, mydate) -- Saturday
                else dateadd(day, 1, mydate)
                end
		
select * from @test

Open in new window


Output
mydate
----------
2012-03-30
2012-03-29
2012-03-28
2012-03-27
2012-03-26

(5 row(s) affected)

mydate
----------
2012-04-02
2012-03-30
2012-03-29
2012-03-28
2012-03-27

Open in new window

0
 

Author Comment

by:karinos57
Comment Utility
i converted the date field into datetime but still getting this error:
Msg 206, Level 16, State 2, Line 1
Operand type clash: date is incompatible with int
thanks
0
 
LVL 9

Expert Comment

by:OCDan
Comment Utility
Hmm unusual, try this instead then mate:
UPDATE t
SET datefield = (case when datename(weekday,datefield) = 'Friday'
THEN dateadd(day,+3,datefield)
ELSE dateadd(day,+1,datefield)
END)
From table1 t

I was just trying to take a shortcut, obviously doesn't work on all versions of SQL.
0
 

Author Closing Comment

by:karinos57
Comment Utility
thnks guys
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now