Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Query DatePart Case question

Posted on 2015-01-26
7
Medium Priority
?
122 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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

636 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