Solved

increment date by one using sql

Posted on 2012-04-02
5
467 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 250 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 250 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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
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.

695 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