Darius
asked on
TSQL - DATEDIFF() FUNCTION
Hi Guys,
I need to specify date...
And I need to reduce records found in WHERE clause which date is not older when 7 days and not today date.
Please assist on DATEDIFF function below
How to use diff function in to simple way?
Thank you!
I need to specify date...
And I need to reduce records found in WHERE clause which date is not older when 7 days and not today date.
Please assist on DATEDIFF function below
How to use diff function in to simple way?
--
DECLARE @OrderDate sql_variant= '2017-01-16';
. . .
where cast(d.dateAdded as date) = @OrderDate and datediff(MINUTE,dateAdded,CURRENT_TIMESTAMP) < 5040 and dateAdded <> GETDATE()
where cast(d.dateAdded as date) = @OrderDate and d.dateAdded = datediff(DAY,(GETDATE()-1),(GETDATE()-7)
. . .
--
Thank you!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You don't need DATEDIFF or any variable:
SELECT *
FROM TableName
WHERE dateAdded BETWEEN GETDATE()-7 and GETDATE()
ASKER
Pawan,
my understand set below gives 7 days in past from provided date.
DECLARE @OrderDate DATETIME = '2017-01-16';
DECLARE @EndDate DATETIME = DATEADD(d,-7,@OrderDate)
It does not matter what user going to select in @OrderDate! Like I mention I need to make sure the date records in where clause is not today date and is not older when 7 days.
I like to reduce big bunch of records of old records from table... and not today date as well!
my understand set below gives 7 days in past from provided date.
DECLARE @OrderDate DATETIME = '2017-01-16';
DECLARE @EndDate DATETIME = DATEADD(d,-7,@OrderDate)
It does not matter what user going to select in @OrderDate! Like I mention I need to make sure the date records in where clause is not today date and is not older when 7 days.
I like to reduce big bunch of records of old records from table... and not today date as well!
records in where clause is not today date and is not older when 7 days.
SELECT *
FROM TableName
WHERE dateAdded BETWEEN CAST(GETDATE()-7 AS DATE) and CAST(GETDATE()-1 AS DATE)
Example pls?
Using my example you will get records where Datecolumn is between todaysdate-7 AND todaysDate
Dates you increase of decrease using like DATEADD(d,-7,@OrderDate) --- (@OrderDate-7 )
DATEADD(d,-6,@OrderDate) --- (@OrderDate-6 days )
DATEADD(d,-5,@OrderDate) --- (@OrderDate-5 days )
..
..
..
DATEADD(d,-1,@OrderDate) --- (@OrderDate-1 day )
etc...
Do you need something else?
Using my example you will get records where Datecolumn is between todaysdate-7 AND todaysDate
Dates you increase of decrease using like DATEADD(d,-7,@OrderDate) --- (@OrderDate-7 )
DATEADD(d,-6,@OrderDate) --- (@OrderDate-6 days )
DATEADD(d,-5,@OrderDate) --- (@OrderDate-5 days )
..
..
..
DATEADD(d,-1,@OrderDate) --- (@OrderDate-1 day )
etc...
Do you need something else?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you Pawan!
ASKER
Thank you Vitor!
Welcome Darius !!
ASKER
Guys,
Is it good idea to do this way?
Is it good idea to do this way?
DECLARE @OrderDate datetime = '2017-01-16'; -- PROVIDED DATE
DECLARE @DATESET datetime set @DATESET =
( select top 1 dateadded From Documents
where dateadded between CAST(GETDATE()-7 AS DATE) and CAST(GETDATE()-1 AS DATE) )
SELECT * FROM Documents
WHERE @DATESET is not NULL AND CAST(dateAdded AS DATE) = @OrderDate
Hi Darius,
Please try like below-
Hope it helps!
Please try like below-
DECLARE @OrderDate datetime = '2017-01-16'; -- PROVIDED DATE
DECLARE @DATESET datetime
Select top 1 @DATESET = dateadded From Documents where dateadded between CAST(GETDATE()-7 AS DATE) and CAST(GETDATE()-1 AS DATE))
SELECT * FROM Documents
WHERE @DATESET is not NULL AND dateAdded = @OrderDate
Hope it helps!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi Darius,
You can also do like below -
Hope it helps
You can also do like below -
DECLARE @OrderDate datetime = '2017-01-16'; -- PROVIDED DATE
DECLARE @DATESET datetime
Select top 1 @DATESET = dateadded From Documents where ( dateadded between CAST(GETDATE()-7 AS DATE) and CAST(GETDATE()-1 AS DATE)) )
AND ( dateadded IS NOT NULL )
SELECT * FROM Documents
WHERE dateAdded = @OrderDate
Hope it helps
Darius, can you explain why the following doesn't meet your requirement?
SELECT *
FROM Documents
WHERE dateAdded >= CAST(GETDATE()-7 AS DATE) and dateAdded < CAST(GETDATE() AS DATE)
ASKER
Vitor, I can see you proposed to use IF statment...
I like to ask what if I like to use different table when is dates records for today date
I like to ask what if I like to use different table when is dates records for today date
DECLARE @OrderDate datetime = '20170124'; -- PROVIDED DATE
DECLARE @DATESET datetime
DECLARE @ISTODAY datetime
select top 1 @DATESET = dateadded
From Documents1
where dateadded between CAST(GETDATE()-7 AS DATE) and CAST(GETDATE()-1 AS DATE)
select top 1 @ISTODAY= dateadded
From Documents2
where dateadded = GETDATE()
IF @DATESET is not NULL
SELECT * FROM Documents
WHERE CAST(dateAdded AS DATE) = @OrderDate
ELSE
-- GET RESULTS IF DATE TODAY AND USER PROVIDED TODAY DATE
Yes, you can use IF in a T-SQL script.
I'm just trying to understand your requirements because you said: "And I need to reduce records found in WHERE clause which date is not older when 7 days and not today date" and then your code shows that you only want to return the today's date:
I'm just trying to understand your requirements because you said: "And I need to reduce records found in WHERE clause which date is not older when 7 days and not today date" and then your code shows that you only want to return the today's date:
DECLARE @OrderDate datetime = '20170124'; -- PROVIDED DATE
(...)
IF @DATESET is not NULL
SELECT * FROM Documents
WHERE CAST(dateAdded AS DATE) = @OrderDate
Why is that?
ASKER
Vitor,
Need to reduce amount of checked records (system is really busy)
required if :
1. Customer provided today date!
(Documents with today timestamp will be reviewed from Table1 )
2. Customer provided older date! (not today but not older when 7 days)!
Documents with these timestamps will be reviewed from Table2
Need to reduce amount of checked records (system is really busy)
required if :
1. Customer provided today date!
(Documents with today timestamp will be reviewed from Table1 )
2. Customer provided older date! (not today but not older when 7 days)!
Documents with these timestamps will be reviewed from Table2
Need to reduce amount of checked records (system is really busy)There are other better methods for that. What you're doing here may not help at all so I would try to go with a simple solution and then realize about the performance (if too slow come back so we can try to tune it). Here's my suggestion:
DECLARE @OrderDate datetime = '20170124'; -- PROVIDED DATE
IF CAST(GETDATE() AS DATE) = @OrderDate
-- GET RESULTS IF DATE TODAY AND USER PROVIDED TODAY DATE
BEGIN
select *
From Documents1
where dateadded > CAST(GETDATE() AS DATE)-1 AND dateadded < CAST(GETDATE() AS DATE)+1
select *
From Documents2
where dateadded > CAST(GETDATE() AS DATE)-1 AND dateadded < CAST(GETDATE() AS DATE)+1
END
ELSE
-- -- GET RESULTS IF USER PROVIDED OLDER DATE
BEGIN
select *
From Documents1
where dateadded >= CAST(GETDATE()-7 AS DATE) and dateadded < CAST(GETDATE() AS DATE)
select *
From Documents2
where dateadded >= CAST(GETDATE()-7 AS DATE) and dateadded < CAST(GETDATE() AS DATE)
END
ASKER
FYI,
Table 1 has only today records
and
Table 2 has records older when today date (all records of today on table 1 will be move to table 2 tomorrow)
Table 1 has only today records
and
Table 2 has records older when today date (all records of today on table 1 will be move to table 2 tomorrow)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I think this is what I need!
IF CAST(GETDATE() AS DATE) = @OrderDate
-- GET RESULTS IF DATE TODAY AND USER PROVIDED TODAY DATE
BEGIN
select *
From Documents1
where dateadded > CAST(GETDATE() AS DATE)-1 AND dateadded < CAST(GETDATE() AS DATE)+1
END
ELSE
-- -- GET RESULTS IF USER PROVIDED OLDER DATE
BEGIN
select *
From Documents2
where dateadded >= CAST(GETDATE()-7 AS DATE) and dateadded < CAST(GETDATE() AS DATE)
END
It will also work. I just adjusted the filtering based on your last comment about having 2 tables.
Please let me know about the performance.
Please let me know about the performance.
ASKER
Thank you guys for your time and patience !!!
Much appreciate!
Much appreciate!
Please try like below-
Open in new window
Output
Open in new window
You need to try like
Open in new window
Hope it helps!