Solved

Manipulate Date in SQL Statement

Posted on 2015-01-27
1
174 Views
Last Modified: 2015-01-27
Hello Experts,

I have a SQL table that keeps information about monthly Transactions by Day of Week as opposed to actual date. Below is an example:

Month              DOW
01-Jan-2014     Mon
01-Jan-2014     Tue
01-Jan-2014     ----
01-Jan-2014     Sun
----
01-Feb-2014     Mon
01-Feb-2014     Tue
---
01-Dec-2014     Mon
01-Dec-2014     Tue
---
01-Dec-2014     Sat
01-Dec-2014     Sun

As you can see, the day of month in the Month column is always 01.

I need to add a 3rd column for a new Date value that is dependent on the value of both Month and DOW columns. Basically, I need to convert day value from the generic 01 value to a value between 1-7:
01-MMM-YYY
02-MMM-YYY
03-MMM-YYY
04-MMM-YYY
05-MMM-YYY
06-MMM-YYY
07-MMM-YYY

The new date will be determined according to the Month and DOW values, so for example:


Month              DOW          New Date        
01-Jan-2014     Mon          06-Jan-2014
01-Jan-2014     Tue          07-Jan-2014
01-Jan-2014     Sun          05-Jan-2014
01-Feb-2014    Mon         03-Feb-2014
01-Feb-2014    Tue          04-Feb-2014
01-Dec-2014    Mon         01-Dec-2014
01-Dec-2014    Tue          02-Dec-2014

Ideally, the new column should be added to the Select statement as condition or CASE statement, based on the values in Month and DOW.

Many thanks in advance.
Hani
0
Comment
Question by:Mehawitchi
1 Comment
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40572827
select [Month], DOW, datepart(dw,[Month]), dateadd(day, (7-datepart(dw,[Month])+
case [DOW] when 'Mon' then 2
when 'Tue' then 3
when 'Wed' then 4
when 'Thu' then 5
when 'Fri' then 6
when 'Sat' then 0
when 'Sun' then 1
end)%7, [Month]) as [New Date]
from myDates
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
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…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

760 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

20 Experts available now in Live!

Get 1:1 Help Now