Need some help with DATEADD  and GETDATE

Posted on 2011-03-24
Last Modified: 2012-05-11
in my sgl table (Server 2000) is a column with name Lieferscheindatum
it is a datetime 8  like 24.03.2011  for today.

I would like to get the data from my table:
1) for the last 3 days (from the current date)
2) for the current week
3) for the last 3 month

I think I have to use DATEADD but I don´t now how.

???DATEADD(Day,-3,Lieferscheindatum)AS Lieferscheindatum..........???? will not work

500 points with a solution.
Thank you.
Best regards,
this query is for the data of the current month and year
this works

nYear  = current year   2011
nMonth = current month  3

sql.Format("SELECT * FROM ViewLieferscheine WHERE \
		   DATEPART(year,Lieferscheindatum)=%d AND\
                     DATEPART(month,Lieferscheindatum)=%d \
                     ORDER BY CAST(LieferscheinNr AS BIGINT)DESC ",nYear,nMonth);

Open in new window

Question by:tsp2002

Accepted Solution

MicMatic earned 167 total points
ID: 35205790
I would try something like:

1. WHERE Lieferscheindatum BETWEEN DATEADD(day, -3, GETDATE()) AND GETDATE()

2. WHERE Lieferscheindatum BETWEEN DATEADD(day, -7, GETDATE()) AND GETDATE()

3. WHERE Lieferscheindatum BETWEEN DATEADD(month, -3, GETDATE()) AND GETDATE()

Open in new window


Expert Comment

ID: 35205796
This will work
SELECT * FROM ViewLieferscheine WHERE Lieferscheindatum >  DATEADD(dd,-3, getdate())

Open in new window


Assisted Solution

selimfelex earned 167 total points
ID: 35205963
if the getdate() returns an hour you must convert it
like this

where Lieferscheindatum=CONVERT(varchar(12),DATEADD(day,-1,GETDATE()),101)
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 166 total points
ID: 35205977

For last 3 days
1. WHERE Lieferscheindatum BETWEEN DATEADD(day, -3, GETDATE()) AND GETDATE()

for Current week you neeed to find out first and last day of week
use below function to get first day of week, you canchange it as per your requiremnt
create function dbo.F_START_OF_WEEK
      @DATE                  datetime,
      -- Sun = 1, Mon = 2, Tue = 3, Wed = 4
      -- Thu = 5, Fri = 6, Sat = 7
      -- Default to Sunday
      @WEEK_START_DAY            int      = 1      
Find the fisrt date on or before @DATE that matches
day of week of @WEEK_START_DAY.
returns            datetime
declare       @START_OF_WEEK_DATE      datetime
declare       @FIRST_BOW            datetime

-- Check for valid day of week
if @WEEK_START_DAY between 1 and 7
      -- Find first day on or after 1753/1/1 (-53690)
      -- matching day of week of @WEEK_START_DAY
      -- 1753/1/1 is earliest possible SQL Server date.
      select @FIRST_BOW = convert(datetime,-53690+((@WEEK_START_DAY+5)%7))
      -- Verify beginning of week not before 1753/1/1
      if @DATE >= @FIRST_BOW
            select @START_OF_WEEK_DATE =


for more info refer

Then once you get first day of week , add 6 or 7 days to it depend on how many days week you consider
and get crrent week data like

cod elike

 WHERE Lieferscheindatum BETWEEN dbo.F_START_OF_WEEK(GETDATE(),1) AND dateadd(dd,6,dbo.F_START_OF_WEEK(GETDATE(),1))

3. For Last 3 months
WHERE Lieferscheindatum BETWEEN DATEADD(month, -3, GETDATE()) AND GETDATE()

Author Comment

ID: 35206521
thanks for the answers.
This will work:
WHERE Lieferscheindatum BETWEEN DATEADD(day, -3, GETDATE()) AND GETDATE()         last 3 days
WHERE Lieferscheindatum BETWEEN DATEADD(month, -3, GETDATE()) AND GETDATE()    last 3 month

I never did a function.
Is there perhaps and easy way.

with DATENAME(weekday, GetDay()) AS..... I will get the current day of the week.
So I want to get all the data of the currrent week.

So if I have the day I just mínus the days like this

weekday = Sunday        = start at Monday so minus 6
weekday = Saturday      = start at Monday so minus 5
weekday = Friday           = start at Monday so minus 4

WHERE Lieferscheindatum BETWEEN DATEADD(day, -6 or -5 or -4 etc, GETDATE()) AND GETDATE()
can you help me with that,
Thank you.
Best regards,


Author Comment

ID: 35207007
Hi there,
I found the solution:

      CTime time=CTime::GetCurrentTime();
       int nGetDayOfWeek =time.GetDayOfWeek();

                //1 = Sonntag
      //2 = Montag
      //3 = Dienstag
      //4 = Mittwoch
      //5 = Donnerstag
      //6 = Freitag
      //7 = Samstag

      int Tage_Minus_Aktuelle_Woche=0;

      if(nGetDayOfWeek==1) Tage_Minus_Aktuelle_Woche=7;
      if(nGetDayOfWeek==2) Tage_Minus_Aktuelle_Woche=1;
      if(nGetDayOfWeek==3) Tage_Minus_Aktuelle_Woche=2;
      if(nGetDayOfWeek==4) Tage_Minus_Aktuelle_Woche=3;
      if(nGetDayOfWeek==5) Tage_Minus_Aktuelle_Woche=4;
      if(nGetDayOfWeek==6) Tage_Minus_Aktuelle_Woche=5;
      if(nGetDayOfWeek==7) Tage_Minus_Aktuelle_Woche=6;

       //aktuelle Woche
            sql.Format("SELECT * FROM ViewLieferscheine WHERE \
                     Lieferscheindatum BETWEEN DATEADD(day,-%d,GETDATE()) AND GETDATE()\
                                 ORDER BY CAST(LieferscheinNr AS BIGINT)DESC ",Tage_Minus_Aktuelle_Woche

Author Closing Comment

ID: 35207020
thank you
Best regards,

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Query Peformance + mulitple query plans 9 54
Dimension table indexes 8 25
Please help with the below query - SQL Server 11 17
TSQL - How to declare table name 26 28
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now