I have a db with value that I convert to date
CONVERT (VARCHAR(11),DATEADD(day,wo_date,'1971/12/31'),106) AS Date
and I am trying to have 7 days without today. I am ok to get last 7 days with today by using below code
SELECT datediff(day,'DEC 31 1971',getdate())-7
but I can not get rolling last week ie yesterday (Thursday) to - 8 (Wendsday)?
Is this what you are looking for?
DATEDIFF ( datepart , startdate , enddate )
SELECT DATEDIFF(DAY,GETDATE()-9,GETDATE() -1)
--verify
SELECT GETDATE()-9 --2018-01-24 LAST WEDNESDAY
SELECT GETDATE()-1 --2018-02-01 YESTERDAY
Brain fart! Found the solution in rather easy way.
wo_date > (SELECT datediff(day,'JAN 01 1972',getdate())-7)
AND wo_date <= (SELECT datediff(day,'JAN 01 1972',getdate()))
Thanks again for inspirations!
Related
how to get data in pass 90 days from giving week.
I have a giving week YEARWEEK("2020-04-12")
when I query like this
SELECT * FROM my_table
where date_colum < DATE_SUB(YEARWEEK("2020-04-12"), INTERVAL 90 DAY)
I got result null.
Please help. Thanks!
I need to show the week number of month in mysql and currently I can get the week number of year when executing this query. I'm retrieving results per week and need to display the week number (as 1st week of January, 2nd week of January. 3rd week of January, etc). Can any body please help me to do this?
SELECT d.draftID_PK,
d.clientID_FK,
count(d.quoteNr) AS totalquote,
DATE_FORMAT (d.draftDate,'%u %M %Y') as draftDate,
c.clientName
FROM draft d
INNER JOIN client c ON c.clientID_PK = d.clientId_FK
WHERE d.draftDate<CURDATE() AND d.draftDate>'2013-01-05'
AND c.clientName = '{$client_name}'
GROUP BY DATE_FORMAT (d.draftDate,'%u')
ORDER BY d.draftDate DESC
I made something, maybe this can help you but the number of weeks varies according to the week begins, example:
Here in Brazil, the week starts in Sunday. So take a look if this can solve your problem.
SET #date:='2014-08-31';
SELECT
#first := date_add(
date_add(
LAST_DAY(#date),
interval 1 DAY
),
interval -1 MONTH
) AS first_day,
#date,
-- EXAMPLE 1
WeekofYear(#date),
WeekofYear(#first),
WeekofYear(#date)-(WeekofYear(#first)-1) Num_Week,
-- EXAPLE 2 - Works in Brazil perfectly
Week(#date),
Week(#first),
Week(#date)-(Week(#first)-1) Num_Week_Brazil
I have a table having two fields start_date and end_date. I need to display the data in this format
4 days 12 hours remaining.
id start_date end_date
1 2012-12-07 12:00:00 2012-12-14 12:00:00
How can i achieve this. Using now() of mysql to find remaing days and hours. I mean today is 2012-12-10 so i need to find how much difference from today to the end date.
This might help you:
SELECT
FLOOR(HOUR(t) / 24) AS days,
HOUR(t) % 24 hours
FROM (
SELECT TIMEDIFF('2012-12-07 12:00:00', '2012-12-14 18:00:00') t
) AS t1
In the nested query you get the difference (in hours)
Please check if this helps ::
Try TIME_TO_SEC(TIMEDIFF())
select
TIME_TO_SEC(TIMEDIFF(end_date, start_date))/86400 as days,
TIME_TO_SEC(TIMEDIFF(end_date, start_date))/3600- TIME_TO_SEC(TIMEDIFF(end_date, start_date))/86400 as hours
from
myTable
In My application have to find upcoming birthday members , so I have to find the birthday between current date and current date +15 days.
I tried this query
Select name from tname
where dayofyear(birthday)-dayofyear(now()) between 0 and 15
this query is not working when the current date reaches end of year
Please provide me the correct way.
Try this query -
SELECT
name
FROM
tname
WHERE
birthday + INTERVAL YEAR(CURDATE()) - YEAR(birthday) YEAR
BETWEEN (CURDATE()) AND CURDATE() + INTERVAL 15 DAY;
select *
from tname
where DATEDIFF(MAKEDATE(YEAR(now()),DAYOFYEAR(birthday)),now()) BETWEEN 0 and 15
this may help you..,this will the short method.
I'm trying to query through historical data and I need to return data just from a 1 month period: 2 weeks back and 2 weeks forward,but I need the year to not matter.
So, if I was to make the query today I would want all rows with date between xxxx-06-31 and xxxx-07-27
Thanks in advance for the help!
EDIT:
I've tried two ways. both of which I believe will not work around the new year. One is to use datepart(day) and the other would be to simply take the year off of date and compare.
The best way to think of this problem is to convert your dates to a number between 0 and 365 corresponding to the day in the year. Then simply choosing dates where this difference is less than 14 gives you your two week window.
That will break down at the beginning or end of the year. But simple modular arithmetic gives you the answer.
Fortunately, MySQL has DAYOFYEAR(date), so it's not so complicated:
SELECT * FROM tbl t
WHERE
MOD(DAYOFYEAR(currdate) - DAYOFYEAR(t.the_date) + 365, 365) <= 14
OR MOD(DAYOFYEAR(t.the_date) - DAYOFYEAR(currdate) + 365, 365) <= 14
That extra + 365 is needed since MySQL's MOD will return negative numbers.
This answer doesn't account for leap years correctly. If the current year is not a leap year and the currdate is within 14 days of the end of the year, then you'll miss one day in Jan that you should have included. If you care about that, then you should replace 365 with [the number of days in the year - 1].
Supposed you have a date like this,
create table datelist
(
d date
);
insert into datelist values
('2012-07-01'),
('2011-06-29'),
('2012-07-02'),
('2010-07-05'),
('2012-05-31'),
('2010-06-30');
Try this query below,
SELECT d, date_format(d,'%Y-%b-%d')
FROM datelist
WHERE (MONTH(d) = 6 AND DAYOFMONTH(d) >= 30)
OR (MONTH(d) = 7 AND DAYOFMONTH(d) <= 27)
SQLFiddle Demo
Is it OK if the solution is terribly slow?
SELECT tbl.*
FROM tbl
INNER JOIN (SELECT COALESCE(DATE(CONCAT(yyyy, '-', MONTH(CURRENT_DATE), '-', DAYOFMONTH(CURRENT_DATE)),
DATE(CONCAT(yyyy, '-02-28'))) AS midpoint
FROM (SELECT DISTINCT(YEAR(d)) AS yyyy
FROM tbl) all_years) adjusted
ON tbl.datecol BETWEEN adjusted.midpoint - INTERVAL 2 WEEK
AND
adjusted.midpoint + INTERVAL 2 WEEK;
That computes all midpoints for all years in the data set, and then pulls records +- 2 weeks from any such midpoint, which handles end-of-year wrapping.
The COALESCE handles 02-29 on years without leapday (which MySQL will NULL-ify), forcing it down to 02-28.