PostgreSQL's date_trunc in mySQL - mysql

Recently, I have been getting familiar with PostgreSQL(using 8.2) and found the date_trunc function extremely useful for easily matching time stamps between certain days/months/etc.
The real usefulness of the function, I believe, comes from the fact that it keeps the output in the format of a timestamp.
I have had to switch to mySQL(5.0) and find some of the date functions rather lacking in comparison. The extract function seems useful and the date function I have found solves some of my problems, but is there any way to replicate PostgreSQL's date_trunc?
Following is an example of how I used to use date_trunc to match queried timestamps to only the last 4 months including the current month, but only if a week has passed into this month already:
WHERE date_trunc('month', QUERY_DATE) BETWEEN
date_trunc('month', now()) - INTERVAL '4 MONTH' AND
date_trunc('month', now() - INTERVAL '1 WEEK')
I have no idea how to recreate such a stipulation in mySQL. So, my question at the end of the day, is whether this type of query can be accomplished in mySQL by trying replicate date_trunc(and how) or whether I need to start looking at these types of queries in a different way to make them work in mySQL(and suggestions on how to do that)?

The extract function seems useful and the date function I have found solves some of my problems, but is there any way to replicate PostgreSQL's date_trunc?
Indeed, EXTRACT looks like it's going to be the closest match for this specific case.
Your original code in PG:
WHERE date_trunc('month', QUERY_DATE) BETWEEN
date_trunc('month', now()) - INTERVAL '4 MONTH' AND
date_trunc('month', now() - INTERVAL '1 WEEK')
Using EXTRACT:
WHERE EXTRACT(YEAR_MONTH FROM QUERY_DATE)
BETWEEN
EXTRACT(YEAR_MONTH FROM NOW() - INTERVAL 4 MONTH)
AND
EXTRACT(YEAR_MONTH FROM NOW() - INTERVAL 1 WEEK)
While it should be functionally identical, this is actually mangling the dates into a YYYYMM string before doing the comparison.
Another option would be using DATE_FORMAT to rebuild the date string and force it to the beginning of the month:
WHERE DATE_FORMAT(QUERY_DATE, '%Y-%m-01')
BETWEEN
DATE_FORMAT(NOW() - INTERVAL 4 MONTH, '%Y-%m-01')
AND
DATE_FORMAT(NOW() - INTERVAL 1 WEEK, '%Y-%m-01')
Also, be aware that MySQL is really poor at dealing with date ranges, even when the field is indexed. You're probably going to end up with a full table scan if you aren't careful.

late to the party, but...
there is a way to get truncated date given you know the interval. For example, if the interval is MONTH, you could get today's date (now()) truncated to the month using the following:
select date_add('1900-01-01', interval TIMESTAMPDIFF(MONTH, '1900-01-01', now()) MONTH);
Given the above, one could create a function to take care of the other intervals as well:
DELIMITER //
create function date_trunc(vInterval varchar(7), vDate timestamp)
returns timestamp
begin
declare toReturn timestamp;
if vInterval = 'year' then set toReturn = date_add('1900-01-01', interval TIMESTAMPDIFF(YEAR, '1900-01-01', vDate) YEAR);
elseif vInterval = 'quarter' then set toReturn = date_add('1900-01-01', interval TIMESTAMPDIFF(QUARTER, '1900-01-01', vDate) QUARTER);
elseif vInterval = 'month' then set toReturn = date_add('1900-01-01', interval TIMESTAMPDIFF(MONTH, '1900-01-01', vDate) MONTH);
elseif vInterval = 'week' then set toReturn = date_add('1900-01-01', interval TIMESTAMPDIFF(WEEK, '1900-01-01', vDate) WEEK);
elseif vInterval = 'day' then set toReturn = date_add('1900-01-01', interval TIMESTAMPDIFF(DAY, '1900-01-01', vDate) DAY);
elseif vInterval = 'hour' then set toReturn = date_add('1900-01-01', interval TIMESTAMPDIFF(HOUR, '1900-01-01', vDate) HOUR);
elseif vInterval = 'minute' then set toReturn = date_add('1900-01-01', interval TIMESTAMPDIFF(MINUTE, '1900-01-01', vDate) MINUTE);
END IF;
return toReturn;
end//
DELIMITER ;
Use it like so:
select date_trunc('quarter', now())

