I need some help doing a date calculation.
I have something that Expires every X number of days away from its Create Date
So, if the Create Date was 4/22 and the Expiration days were set to 10 it would expire
5/2, 5/12, 5/22, 6/1 etc...
I need to be able to tell people when their item is going to expire within 5 days
So for 5/2, I need to add this item to a count if the current date is between 4/27 and 5/2.
This is in SQL.
All we have are the RunDate, the CreateDate and the ExpirationDays
I've done the math calc to roughly get the Expiration date, but if it gets a remainder it's not helpful, and I don't want to skew anyone's answer by posting what I think it should be. I've tried quite a few ways and am getting a little desperate.
Any help would be greatly appreciated
EDIT:
I did the math for this and it looks like this
CreateDate + (((RunDate - CreateDate)/ExpireDays)*ExpireDays)) Between Rundate-1 and Rundate +5
But this gives me arithmetic overflow in SQL, so I'm not sure what to do...
In MySql you could do something
(ExpirationDays - (DATEDIFF(NOW(), CreateDate) % ExpirationDays)) > 5;
EDIT
For SQL Server you would do it a little different:
#expiringDays - (DATEDIFF(dd, ml.CreateDate, #date) % #expiringDays) > 5;
With Expirations As
(
Select Cast('2011-04-22' As datetime) As CreateDate, 10 As ExpirationDays
Union All
Select DateAdd( d, ExpirationDays, CreateDate ), ExpirationDays
From Expirations
Where CreateDate <= DateAdd(d,10,CURRENT_TIMESTAMP) --(arbitary end date)
)
Select *
From Expirations
Where CreateDate >= CURRENT_TIMESTAMP
And CreateDate <= DateAdd(d,5,CURRENT_TIMESTAMP)
Using similar logic to the math you used in your updated post:
With Expirations As
(
Select Cast('2011-04-22' As datetime) As CreateDate, 10 As ExpirationDays
Union All
Select DateAdd( d, ExpirationDays, CreateDate ), ExpirationDays
From Expirations
Where CreateDate <= DateAdd(d,10,CURRENT_TIMESTAMP) --(arbitary end date)
)
Select *
From Expirations
Where CreateDate >= DateAdd(d, -1, CURRENT_TIMESTAMP)
And CreateDate <= DateAdd(d, 5, CURRENT_TIMESTAMP)
Related
I have a small question about SQL Server: how to get the last 30 days information from this column from table1:
created_at
updated_at
2020-02-05T01:25:42Z
2020-02-05T01:25:42Z
2020-05-05T02:31:56Z
2020-05-05T02:31:56Z
With the above data, I would need something like day count within 30 days.
I have tried
SELECT * FROM table1
DATEDIFF(CAST(SUBSTR(updated_at,1,10)) AS VARCHAR,CURRENT_TIMESTAMP) BETWEEN 0 AND 30 ;
and
SELECT * FROM table1
WHERE updated_at BETWEEN DATETIME('now', '-30 days') AND DATETIME('now', 'localtime')
Would need your expertise to help me with this query
Thank you!
SELECT *
FROM (
SELECT otherColumns
, DATEADD(mi, DATEDIFF(mi, GETUTCDATE(), GETDATE()), updated_at) AS updated_at
FROM table1
) b
WHERE CAST(b.updated_at AS DATE) >= DATEADD(DAY,-30,GETDATE())
I think this will help you
If you want a count of updates by day for 30 (or so) days, then:
SELECT CONVERT(DATE, updated_at) as dte, COUNT(*)
FROM table1
WHERE updated_at >= DATEADD(DAY, -30, CONVERT(DATE, GETDATE()))
GROUP BY CONVERT(DATE, updated_at)
ORDER BY CONVERT(DATE, updated_at);
Note that SQLite date/time functions (which your code uses) are very peculiar to to SQLite. So are SQL Server's -- although I personally find them easier to remember.
I need to ignore data anything that expected within the last 10 days back should be received if not ignore it. I am not sure how to write this in SQL. I have EXPDATE column. I am not sure my statement correct or not.
I believe the logic should be like this
Expected Date + 10 Days < Today`s date?
GETDATE() < DATEADD(DAY, +10, GETDATE()) - I found this online but where can I plug in my ExpectedDate column?
Thanks in advance!!
You can do it either way-
First add 10 days to EXPDATE and compare it with todays date like below.
select * from MyTable Where DATEADD(DAY, 10, EXPDATE) < GETDATE()
The other way, you can substract 10 days from today and compare it with EXPDATE.
select * from MyTable Where DATEADD(DAY, -10, GETDATE()) > EXPDATE
I would prefer 2nd one and would use variable to calculate 10 days back date as it's constant and then use it in where clause like below.
Declare #myDate datetime
SET #myDate = DATEADD(DAY, -10, GETDATE())
select * from MyTable Where #myDate > EXPDATE
I want every User that signed on in the last 30 Days, and to check their first 7 days of activity. Date maps their activity, once they've signed on. Day is how many days have passed since the user signed on.
I want something like this.
SELECT UserID, Date
FROM Web
WHERE (Day < = 30) and ( CONVERT(date, Date) - CONVERT(date,CURDATE()) - Day) <= 7 )
ORDER BY UserID asc, Date desc;
My SQL skills are very basic, so I need it to be simple as possible.
EDITED:
I'm thinking maybe something like this:
SELECT UserID, DT
FROM test
WHERE (Day < = 30) and DATEDIFF(DAY, DATEADD( DAY, -Day, GETDATE() ), Date) <= 7
ORDER BY UserID asc, DT desc;
This should do the trick:
SELECT UserID, Date
FROM Web
WHERE (Day < = 30) and DATEDIFF(DAY, DATEADD(DAY, -[Day], GETDATE()), [Date]) <= 7
ORDER BY UserID asc, Date desc;
I'm working in project for by PHP and SQL Server.
The owner want from me design page show only users who register in same day
i.e., if today 11-3-2011 i want show only all users who register in 11-3-2011
The table is:
id username date
1 john 11\3\2011
2 sara 11\3\2011
3 john 5\1\2011
4 kreem 1\2\2011
i make it by mysql
where DATE_ADD( items.created_date, INTERVAL 1 DAY ) > NOW()
this cable of code show data which only insert in same day thats mean if today 10-4-2011 it will show only data which insert in 10-4-2011 if i today 15-4-2011 and im dose not insert any thing it will not show any thing, how i can build code like this in sql server? hope to be my question clear and understand
select id, userName
from YourTable
where CAST(date AS DATE) = CAST(GETDATE() AS DATE)
Function GETDATE() returns the current date and time.
CAST(column as TYPE) will threat DateTime as just Date to omit differences in Time
For getting today's date record:
select * from tablename where date column between '2011-10-25 00:00:00' And '2011-10-25 23:59:59'
select * from yourtable where date = CONVERT(VARCHAR(20),GETDATE(),101) // hope this will be helpful to get current date value..
This query compares only the date piece of today, ignoring the time. It works in MS SQL Server, I'm not sure about other implimentations:
select *
from YourTable
where convert(datetime, floor(convert(float, GETDATE()))) = [date]
select *
from YourTable
where
[date] >= '20110311' and
[date] < '20110312'
If you want it for today (always) you can use this
select *
from YourTable
where
[date] >= dateadd(d, datediff(d, 0, getdate()), 0) and
[date] < dateadd(d, datediff(d, 0, getdate())+1, 0)
Last 10 days.
select *
from YourTable
where
[date] >= dateadd(d, datediff(d, 0, getdate())-9, 0)
Edit 1 Query with sample data and result
Table definition
create table users
(name varchar(20),
user_register_date datetime)
Data
insert into users values ('user today', getdate())
insert into users values ('user yesterday', getdate()-1)
insert into users values ('user 9 days ago', getdate()-9)
insert into users values ('user 10 days ago', getdate()-10)
Query
select *
from users
where user_register_date >= dateadd(d, datediff(d, 0, getdate())-9, 0)
Result
name user_register_date
-------------------- -----------------------
user today 2011-04-14 08:29:28.407
user yesterday 2011-04-13 08:29:28.410
user 9 days ago 2011-04-05 08:29:28.410
SELECT * from YourTable
WHERE cast(convert(varchar(10),yourdate,101) as datetime) =
cast(convert(varchar(10),[any day you want],101) as datetime)
Hope this helps. Just replace [any day you want] with a date or datetime value
The CAST function is equivalent to the CONVERT function, except convert allows for some formatting options. In my example, I simply trimmed off the time from the date.
I am looking for a good SQL Statement to select all rows from the previous day from one table. The table holds one datetime column. I am using SQL Server 2005.
get today no time:
SELECT dateadd(day,datediff(day,0,GETDATE()),0)
get yestersday no time:
SELECT dateadd(day,datediff(day,1,GETDATE()),0)
query for all of rows from only yesterday:
select
*
from yourTable
WHERE YourDate >= dateadd(day,datediff(day,1,GETDATE()),0)
AND YourDate < dateadd(day,datediff(day,0,GETDATE()),0)
To get the "today" value in SQL:
convert(date, GETDATE())
To get "yesterday":
DATEADD(day, -1, convert(date, GETDATE()))
To get "today minus X days": change the -1 into -X.
So for all yesterday's rows, you get:
select * from tablename
where date >= DATEADD(day, -1, convert(date, GETDATE()))
and date < convert(date, GETDATE())
It's seems the obvious answer was missing. To get all data from a table (Ttable) where the column (DatetimeColumn) is a datetime with a timestamp the following query can be used:
SELECT * FROM Ttable
WHERE DATEDIFF(day,Ttable.DatetimeColumn ,GETDATE()) = 1 -- yesterday
This can easily be changed to today, last month, last year, etc.
SELECT * from table_name where date_field = DATE_SUB(CURRENT_DATE(),INTERVAL 1 DAY);
Its a really old thread, but here is my take on it.
Rather than 2 different clauses, one greater than and less than. I use this below syntax for selecting records from A date. If you want a date range then previous answers are the way to go.
SELECT * FROM TABLE_NAME WHERE
DATEDIFF(DAY, DATEADD(DAY, X , CURRENT_TIMESTAMP), <column_name>) = 0
In the above case X will be -1 for yesterday's records
This should do it:
WHERE `date` = CURDATE() - INTERVAL 1 DAY
Can't test it right now, but:
select * from tablename where date >= dateadd(day, datediff(day, 1, getdate()), 0) and date < dateadd(day, datediff(day, 0, getdate()), 0)
In SQL Server do like this:
where cast(columnName as date) = cast(getdate() -1 as date)
You should cast both sides of the expression to date to avoid issues with time formatting.
If you need to control interval in more detail, then you should try something like:
declare #start datetime = cast(getdate() - 1 as date)
declare #end datetime = cast(getdate() - 1 as date)
set #end = dateadd(second, 86399, #end)
Another way to tell it "Yesterday"...
Select * from TABLE
where Day(DateField) = (Day(GetDate())-1)
and Month(DateField) = (Month(GetDate()))
and Year(DateField) = (Year(getdate()))
This conceivably won't work well on January 1, as well as the first day of every month. But on the fly it's effective.
Well, its easier to cast the datetime column to date and than compare.
SELECT * FROM TABLE_NAME WHERE cast(COLUMN_NAME as date) =
dateadd(day,0, convert(date, getdate(), 105))
A simple alternative
Select GETDATE() - 1
Change 1 to go back that many number of days
PS : This gives you timestamp accuracy.
This worked a charm:
SELECT * FROM mytable WHERE date(mydate) = DATE_SUB(CURDATE(), INTERVAL 1 DAY);
subdate(now(),1) will return yesterdays timestamp
The below code will select all rows with yesterday's timestamp
Select * FROM `login` WHERE `dattime` <= subdate(now(),1) AND `dattime` > subdate(now(),2)