Solved

Need some help with DATEADD  and GETDATE

Posted on 2011-03-24
7
461 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
Comment Utility
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
Comment Utility
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
Comment Utility
if the getdate() returns an hour you must convert it
like this

where Lieferscheindatum=CONVERT(varchar(12),DATEADD(day,-1,GETDATE()),101)
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 166 total points
Comment Utility


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
Comment Utility
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
Comment Utility
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
Comment Utility
thank you
Best regards,
Thomas
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

12 Experts available now in Live!

Get 1:1 Help Now