Here is a function that mimics postgres' DATE_TRUNC contract using the DATE_FORMAT mysql function that #Charles has recommended above.
DROP FUNCTION IF EXISTS DATE_TRUNC;
CREATE FUNCTION DATE_TRUNC(
in_granularity ENUM('hour', 'day', 'month', 'year'),
in_datetime datetime(6)
)
RETURNS datetime(6)
DETERMINISTIC
BEGIN
IF (in_granularity = 'hour') THEN
RETURN DATE_FORMAT(in_datetime, '%Y-%m-%d %H:00:00.0000');
END IF;
IF (in_granularity = 'day') THEN
RETURN DATE_FORMAT(in_datetime, '%Y-%m-%d 00:00:00.0000');
END IF;
IF (in_granularity = 'month') THEN
RETURN DATE_FORMAT(in_datetime, '%Y-%m-01 00:00:00.0000');
END IF;
IF (in_granularity = 'year') THEN
RETURN DATE_FORMAT(in_datetime, '%Y-01-01 00:00:00.0000');
END IF;
END;

Here are some MySQL equivalents for the most common Redshift/Postgres date_trunc expressions, using date arithmetic:
select
null
-- RS date_trunc('hour', now()) = '2023-01-13 17:00:00.000 +0100'
,date_add(date(now()), interval hour(now()) hour) as trunc_hour
-- RS date_trunc('week', now()) = '2023-01-09 00:00:00.000 +0100'
,date_sub(cast(date(now()) as datetime), interval weekday(now()) day) as trunc_week
-- RS date_trunc('month', now()) = '2023-01-01 00:00:00.000 +0100'
,date_add(cast(makedate(year(now()), 1) as datetime), interval month(now())-1 month) as trunc_month
-- RS date_trunc('year', now()) = '2023-01-01 00:00:00.000 +0100'
,cast(makedate(year(now()), 1) as datetime) as trunc_year

Related

How to find year and Month from no of days passed in sql

