Solved

Need some help with DATEADD  and GETDATE

Posted on 2011-03-24
7
471 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Help 27 69
SQL DATEADD 10 81
T-SQL: I need to add an index on a field 5 27
calculate running total 8 16
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

726 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