How I can calculate a date in the future based on the date of a field?
For example, I have a field in my dataset called SPS_START_DATE and I need to create a second field which is 20 weeks on from this date. What SQL would be required to calculate the date 20 weeks into the future?
Anyway, use DATEADD
SELECT
DATEADD(week, 20, [SPS_START_DATE])
FROM
[My DataSet];
DATEADD() is perfect for this.
For example:
SELECT GETDATE() AS DateTime, --Get the date/time from today
CAST(GETDATE() AS DATE) AS DateOnly, --Cast date/time value as date only
DATEADD(wk, 20, GETDATE()) AS TwentyWeeksDateTime, --Get the date/time of 20 weeks from now
CAST(DATEADD(wk, 20, GETDATE()) AS DATE) AS TwentyWeeksDate --Cast date/time value as date only
Related
I need to get last day of the month with time like
2023-01-31 23:59:59:000000
I'm able to get only the last day of the month with time stamp as
2023-01-31 00:00:00:000000
As jarlh said your best method is to add a day to the end of the month, then subtract a second (although if you really want the absolute maximum time I think you'd want to subtract 3 milliseconds).
EOMONTH -> Add 1 day -> Cast as datetime -> remove 1 second / 3 milliseconds. You have to cast as datetime because the EOMONTH function implicitly casts to a date
The code will be something like this:
SELECT DATEADD(SECOND, -1, CAST(DATEADD(DAY, 1, EOMONTH(#currentDate)) AS DATETIME))
SELECT DATEADD(MILLISECOND, -3, CAST(DATEADD(DAY, 1, EOMONTH(#currentDate)) AS DATETIME))
There are already similar questions with a lot of answers. You should find your anwer for sure:
Get the last day of the month in SQL
SQL query to display end date of current month
DECLARE #currentDate DATE = GETDATE()
SELECT EOMONTH (#currentDate) AS CurrentMonthED
SQL query to display end date of Next month
DECLARE #currentDate DATE = GETDATE()
SELECT EOMONTH (#currentDate, 1 ) AS NextMonthED
This is what my query's date range is:
WHERE
date BETWEEN 20190101 AND [here date should come as last year YTD -1]
For example if we use this query today (20201106) (format:yyyymmdd), then the 2nd date should be 20191105.
For more clarity: when I run this query today (20201106) my query should fetch results from date range:
WHERE
date BETWEEN 20190101 AND 20191105
When I run this query tomorrow (20201107), my query should fetch results from the date range:
WHERE
date BETWEEN 20190101 AND 20191106
How can I do this?
Can anyone help?
you can use dateadd() with getdate()
DATEADD(year,-1, GETDATE())
or other function that can get current database time but this should work.
also remember to use convert() to modify the date format into what you want.
WHERE
DATEADD(YEAR,-1, GETDATE()) -- One Day Before Today in the Last Year
AND
DATEADD(yy, DATEDIFF(yy, 0, DATEADD(YEAR,-1, GETDATE())), 0) AS StartOfYear -- The Day 1 of the Last Year
I would phrase this as:
where date >= datefromparts(year(getdate()) - 1, 1, 1)
and date < dateadd(year, -1, convert(date, getdate()))
This filters from the start of last year and the 1 year and 1 day ago.
Note that I changed the filtering strategy to use half-open intervals rather than between. This is somehow more flexible, as it would properly handle a time component in in date, if any, and simplifies the offsetting logic.
Note also that getdate() has a time component, that needs to be removed while offsetting to implement your logic.
I need to calculate a future date by subtracting a date from another date. The catch is the date field I am subtracting has many days of the month...i.e. 10/27/2020 or 11/30/2020. What I need to do is, no matter what the day of the month is, I need to subtract the month as if it were the first day of the month no matter what it may be.
So for example I have a date 12/31/2020 I need to subtract another date 10/23/2020 from that date but I need to default 10/23/2020 to 10/01/2020 and same for any other date I would subtract from the original date. Any ideas?
You can get first day of the month and then use datediff to get the date difference.
DECLARE #table table(fromdate date,todate date)
insert into #table
VALUES ('2020-10-27','2020-12-31')
SELECT datediff(day, DATEADD(month, DATEDIFF(month, 0, fromdate), 0),todate) AS DifferenceDays
from #table
+----------------+
| DifferenceDays |
+----------------+
| 91 |
+----------------+
Not really sure what you are really asking. But I'm assuming that you want to get the date gap or difference between 2 dates. But, before that, you want to set a default day for the first date.
You can set up the first date to have day 1, then use DATEDIFF to know the gap / difference.
-- source date
declare #date date = '10/27/2020'
declare #dateSubtract date = '10/23/2020'
-- break the date, take month and year only
declare #month int = DATEPART(MONTH, #date)
declare #year int = DATEPART(YEAR, #date)
-- reconstruct the date with default day=1
select #date = CAST((STR(#month) + '/01/' + STR(#year)) as date)
-- get the calculation
select DATEDIFF(DAY, #date, #dateSubtract)
The result will be (in Days),
22
You can change the DATEDIFF parameter to MONTH or YEAR.
#UPDATE 1:
As mentioned by Alex in comment below, you can reconstruct the date using DATEFROMPARTS which more safer. Because casting from string may causing a confusion of date format.
The number of rows in the A table, where the date value of the timestamps in the [time_created] and / or [time_updated] fields correspond to the current day's date
You can use the CONVERT() function.
CONVERT(date, YOUR-COL-NAME-HERE)
So if you are looking for rows with today's date, you should have something like:
WHERE CONVERT(date, YOUR-COL-NAME-HERE) = CONVERT(date, GETDATE())
GETDATE() returns the current date as a datetime. You can use convert to extract the date part from it:
SELECT *
FROM mytable
WHERE CONVERT (DATE, GETDATE()) IN
(CONVERT(DATE, time_created), CONVERT(DATE, time_updated))
I want to get the previous month relative to the current date
SELECT datediff(mm,-1,2-2-2011)
This query gives 67 which is a wrong value .. where i went wrong ?
You can use DATEADD
eg.
SELECT DATEADD(month, -1, GETDATE())
This 2-2-2011 is not a valid date literal - you are subtracting 2 from 2 and then 2011 from the result - proper date literals are '2-2-2011' and #2-2-2011#. You can use GETDATE() to get the current date, instead of relying on a literal.
Nor should you be using DATEDIFF - it gives you the difference between dates.
You should be using DATEADD to calculate new dates.
Try this:
SELECT DATEADD(mm,-1, GETDATE())
This will get the date a month ago.
If you just want the month, you need to also use DATEPART:
SELECT DATEPART(mm, SELECT DATEADD(mm,-1, GETDATE()))
SELECT datepart(mm, dateadd(mm,-1,'2011/1/1') )
If you want the month before the current month, you want
SELECT MONTH(DATEADD(mm, -1, GETDATE()))
If you want the date for a month before the current date, you want
SELECT DATEADD(mm, -1, GETDATE())
BTW, SELECT datediff(mm,-1,2-2-2011) computes the number of months between day -1 and day -2011, which is 67 (2010 / 30). That's nowhere near what you seem to actually want.
You need to use DATEADD - not DATEDIFF
DATEDIFF calculates the difference between two dates - it doesn't add day or months to an existing date....
Also, you need to put your dates into single quotes: use '2-2-2011' instead of simply 2-2-2011.
And lastly: I would strongly recommend using the ISO-8601 date format YYYYMMDD (here: 20110202) - it will work regardless of the language and regional settings on your SQL Server - your date format will BREAK on many servers due to language settings.
DATEDIFF calculates the difference between your stating and ending dates
every date previous to the current date has a positive number and every date next to the current date has negative number, this works in geting your specific date weather it a day,month,year or hour to understand this better below is the syntax of datediff
DATEDIFF (your datetime type, your starting date,your ending date)
the function does (your ending date)-(your starting date)
in your case the below datediff will work just pefectly
SELECT DATEDIFF (month,[you_date_or_datetime_column],GETDATE()) = 1
You can use DATEADD try this code
For previous month
SELECT DATEADD(month, -1, GETDATE())
For 7 day previous
$date = date('Y-m-d',strtotime("-7 days"));
SELECT * FROM users WHERE `date` LIKE '%$date%'
$date varibale get previous date date