?
Solved

increment date by one using sql

Posted on 2012-04-02
5
Medium Priority
?
469 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 9

Assisted Solution

by:OCDan
OCDan earned 1000 total points
ID: 37798351
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 1000 total points
ID: 37798400
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
ID: 37798406
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
ID: 37798443
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
ID: 37798501
thnks guys
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Suggested Courses

762 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