Solved

Manipulate Date in SQL Statement

Posted on 2015-01-27
1
184 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server - Copy database from one server to another 3 30
Solar Winds can't see SQL Server Express 17 32
TSQL XML Namespaces 7 22
SQL Quer 4 21
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

840 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