?
Solved

SQL Query DatePart Case question

Posted on 2015-01-26
7
Medium Priority
?
125 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
  • 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
Technology Partners: 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!

 
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 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.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
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 …
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

749 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