How to Find Current Month and Year From Custom Date range?

I have a problem about custom date range. let me explain it, for our company month count from current month day 21 to next month day 20. if i want to know which employee join current month then normally i can find using sql

SELECT *
  FROM tblEmployeeMaster
  --CURRENT MONTH
  WHERE MONTH(DateOfJoining)=MONTH(GETDATE())  AND YEAR(DateOfJoining)=YEAR(GETDATE())

but for this custom date range i can not find employee who are join in this month. For example in this system if i want to find list of employee joined in this month then result will be show that employee who join in previous month after day 21 to current month day 20. Could someone please help me?

Answers


In this example, this month's period is considered to be from 21 of the previous month to 20 of current month, employees in that period are considered to be this month's employees (all who came to company after 2012-05-21 until 2012-06-20 (including 2012-06-20)). Try it:

SELECT  DateOfJoining,
        DATEADD(DAY, 20, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, 0)) PeriodStart,
        DATEADD(DAY, 19, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)) PeriodEnd
FROM    tblEmployeeMaster
WHERE   DateOfJoining > DATEADD(DAY, 20, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, 0)) 
        AND 
        DateOfJoining < DATEADD(DAY, 20, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0))

SELECT *
FROM tblEmployeeMaster  
WHERE DateOfJoining BETWEEN CAST(CAST(YEAR(GETDATE()) AS VARCHAR) +'-'+ CAST(MONTH(GETDATE()) AS VARCHAR) +'-21' AS DATETIME)
AND CAST(CAST(YEAR(DATEADD(month,1,GETDATE())) AS VARCHAR) +'-'+ CAST(MONTH(DATEADD(month,1,GETDATE())) AS VARCHAR) +'-20' AS DATETIME)

Need Your Help

If I have two values, how do I push each of them to the same row in an array? - Ruby

ruby

So I am parsing a page with Nokogiri and am storing values like this: