Home > database >  SQL - Correct dates not being presented in result set with date ranges in WHERE clause
SQL - Correct dates not being presented in result set with date ranges in WHERE clause

Time:10-08

Result set without the where clause shows there is data from the 30th September, when using the below where clause the 30th data doesn't come through in the result set (see images below for result sets) - my main aim is to show data from between the first day of the previous month and the last day of the previous month - Any help is much appreciated:

SQL Query

DECLARE @date1 datetime
DECLARE @date2 datetime
SET @date1 = getdate()
SET @date2 = getdate()-15 -- reason for less 15 days is because this will only be run within the first 15 days 
                          -- of the current month, it will enable me to get the last day of the previous 
                          -- month even if I run the SQL in the next month.

SELECT
        A.TA_SEQ as 'WO No',
        A.TA_DUE_DATEUTC

FROM 
        F_TASKS A WITH (NOLOCK)
        INNER JOIN FINFILE B WITH (NOLOCK) ON A.TA_FKEY_IN_SEQ = B.IN_SEQ 
        INNER JOIN InstructionSetGroups C WITH (NOLOCK) ON B.InstructionSetGroupId = C.InstructionSetGroupId 

WHERE
        A.TA_TASK_DESC = 'BREAKDOWN' AND
        A.TA_STATUS IN ('ACTIVE', 'ASSIGNED', 'COMPLETE', 'HISTORY') AND
        A.TA_DUE_DATE >= DATEADD(DAY, 1, EOMONTH(@date1, -2)) AND
        A.TA_DUE_DATE <= EOMONTH(@date2) 

ORDER BY 
        A.TA_DUE_DATE desc

Result Sets

Result set using date ranges in where clause:

Blockquote

Result set without using date ranges in where clause - as you can see there's lots of data from the 30th too which isn't being captured

Blockquote

CodePudding user response:

I would take a different approach to handling your date values. That whole -15 stuff is bizarre and will cause issues on days towards the end of the month. Instead using a single variable is sufficient if you understand some date math.

DECLARE @date1 date --use the data datatype here because time is important in this case
SET @date1 = getdate()

--this is to show you the values and can be removed.
select BeginningOfLastMonth = dateadd(month, datediff(month, 0, @date1) - 1, 0)
    , BeginningOfCurrentMonth = dateadd(month, datediff(month, 0, @date1), 0)

--Your select statement here
where A.TA_DUE_DATE >= dateadd(month, datediff(month, 0, @date1) - 1, 0)
        A.TA_DUE_DATE <= dateadd(month, datediff(month, 0, @date1), 0) --ANY datetime value less than this is before the end of last month

CodePudding user response:

EOMONTH is terribly ambiguous, because it returns the last day of the month at midnight. I talk about why I don't like this function in Will you use EOMONTH()?

If you want the whole previous month, there are easier and safer ways than assuming the code will run within the first 15 days:

DECLARE @ThisMonth date = DATEFROMPARTS(YEAR(GETDATE()), MONTH(GETDATE()), 1);

SELECT ... FROM ...
  WHERE TA_DUE_DATE >= DATEADD(MONTH, -1, @ThisMonth)
    AND TA_DUE_DATE <  @ThisMonth;

I explain why DATEFROMPARTS is best suited for this kind of work in Simplify Date Period Calculations in SQL Server.

With the rest, where do we begin?

  • Related