?
Solved

SQL Query DatePart Case question

Posted on 2015-01-26
7
Medium Priority
?
121 Views
Last Modified: 2015-01-26
Hello all,

I have the following tables that has the following:

Table:  Customer
Fields:  CustID,   BillingFrequencyID

Table:   BillingFrequency
BillingFrequencyID,   BillingFrequencyName
1                                   Monthly
2                                   Quarterly
3                                   Semi-Annually
4                                   Annually

What I need to do is I am going to get passed a parameter @PeriodEndDate.    I need to filter the list of Customers based on does that Date fall into the Billing Frequency.   Such as lets say I get passed:

12/31/14  that would mean I would want all customers where BillingFrequency IN (1, 2, 4) it falls on the last day of the month, last day of quarter and last day of the year.

If it is 1/23/14 - then no data would return

If 1/31/15 then where BillingFrequency = 1 as its just a Month End.

How can I use date parsing to get the data correct?

Thanks all
0
Comment
Question by:sbornstein2
[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
  • 4
  • 3
7 Comments
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 40570926
like this ?
select *
from customer
where

billingfrequency in (
case when month(@periodenddate) in (1,3,5,7,8,10,12)
     then case when datepart(dd,@periodenddate)=31 then 1 end
     when month(@periodenddate) in (9,4,6,11) 
     then case when datepart(dd,@periodenddate)=30 then 1 end
     else case when year(@periodenddate)%4 = 0 
               then case when datepart(dd,@periodenddate)=29  
                         then 1 end
               else case when datepart(dd,@periodenddate)=28
                         then 1 end
               end
     end
,case when (month(@periodenddate) in (3,12) and datepart(dd,@periodenddate)=31)
        or (month(@periodenddate) in (6,9) and datepart(dd,@periodenddate)=30)
      then 2
      end
,case when month(@periodenddate)=6 and datepart(dd,@periodenddate)=30 then 3 end
,case when month(@periodenddate)=12 and datepart(dd,@periodenddate)=31 then 4 end
)
order by billingfrequency,custid

Open in new window

0
 

Author Comment

by:sbornstein2
ID: 40570946
There must be some easier ways to handle the end of the month days correct?  It looks like this:

DATEPART(DAY,DATEADD(DAY,1,'10/31/2014')) = 1  for example will always check if it falls on the end of a month.
0
 

Author Comment

by:sbornstein2
ID: 40570951
actually the IN might not be correct.   I don't want to pull for example a Customer with a Quarter End if the date is a month end date such as '2/28/15' is passed I would not want this customer even though its a month end.
0
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

 
LVL 50

Accepted Solution

by:
Lowfatspread earned 2000 total points
ID: 40570968
ok like this then
select *
from customer
where

billingfrequency in (
case when DATEPART(DAY,DATEADD(DAY,1,@periodenddate)) = 1
     then 1
     end
,case when DATEPART(DAY,DATEADD(DAY,1,@periodenddate)) = 1
      and month(@periodenddate) in (3,6,9,12)
      then 2
      end
,case when month(@periodenddate)=6 and datepart(dd,@periodenddate)=30 then 3 end
,case when month(@periodenddate)=12 and datepart(dd,@periodenddate)=31 then 4 end
)
order by billingfrequency,custid

Open in new window

0
 

Author Comment

by:sbornstein2
ID: 40570970
maybe this?

case when DATEPART(DAY,DATEADD(DAY,1,'10/31/2014')) = 1 then 1
     end,
case when DATEPART(DAY,DATEADD(DAY,1,'10/31/2014')) = 1 and
          (MONTH('10/31/2014') = 3 OR MONTH('10/31/2014') = 6
           OR MONTH('10/31/2014') = 9 OR MONTH('10/31/2014') = 12)
      then 2
      end,
case when DATEPART(DAY,DATEADD(DAY,1,'10/31/2014')) = 1 and
          (MONTH('10/31/2014') = 6 OR MONTH('10/31/2014') = 12)
      then 3
      end,
case when DATEPART(DAY,DATEADD(DAY,1,'10/31/2014')) = 1 AND MONTH('10/31/2014') = 12
      then 4
      end
0
 

Author Closing Comment

by:sbornstein2
ID: 40570971
awesome thanks
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 40570976
I don't want to pull for example a Customer with a Quarter End if the date is a month end date such as '2/28/15' is passed I would not want this customer even though its a month end.
28th fEB ISN'T  a quarter end anyway so why would a qe customer get selected?
0

Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

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…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
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.
Suggested Courses

777 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