SQL server filtering from Today date and X number of days prior

Jimko1 61 Reputation points
2021-03-09T11:04:33.42+00:00

Hi all im pretty new to SQL.

This is the current simple query, which returns the carrier with lowest number of haulage shipments in the uk for a particular brewery.

USE Rep_UK

SELECT count(DISTINCT Load_ID) AS STO
 ,carrierName
FROM dbo.main_table_uk AS main
WHERE Origin_Location_ID LIKE 'GB%'
 AND Destination_Location_ID LIKE 'GB%'
GROUP BY carrierName
ORDER BY STO

I need to in addition to this, also filter by a date range which needs to be 199 days previous from the current (today) date.

We have a 'delivery date' table which contains dates in the following format dd/mm/yyyy.

I understand I need to likely convert this first to INT then minus 199 from current date but cannot work out how to do it, any help much appreciated!

If I missed something please let me know!

using SQL server.

EDIT: I tried to use WHERE deliveryDate < DateAdd(dd, -199, GetDate()) but I got an error


EDIT found what I was looking for by adding

WHERE convert(DATE, main.deliveryDate, 103) >= GETDATE()-199

this worked and did not return error, many thanks for all help.

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,641 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,547 questions
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Vaibhav Chaudhari 38,561 Reputation points
    2021-03-09T11:14:34.66+00:00

    Try adding below like where clause

    Where convert(date,YourDateCol , 103)< cast(GETDATE() as date) and YourDateCol > DATEADD(DAY,-199, convert(date,GETDATE(), 103))
    

    Also see below example -

    select DATEADD(DAY,-199, convert(date, '09/03/2021', 103))
    

    Please don't forget to Accept Answer and Up-vote if the response helped -- Vaibhav

    1 person found this answer helpful.

  2. Olaf Helper 40,656 Reputation points
    2021-03-09T11:15:24.043+00:00

    If you get an error message, then please post the full error message.

    This should work

    SELECT count(DISTINCT main.Load_ID) AS STO
          ,main.carrierName
    FROM dbo.main_table_uk AS main
    WHERE main.Origin_Location_ID LIKE 'GB%'
          AND main.Destination_Location_ID LIKE 'GB%'
          AND main.deliveryDate BETWEEN DATEADD(day, -199, CONVERT(date, GETDATE())) AND CONVERT(date, GETDATE())
    GROUP BY main.carrierName
    ORDER BY STO
    

    Keep in mind, that GETDATE() returns a time portion, which can cause wrong results; that's why I convert it to "date" without time.

    1 person found this answer helpful.

  3. BredonHill 6 Reputation points
    2021-03-09T14:04:19.463+00:00

    Does this help?

    -- firstly, for test purposes, build a table of delivery dates in required format
    create table #temp ( mydate varchar(50));
    
    declare @d date;
    set @d = '20200101';
    
    declare @s char(10);
    
    while @d <= cast(getdate() as date)  
    begin
        set @s = convert(varchar(50), @d, 103);
        insert into #temp values (@s);
        set @d = dateadd(d,1,@d);
    end
    
    -- check character dates are in format required
    select mydate from #temp;
    
    -- check expression
    select dateadd(day,-199,cast(getdate() as date));
    
    -- check where clause
    select mydate from #temp 
        where mydate between convert(varchar(50),dateadd(day,-199,cast(getdate() as date)),103)
        and cast(getdate() as date) ;
    
    1 person found this answer helpful.