Solved

Need some help with DATEADD  and GETDATE

Posted on 2011-03-24
7
469 Views
Last Modified: 2012-05-11
Hello,
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,
Thomas
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

0
Comment
Question by:tsp2002
7 Comments
 
LVL 3

Accepted Solution

by:
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

0
 
LVL 7

Expert Comment

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

Open in new window

0
 
LVL 3

Assisted Solution

by:selimfelex
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)
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
as
begin
declare       @START_OF_WEEK_DATE      datetime
declare       @FIRST_BOW            datetime

-- Check for valid day of week
if @WEEK_START_DAY between 1 and 7
      begin
      -- 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
            begin
            select @START_OF_WEEK_DATE =
            dateadd(dd,(datediff(dd,@FIRST_BOW,@DATE)/7)*7,@FIRST_BOW)
            end
      end

return @START_OF_WEEK_DATE

end
go
for more info refer
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=47307

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()
0
 

Author Comment

by:tsp2002
ID: 35206521
Hello,
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




pratima_mcs
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,
Thomas




0
 

Author Comment

by:tsp2002
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
0
 

Author Closing Comment

by:tsp2002
ID: 35207020
thank you
Best regards,
Thomas
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

In this article I will describe the Detach & Attach 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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

808 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