Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Need some help with DATEADD  and GETDATE

Posted on 2011-03-24
7
Medium Priority
?
475 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 668 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 668 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 664 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

618 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