Solved

SQL Query DatePart Case question

Posted on 2015-01-26
7
116 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
Industry Leaders: 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 500 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 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
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.

688 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