I have the amount of days passed , I want to find out the month and year for that particular day gap using big query SQL
For eg: I have the following fields : date_today as DATE , day_passed
as INTEGER
date_today = '2018-01-22'
day_passed = 32
From this day passed I want to create a new column like below using day_passed column value
month_joined = '2017-12'
Challenge: Months may have different days In Feb(28 or 29),30,31
What will be the best way to solve this problem?
In tsql you can use this query which I suggest you modify into a function.
declare #date nvarchar(20) = '2018-01-22'
declare #interval int = -32
declare #newDate datetime = DATEADD(DAY,#interval,CAST(#date as datetime))
select cast(year(#newDate) as nvarchar(4)) +'-'+ cast(month(#newDate) as nvarchar(2)) as 'month_joined'
To create into a function in tsql:
CREATE FUNCTION YearMonthConvert(
#Date datetime,
#Interval int = 0)
RETURNS NVARCHAR(10)
AS
BEGIN
DECLARE #newDate datetime = DATEADD(DAY,#Interval,CAST(#Date as datetime))
RETURN cast(year(#newDate) as nvarchar(4)) +'-'+ cast(month(#newDate) as nvarchar(2))
END;
then it can be called like this:
select dbo.yearmonthconvert('2018-01-22',-32)
I believe the mysql syntax is as follows - not done mysql so if anyone wants to correct me in the comments that would be much appreciated.
set #date = '2018-01-22';
set #interval = -32;
set month_joined = DATE_ADD(#date, INTERVAL #interval DAY);
Use mysql functons
DATE_SUB
or
DATE_ADD
SELECT DATE_SUB('2018-01-22', INTERVAL 32 DAY)
Result:
2017-12-21
Below is for BigQuery Standard SQL
It should give you an idea of how you can work with dates in BigQuery
#standardSQL
SELECT
date_today,
day_passed,
DATE_SUB(date_today, INTERVAL day_passed DAY) day_joined,
DATE_TRUNC(DATE_SUB(date_today, INTERVAL day_passed DAY), MONTH) month_joined_as_date,
FORMAT_DATE('%Y-%m', DATE_SUB(date_today, INTERVAL day_passed DAY)) month_joined_as_string
FROM `yourproject.yourdataset.yourtable`
you can test / play with above using dummy data from your question as below
#standardSQL
WITH `yourproject.yourdataset.yourtable` AS (
SELECT DATE '2018-01-22' date_today, 32 day_passed
)
SELECT
date_today,
day_passed,
DATE_SUB(date_today, INTERVAL day_passed DAY) day_joined,
DATE_TRUNC(DATE_SUB(date_today, INTERVAL day_passed DAY), MONTH) month_joined_as_date,
FORMAT_DATE('%Y-%m', DATE_SUB(date_today, INTERVAL day_passed DAY)) month_joined_as_string
FROM `yourproject.yourdataset.yourtable`
with result
date_today day_passed day_joined month_joined_as_date month_joined_as_string
2018-01-22 32 2017-12-21 2017-12-01 2017-12
I solved it in a simple way in bigquery
SUBSTR(STRING(DATE_ADD(DATE(date_today), -day_passed, "DAY")),1,7) AS month_joined

SQL: DATE_ADD(date,INTERVAL expr type) skip weekends

I'm currently using DATE_ADD(date,INTERVAL expr type) to set a due date as a trigger in a mySQL Database.
What I'm wanting to know is if it is possible to skip weekends (Saturday, Sunday) as part of the trigger.
You'd have to create an own function for doing that. You can look how to do that in this answer, for example (just use function instead of procedure). As for how to write such a function, here's a working algorithm. The code is quite straightforward: it loops through days and skips weekends.
CREATE FUNCTION `DAYSADDNOWK`(addDate DATE, numDays INT) RETURNS date
BEGIN
IF (WEEKDAY(addDate)=5) THEN
SET addDate=DATE_ADD(addDate, INTERVAL 1 DAY);
END IF;
IF (WEEKDAY(addDate)=6) THEN
SET addDate=DATE_ADD(addDate, INTERVAL 1 DAY);
END IF;
WHILE numDays>0 DO
SET addDate=DATE_ADD(addDate, INTERVAL 1 DAY);
IF (WEEKDAY(addDate)=5) THEN
SET addDate=DATE_ADD(addDate, INTERVAL 1 DAY);
END IF;
IF (WEEKDAY(addDate)=6) THEN
SET addDate=DATE_ADD(addDate, INTERVAL 1 DAY);
END IF;
SET numDays=numDays-1;
END WHILE;
RETURN addDate;
END
Currently SELECT DAYSADDNOWK(CURDATE(), 5) yields 2016-03-07, which is correct.
Of course you only can use it with days, so no arbitrary interval, but your question mentioned date datatype, and I don't quite see how one could add a month not counting working days.
This function simply creates a list of dates starting at the date given in the arguments, and then figures out which date is x number of days (the interval) out while disregarding days 1 and 7 (which are Sunday and Saturday respectively on SQL Server).
CREATE FUNCTION [dbo].[udf_days_add_no_wknd]
(
#start_date date
, #interval int
)
RETURNS date
AS
BEGIN
declare #answer date
; with dates as
(
select #start_date as date_val
union all
select dateadd(d, 1, date_val) as date_val
from dates
where date_val < dateadd(d, #interval * 10, #start_date)
)
, final as
(
select top 1 lead(ld.date_val, #interval, NULL) over (order by ld.date_val asc) as new_date_val
from dates as ld
where 1=1
and datepart(dw, ld.date_val) not in (1,7) --eliminating weekends
)
select #answer = (select new_date_val from final)
return #answer
END
It is worth nothing that this solution is dependent on having SQL Server 2012 or later, considering the use of the lead() function.

how Selecting Birthdays 7 Days Before Today in mysql

i have date field and i query for selecting Birthday before 7 days in mysql.
for example
if
birth = 1986-08-05
if now is 2012-07-30 this query alarm me.
or
birth = 1986-01-05
if now is 2012-12-30 this query alarm me.
birth is field of user_table.
select * from user_table
where date_format(date_sub(birth, interval 7 days), "%m-%d")
= date_format(now(), "%m-%d")
or date_format(date_sub(birth, interval 7 days), "%m-%d") = '02-29'
and month(now()) = 2 and month(date_add(now(), interval 1 day)) = 3
i find my select for this goal.
select *,birthdate,
concat(if(date_format(birthdate, '%m') = '12',date_format(curdate(), "%Y")
,date_format(now(), "%Y")),
date_format(date_add(curdate(), interval 7 day), '%Y')) as birthday
from users
HAVING birthday BETWEEN curdate() and date_add(curdate(), interval 7 day)
thanks from your help.
Use SQL functions if you want reusable code and want to keep your SQL easy to read and maintain. A birthday is an anniversary, so...
DROP FUNCTION IF EXISTS anniversary_after;
DELIMITER $$
CREATE FUNCTION anniversary_after(anydate DATE, after DATE)
RETURNS DATE DETERMINISTIC
BEGIN
DECLARE anniversary DATE;
DECLARE years INTEGER;
SET years = YEAR(after) - YEAR(anydate);
SET anniversary = DATE_ADD(anydate, INTERVAL years YEAR);
IF anniversary < after THEN
SET anniversary = DATE_ADD(anniversary, INTERVAL 1 YEAR);
END IF;
RETURN anniversary;
END
$$
DELIMITER ;
DROP FUNCTION IF EXISTS anniversary;
CREATE FUNCTION anniversary(anydate DATE)
RETURNS DATE DETERMINISTIC
RETURN anniversary_after(anydate, CURRENT_DATE());
Show date of next anniversary:
SELECT anniversary('1994-04-05');
Show days until next anniversary:
SELECT DATEDIFF(anniversary('1994-04-05'), CURRENT_DATE());
something like this?
SELECT user_name, DATE_SUB(birth, INTERVAL 7 DAYS) as SevenDaysBefore from user_table

How can I store a MySQL interval type?

This is what I would like to be able to do:
SET #interval_type := MONTH;
SELECT '2012-01-01' + INTERVAL 6 #interval_type;
+------------+
|'2012-06-01'|
+------------+
And of course that doesn't work and there is no "interval" data type in MySQL.
I want to be able to store an interval value and an interval type in a table so that i can have the database quickly do the math naturally without having to write a big switch statement, ala
... ELSE IF (type = 'MONTH') { SELECT #date + INTERVAL #value MONTH; } ...
Is this supported in any way in MySQL or do you have a clever hack for this?
Thanks; you rock.
This solution may come handy to somebody implementing the job queue for cron or something similar.
Let us suppose we have a reference date (DATETIME) and interval of repetition. We would like to store both values in database and get the quick comparison whether it's already time to execute and include job into execution queue or not.
The interval could be non trivial e.g. (1 YEAR 12 DAYS 12 HOUR) and is controlled by wise user (admin) so that user is not going to use values exceeding the range of regular DATETIME data type or otherwise the conversion must be implemented first. (18 MONTH -> 1 YEAR 6 MONTH).
We can use then DATETIME data type for storing both values reference date and interval. We can define stored function using:
DELIMITER $$
CREATE DEFINER=`my_db`#`%` FUNCTION `add_interval`(`source` DATETIME, `interval` DATETIME) RETURNS datetime
BEGIN
DECLARE result DATETIME;
SET result = `source`;
SET result=DATE_ADD(result, INTERVAL EXTRACT(YEAR FROM `interval`) YEAR);
SET result=DATE_ADD(result, INTERVAL EXTRACT(MONTH FROM `interval`) MONTH);
SET result=DATE_ADD(result, INTERVAL EXTRACT(DAY FROM `interval`) DAY);
SET result=DATE_ADD(result, INTERVAL EXTRACT(HOUR FROM `interval`) HOUR);
SET result=DATE_ADD(result, INTERVAL EXTRACT(MINUTE FROM `interval`) MINUTE);
SET result=DATE_ADD(result, INTERVAL EXTRACT(SECOND FROM `interval`) SECOND);
RETURN result;
END
We can then make DATETIME arithmetic using this function e.g.
// test solution
SELECT add_interval('2014-07-24 15:58:00','0001-06-00 00:00:00');
// get job from schedule table
SELECT job FROM schedule WHERE add_interval(last_execution,repetition)<NOW();
// update date of executed job
UPDATE schedule SET last_execution=add_interval(last_execution,repetition);
You can solve this problem using prepared statements, considering there is no language construct available for use. The benefit here being you get the performance and flexibility that you want; this could easily be placed in a stored procedure or function for added value:
SET #date = '2012-01-01';
SET #value = 6;
SET #type = 'MONTH';
SET #q = 'SELECT ? + INTERVAL ? ';
SET #q = CONCAT(#s, #type);
PREPARE st FROM #q;
EXECUTE st USING #date, #value;
Alternatively, depending on your database / software architecture and the type of date/time intervals you are thinking of, you could simply this problem by using a time-scale interval:
SELECT #date + INTERVAL #value SECOND
1 second - 1
1 minute - 60
1 hour - 3600
1 day - 86400 (24 hours)
1 week - 604800 (7 days)
1 month - 2419200 (4 weeks)
Here's the simplistic approach. It works reasonably fast. You can change the order of the switch statements to optimize for speed if you feel that you will be hitting some more often then others. I have not benched this against Chris Hutchinson's solution. I ran into problems trying to wrap it into a nice function because of the dynamic SQL. Anyway, for posterity, this is guaranteed to work:
CREATE FUNCTION AddInterval( date DATETIME, interval_value INT, interval_type TEXT )
RETURNS DATETIME
DETERMINISTIC
BEGIN
DECLARE newdate DATETIME;
SET newdate = date;
IF interval_type = 'YEAR' THEN
SET newdate = date + INTERVAL interval_value YEAR;
ELSEIF interval_type = 'QUARTER' THEN
SET newdate = date + INTERVAL interval_value QUARTER;
ELSEIF interval_type = 'MONTH' THEN
SET newdate = date + INTERVAL interval_value MONTH;
ELSEIF interval_type = 'WEEK' THEN
SET newdate = date + INTERVAL interval_value WEEK;
ELSEIF interval_type = 'DAY' THEN
SET newdate = date + INTERVAL interval_value DAY;
ELSEIF interval_type = 'MINUTE' THEN
SET newdate = date + INTERVAL interval_value MINUTE;
ELSEIF interval_type = 'SECOND' THEN
SET newdate = date + INTERVAL interval_value SECOND;
END IF;
RETURN newdate;
END //
It comes with this equally simplistic benchmark test:
CREATE FUNCTION `TestInterval`( numloops INT )
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE date DATETIME;
DECLARE newdate DATETIME;
DECLARE i INT;
SET i = 0;
label1: LOOP
SET date = FROM_UNIXTIME(RAND() * 2147483647);
SET newdate = AddInterval(date,1,'YEAR');
SET i = i+1;
IF i < numloops THEN
ITERATE label1;
ELSE
LEAVE label1;
END IF;
END LOOP label1;
return i;
END //

Difference between two dates in MySQL

How to calculate the difference between two dates, in the format YYYY-MM-DD hh: mm: ss and to get the result in seconds or milliseconds?
SELECT TIMEDIFF('2007-12-31 10:02:00','2007-12-30 12:01:01');
-- result: 22:00:59, the difference in HH:MM:SS format
SELECT TIMESTAMPDIFF(SECOND,'2007-12-30 12:01:01','2007-12-31 10:02:00');
-- result: 79259 the difference in seconds
So, you can use TIMESTAMPDIFF for your purpose.
If you are working with DATE columns (or can cast them as date columns), try DATEDIFF() and then multiply by 24 hours, 60 min, 60 secs (since DATEDIFF returns diff in days). From MySQL:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
for example:
mysql> SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30 00:00:00') * 24*60*60
Get the date difference in days using DATEDIFF
SELECT DATEDIFF('2010-10-08 18:23:13', '2010-09-21 21:40:36') AS days;
+------+
| days |
+------+
| 17 |
+------+
OR
Refer the below link
MySql difference between two timestamps in days?
SELECT TIMESTAMPDIFF(HOUR,NOW(),'2013-05-15 10:23:23')
calculates difference in hour.(for days--> you have to define day replacing hour
SELECT DATEDIFF('2012-2-2','2012-2-1')
SELECT TO_DAYS ('2012-2-2')-TO_DAYS('2012-2-1')
select
unix_timestamp('2007-12-30 00:00:00') -
unix_timestamp('2007-11-30 00:00:00');
If you want to add where clause with DATEDIFF then it is also possible to add where clause or condition.
Take a look of following example.
select DATEDIFF(now(), '2022-08-12 17:55:51.000000') from properties p WHERE p.property_name = 'KEY';
Result : 6
SELECT TIMESTAMPDIFF(SECOND,'2018-01-19 14:17:15','2018-01-20 14:17:15');
Second approach
SELECT ( DATEDIFF('1993-02-20','1993-02-19')*( 24*60*60) )AS 'seccond';
CURRENT_TIME() --this will return current Date
DATEDIFF('','') --this function will return DAYS and in 1 day there are 24hh 60mm 60sec
Or, you could use TIMEDIFF function
mysql> SELECT TIMEDIFF('2000:01:01 00:00:00', '2000:01:01 00:00:00.000001');
'-00:00:00.000001'
mysql> SELECT TIMEDIFF('2008-12-31 23:59:59.000001' , '2008-12-30 01:01:01.000002');
'46:58:57.999999'
This function takes the difference between two dates and shows it in a date format yyyy-mm-dd. All you need is to execute the code below and then use the function. After executing you can use it like this
SELECT datedifference(date1, date2)
FROM ....
.
.
.
.
DELIMITER $$
CREATE FUNCTION datedifference(date1 DATE, date2 DATE) RETURNS DATE
NO SQL
BEGIN
DECLARE dif DATE;
IF DATEDIFF(date1, DATE(CONCAT(YEAR(date1),'-', MONTH(date1), '-', DAY(date2)))) < 0 THEN
SET dif=DATE_FORMAT(
CONCAT(
PERIOD_DIFF(date_format(date1, '%y%m'),date_format(date2, '%y%m'))DIV 12 ,
'-',
PERIOD_DIFF(date_format(date1, '%y%m'),date_format(date2, '%y%m'))% 12 ,
'-',
DATEDIFF(date1, DATE(CONCAT(YEAR(date1),'-', MONTH(DATE_SUB(date1, INTERVAL 1 MONTH)), '-', DAY(date2))))),
'%Y-%m-%d');
ELSEIF DATEDIFF(date1, DATE(CONCAT(YEAR(date1),'-', MONTH(date1), '-', DAY(date2)))) < DAY(LAST_DAY(DATE_SUB(date1, INTERVAL 1 MONTH))) THEN
SET dif=DATE_FORMAT(
CONCAT(
PERIOD_DIFF(date_format(date1, '%y%m'),date_format(date2, '%y%m'))DIV 12 ,
'-',
PERIOD_DIFF(date_format(date1, '%y%m'),date_format(date2, '%y%m'))% 12 ,
'-',
DATEDIFF(date1, DATE(CONCAT(YEAR(date1),'-', MONTH(date1), '-', DAY(date2))))),
'%Y-%m-%d');
ELSE
SET dif=DATE_FORMAT(
CONCAT(
PERIOD_DIFF(date_format(date1, '%y%m'),date_format(date2, '%y%m'))DIV 12 ,
'-',
PERIOD_DIFF(date_format(date1, '%y%m'),date_format(date2, '%y%m'))% 12 ,
'-',
DATEDIFF(date1, DATE(CONCAT(YEAR(date1),'-', MONTH(date1), '-', DAY(date2))))),
'%Y-%m-%d');
END IF;
RETURN dif;
END $$
DELIMITER;
select TO_CHAR(TRUNC(SYSDATE)+(to_date( '31-MAY-2012 12:25', 'DD-MON-YYYY HH24:MI')
- to_date( '31-MAY-2012 10:37', 'DD-MON-YYYY HH24:MI')),
'HH24:MI:SS') from dual
-- result : 01:48:00
OK it's not quite what the OP asked, but it's what I wanted to do :-)
This code calculate difference between two dates in yyyy MM dd format.
declare #StartDate datetime
declare #EndDate datetime
declare #years int
declare #months int
declare #days int
--NOTE: date of birth must be smaller than As on date,
--else it could produce wrong results
set #StartDate = '2013-12-30' --birthdate
set #EndDate = Getdate() --current datetime
--calculate years
select #years = datediff(year,#StartDate,#EndDate)
--calculate months if it's value is negative then it
--indicates after __ months; __ years will be complete
--To resolve this, we have taken a flag #MonthOverflow...
declare #monthOverflow int
select #monthOverflow = case when datediff(month,#StartDate,#EndDate) -
( datediff(year,#StartDate,#EndDate) * 12) <0 then -1 else 1 end
--decrease year by 1 if months are Overflowed
select #Years = case when #monthOverflow < 0 then #years-1 else #years end
select #months = datediff(month,#StartDate,#EndDate) - (#years * 12)
--as we do for month overflow criteria for days and hours
--& minutes logic will followed same way
declare #LastdayOfMonth int
select #LastdayOfMonth = datepart(d,DATEADD
(s,-1,DATEADD(mm, DATEDIFF(m,0,#EndDate)+1,0)))
select #days = case when #monthOverflow<0 and
DAY(#StartDate)> DAY(#EndDate)
then #LastdayOfMonth +
(datepart(d,#EndDate) - datepart(d,#StartDate) ) - 1
else datepart(d,#EndDate) - datepart(d,#StartDate) end
select
#Months=case when #days < 0 or DAY(#StartDate)> DAY(#EndDate) then #Months-1 else #Months end
Declare #lastdayAsOnDate int;
set #lastdayAsOnDate = datepart(d,DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#EndDate),0)));
Declare #lastdayBirthdate int;
set #lastdayBirthdate = datepart(d,DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,#StartDate)+1,0)));
if (#Days < 0)
(
select #Days = case when( #lastdayBirthdate > #lastdayAsOnDate) then
#lastdayBirthdate + #Days
else
#lastdayAsOnDate + #Days
end
)
print convert(varchar,#years) + ' year(s), ' +
convert(varchar,#months) + ' month(s), ' +
convert(varchar,#days) + ' day(s) '
If you've a date stored in text field as string you can implement this code it will fetch the list of past number of days a week, a month or a year sorting:
SELECT * FROM `table` WHERE STR_TO_DATE(mydate, '%d/%m/%Y') < CURDATE() - INTERVAL 30 DAY AND STR_TO_DATE(date, '%d/%m/%Y') > CURDATE() - INTERVAL 60 DAY
//This is for a month
SELECT * FROM `table` WHERE STR_TO_DATE(mydate, '%d/%m/%Y') < CURDATE() - INTERVAL 7 DAY AND STR_TO_DATE(date, '%d/%m/%Y') > CURDATE() - INTERVAL 14 DAY
//This is for a week
%d%m%Y is your date format
This query display the record between the days you set there like: Below from last 7 days and Above from last 14 days so it would be your last week record to be display same concept is for month or year. Whatever value you're providing in below date like: below from 7-days so the other value would be its double as 14 days. What we are saying here get all records above from last 14 days and below from last 7 days. This is a week record you can change value to 30-60 days for a month and also for a year.
Thank You Hope it will help someone.
You would simply do this:
SELECT (end_time - start_time) FROM t; -- return in Millisecond
SELECT (end_time - start_time)/1000 FROM t; -- return in Second
Why not just
Select Sum(Date1 - Date2) from table
date1 and date2 are datetime