Insert into MySQL table all Sundays and Saturdays from current year - mysql

I have this tiny table with just "fecha" field in it.
I need a MySQL query that inserts every sunday and saturday of the year to the table.
My own research got me to the point where i know i need to do something like this:
DECLARE diaRecorrido date();
SET diaRecorrido = date(now());
WHILE DATEPART(WEEKDAY,diaRecorrido) = 1 or DATEPART(WEEKDAY,diaRecorrido) = 7
BEGIN
INSERT INTO feriados (fecha)
VALUES (diaRecorrido)
IF diaRecorrido=2017/01/01
THEN
LEAVE lazo;
END IF;
END;
Any guidance is much apreciated!

I think, you should use DAYOFWEEK()
create PROCEDURE generateSundayAndSaturday()
BEGIN
DECLARE _now DATETIME;
DECLARE _endYear DATETIME;
SET _endYear = DATE_FORMAT(NOW() + INTERVAL 1 YEAR ,'%Y-01-01');
SELECT now() into _now from dual;
while _now < _endYear DO
if DAYOFWEEK(_now) = 7 THEN -- Saturday
-- insert into
SET _now = _now + INTERVAL 1 DAY;
ELSEIF DAYOFWEEK(_now) = 1 THEN -- Sunday
-- insert into
SET _now = _now + INTERVAL 6 DAY;
ELSE
SET _now = _now + INTERVAL 1 DAY;
END IF;
END WHILE;
END;

You could create a numbers table and then select from it the days of the year which you wish to keep:
DECLARE #Date1 DATE, #Date2 DATE
SET #Date1 = '20160101'
SET #Date2 = '20161231'
INSERT INTO feriados
SELECT DATEADD(DAY,number+1,#Date1) [Date]
FROM master..spt_values
WHERE type = 'P' AND
DATEADD(DAY,number+1,#Date1) < #Date2 AND
(DATEPART(WEEKDAY, DATEADD(DAY,number+1,#Date1)) = 1 OR
DATEPART(WEEKDAY, DATEADD(DAY,number+1,#Date1)) = 7)

This is MS SQL (t-sql) query. I think you can fix small syntax differences for MySQL.
declare #thisYear datetime=cast(year(getdate()) as varchar)+'-01-01',
#newyear datetime=cast(year(getdate())+1 as varchar)+'-01-01'
declare #frstSat datetime, #frstSun datetime
select #frstSat= dateadd(dd,7-DATEPART(weekday,#thisYear),#thisYear) ,
#frstSun= case DATEPART(weekday,#thisYear)
when 1 then #thisYear --if 1/1 is Sunday
else dateadd(dd,8-DATEPART(weekday,#thisYear),#thisYear) end
;with sat as (
select #frstSat dt,'Sat' WkDay
union all
select dateadd(dd,7,dt),'Sat' WkDay
from sat
where dateadd(dd,7,dt)<#newyear
)
,sun as (
select #frstSun dt,'Sun' WkDay
union all
select dateadd(dd,7,dt),'Sun' WkDay
from sun
where dateadd(dd,7,dt)<#newyear
)
--insert mytable
select * from sat
union
select * from sun
order by dt

Related

MYSQL Syntax Error, unexpected while_sym, expecting END_OF_INPUT or ';'

I'm trying to create an archiving event, and what I came up with look like this:
SET #Date = NULL;
SET #SEDOL = NULL;
CREATE EVENT IF NOT EXISTS Archive_Event
ON SCHEDULE EVERY 1 DAY
DO
SELECT #Date = Date,#SEDOL = SEDOL
from daily.analytics
where Date = (SELECT MIN(Date)
from daily.analytics
WHERE Date < date_sub(NOW(), INTERVAL 21 DAY))
order by SEDOL desc limit 1
WHILE #date is not null
begin
begin transaction
-- Adds old line to archive
insert into daily.Archive_analytics
select * from daily.analytics where Date = #Date AND SEDOL = #SEDOL
-- Deletes old line from main table
delete from daily.analytics where Date = #Date AND SEDOL = #SEDOL
commit transaction
-- Find the next greater minimum value
SELECT #Date = Date,#SEDOL = SEDOL
from daily.analytics
where Date = (SELECT MIN(Date)
from daily.analytics
WHERE Date < date_sub(NOW(), INTERVAL 21 DAY))
order by SEDOL desc
limit 1
END WHILE
END
Searching the web this seems to be more or less correct, however my WHILE statement is getting a "Syntax Error, unexpected while_sym, expecting END_OF_INPUT or ';'" and I'm not quite sure why.
Would greatly appreciate any help.
So it turns out the problem was due to delimiters and having multiple blocks in the code. The version without syntax errors looks like this:
SET #Date = NULL;
SET #SEDOL = NULL;
delimiter |
CREATE EVENT IF NOT EXISTS daily.Archive_Event_HOUR
ON SCHEDULE EVERY 1 DAY
DO
begin
SELECT #Date = Date,#SEDOL = SEDOL
from daily.analytics
where Date = (SELECT MIN(Date)
from daily.analytics
WHERE Date < date_sub(NOW(), INTERVAL 21 DAY))
order by SEDOL desc limit 1;
WHILE #Date is not null DO
start transaction;
-- Adds old line to archive
insert into daily.Archive_analytics
select * from daily.analytics where Date = #Date AND SEDOL = #SEDOL;
-- Deletes old line from main table
delete from daily.analytics where Date = #Date AND SEDOL = #SEDOL;
commit;
-- Find the next greater minimum value
SELECT #Date = Date,#SEDOL = SEDOL
from daily.analytics
where Date = (SELECT MIN(Date)
from daily.analytics
WHERE Date < date_sub(NOW(), INTERVAL 21 DAY))
order by SEDOL desc
limit 1;
END WHILE;
END|
delimiter ;

displaying dates of entire week starting day as saturday

I have got the code in this site for all weeks in a year as follows,i should populate week dates, starting date as saturday and end date as friday. when the week is finished it should enter to the next week with dates.
how could i achieve this please help me.
DECLARE #Year INT=2013;
DECLARE #start DATE;
--DECLARE #WK INT=2
SET #start = DATEADD(YEAR, #Year-1900, 0);
;WITH n AS
(
SELECT TOP (366) -- in case of leap year
TDate = DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY name)-1, #start)
FROM sys.all_objects
),
x AS
(
SELECT md = MIN(TDate) FROM n
WHERE DATEPART(WEEKDAY, TDate) = 7 -- assuming DATEFIRST is SATURDAY
),
y(TDate,wk) AS
(
SELECT n.TDate, ((DATEPART(DAYOFYEAR,n.TDate)-
DATEDIFF(DAY, #start,x.md)-1)/7)+1
FROM n CROSS JOIN x
WHERE n.TDate >= x.md
AND n.TDate < DATEADD(YEAR, 1, #start)
)
SELECT [date] = TDate, [week] = wk
FROM y WHERE wk < 53
ORDER BY [date];
Not really sure what you're asking, but based on your query above this will give week numbers, based on Saturday being the first day of the week, for 2013:
DECLARE #Year INT=2013;
DECLARE #start DATE;
SET #start = DATEADD(YEAR, #Year-1900, 0);
SET DATEFIRST 6; -- Set start of week as Saturday
WITH n AS
(
SELECT TOP (366) -- in case of leap year
TDate = DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY name)-1, #start)
FROM sys.all_objects
)
select TDate
, DATEPART(WEEK,TDate)
from n
where year(TDate) = 2013;
Edit:
So based on the various comments and answers I think what is required here is, for a given day, return all days in that same week, with Saturday as the first day of the week. So something like this:
set datefirst 6; -- make sure first day of week is Saturday
declare #date date = getdate(); -- change date as required here
with daysOfWeek as
(
select [date] = dateadd(dd, -datepart(dw, #date) + 1, #date)
union all
select [date] = dateadd(dd, 1, [date])
from daysOfWeek
where [date] < dateadd(dd, -datepart(dw, #date) + 7, #date)
)
select [date], dayOfWeek = datename(dw, [date])
from daysOfWeek
Which gives results:
I think this is what's required here?
Second edit:
First, create the function:
create function dbo.weekDates (#date date)
returns #dates table ([date] date, [dayofweek] varchar(9))
as
begin
with daysOfWeek as
(
select [date] = dateadd(dd, -datepart(dw, #date) + 1, #date)
union all
select [date] = dateadd(dd, 1, [date])
from daysOfWeek
where [date] < dateadd(dd, -datepart(dw, #date) + 7, #date)
)
insert into #dates ([date], [dayofweek])
select [date], [dayOfWeek] = datename(dw, [date])
from daysOfWeek;
return
end
go
Using the function:
set datefirst 6 -- Set Saturday as first day of week
select * from dbo.weekDates (getdate()) -- Change input parameter as required
I HAVE ACHIEVED TO DISPLAY THE WEEK DATES BUT IT IS DISPLAYING THE NEXT WEEK DATES I WANT TO DISPLAY THE CURRENT WEEK DATES, HERE START DAY IS SATURDAY AND END DAY IS FRIDAY
ALTER FUNCTION GetCurrentWeek()
RETURNS #TWeek TABLE (TWeek NVARCHAR(20))
AS
BEGIN
DECLARE #Year INT= DATEPART(YEAR,GETDATE());
DECLARE #start DATE;
SET #start = DATEADD(YEAR, #Year-1900, 0);
;WITH n AS
(
SELECT TOP (366) -- in case of leap year
TDate = DATEADD(DAY, ROW_NUMBER() OVER (ORDER BY name)-1, #start)
FROM sys.all_objects
),
x AS
(
SELECT md = MIN(TDate) FROM n
WHERE DATEPART(WEEKDAY, TDate) = 7 -- assuming DATEFIRST is SATURDAY
),
y(TDate,wk) AS
(
SELECT n.TDate, ((DATEPART(DAYOFYEAR, n.TDate)
- DATEDIFF(DAY, #start, x.md)-1)/7) + 1
FROM n CROSS JOIN x
WHERE n.TDate >= x.md
AND n.TDate < DATEADD(YEAR, 1, #start)
)
INSERT #TWeek
SELECT [date] = TDate
FROM y WHERE wk =DATEPART(wk, GetDate())
ORDER BY [date];
RETURN;
END

CTE to get dates between two dates using SQL Server

I want to fetch missing dates between two dates
say #maxDate = '2013-01-28'
say #curDate = GetDate()
I have written CTE to do this. Here is my CTE:
create procedure EmpDate
as begin
declare #curDate Date
set #curDate = GETDATE()
declare #maxDate Date
select #maxDate = MAX(EmpAttendance.Sdate)
from EmpAttendance
;with GetDates As
(
select 1 as counter, #maxDate as Date
UNION ALL
select counter + 1, DATEADD(day,counter,#maxDate)
from GetDates
where DATEADD(day, counter, #maxDate) < #curDate
)
select Date from GetDates
end
go
The result of this is
Date
2013-01-28
2013-01-29
2013-01-30
but I want
2013-01-29
2013-01-30
Please help me out.
Change
select 1 as counter, #maxDate as Date
to
select 1 as counter, DATEADD(day,1,#maxDate) as Date
To make it simpler though change the CTE
;with GetDates As
(
select DATEADD(day,1,#maxDate) as TheDate
UNION ALL
select DATEADD(day,1, TheDate) from GetDates
where TheDate < #curDate
)
...
DECLARE #STARTDATE DATETIME;
DECLARE #ENDDATE DATETIME;
DECLARE #YEARS INTEGER;
SET #YEARS = 10;
SET #STARTDATE = '20170101';
SELECT #ENDDATE = DATEADD(DAY,-1,DATEADD(YEAR,#YEARS,#STARTDATE));
DECLARE #FirstDayOfWeek INTEGER;
SET #FirstDayOfWeek = 6;
;WITH CTE_DATES
AS
(
SELECT #STARTDATE AS [DATE],
1 AS [Level]
UNION ALL
SELECT
DATEADD(DAY,1, [DATE] ) , [Level] + 1
FROM CTE_DATES
WHERE [DATE] < #ENDDATE
)
SELECT
[DATE],
DATENAME(dw,[Date]) AS Daynamelong,
LEFT(DATENAME(dw,[Date]),3) AS DaynameShort,
DATEPART(dw,[Date]) AS NaturalDayNumber,
CASE WHEN DATEPART(dw,[Date]) >= #FirstDayOfWeek THEN (DATEPART(dw,[Date]) - (#FirstDayOfWeek)) +1
ELSE
((DATEPART(dw,[Date]) - (#FirstDayOfWeek)) +1) + 7
END AS SpecialDayNumber,
[Level]
FROM
CTE_DATES
ORDER BY
[DATE] ASC
OPTION (MAXRECURSION 5000);

Count days between two dates, excluding weekends (MySQL only)

I need to calculate the difference (in days) between two dates in MySQL excluding weekends (Saturday and Sunday). That is, the difference in days minus the number of Saturday and Sunday in between.
At the moment, I simply count the days using:
SELECT DATEDIFF('2012-03-18', '2012-03-01')
This return 17, but I want to exclude weekends, so I want 12 (because the 3rd and 4th, 10th and 11th and 17th are weekends days).
I do not know where to start. I know about the WEEKDAY() function and all related ones, but I do not know how to use them in this context.
Simply try it using a simple function :
CREATE FUNCTION TOTAL_WEEKDAYS(date1 DATE, date2 DATE)
RETURNS INT
RETURN ABS(DATEDIFF(date2, date1)) + 1
- ABS(DATEDIFF(ADDDATE(date2, INTERVAL 1 - DAYOFWEEK(date2) DAY),
ADDDATE(date1, INTERVAL 1 - DAYOFWEEK(date1) DAY))) / 7 * 2
- (DAYOFWEEK(IF(date1 < date2, date1, date2)) = 1)
- (DAYOFWEEK(IF(date1 > date2, date1, date2)) = 7);
Test :
SELECT TOTAL_WEEKDAYS('2013-08-03', '2013-08-21') weekdays1,
TOTAL_WEEKDAYS('2013-08-21', '2013-08-03') weekdays2;
Result :
| WEEKDAYS1 | WEEKDAYS2 |
-------------------------
| 13 | 13 |
Illustration:
mtwtfSSmtwtfSS
123456712345 one week plus 5 days, you can remove whole weeks safely
12345------- you can analyze partial week's days at start date
-------12345 or at ( end date - partial days )
Pseudocode:
#S = start date
#E = end date, not inclusive
#full_weeks = floor( ( #E-#S ) / 7)
#days = (#E-#S) - #full_weeks*7 OR (#E-#S) % 7
SELECT
#full_weeks*5 -- not saturday+sunday
+IF( #days >= 1 AND weekday( S+0 )<=4, 1, 0 )
+IF( #days >= 2 AND weekday( S+1 )<=4, 1, 0 )
+IF( #days >= 3 AND weekday( S+2 )<=4, 1, 0 )
+IF( #days >= 4 AND weekday( S+3 )<=4, 1, 0 )
+IF( #days >= 5 AND weekday( S+4 )<=4, 1, 0 )
+IF( #days >= 6 AND weekday( S+5 )<=4, 1, 0 )
-- days always less than 7 days
Below function will give you the Weekdays, Weekends, Date difference with proper results:
You can call the below function like,
select getWorkingday('2014-04-01','2014-05-05','day_diffs');
select getWorkingday('2014-04-01','2014-05-05','work_days');
select getWorkingday('2014-04-01','2014-05-05','weekend_days');
DROP FUNCTION IF EXISTS PREPROCESSOR.getWorkingday;
CREATE FUNCTION PREPROCESSOR.`getWorkingday`(d1 datetime,d2 datetime, retType varchar(20)) RETURNS varchar(255) CHARSET utf8
BEGIN
DECLARE dow1, dow2,daydiff,workdays, weekenddays, retdays,hourdiff INT;
declare newstrt_dt datetime;
SELECT dd.iDiff, dd.iDiff - dd.iWeekEndDays AS iWorkDays, dd.iWeekEndDays into daydiff, workdays, weekenddays
FROM (
SELECT
dd.iDiff,
((dd.iWeeks * 2) +
IF(dd.iSatDiff >= 0 AND dd.iSatDiff < dd.iDays, 1, 0) +
IF (dd.iSunDiff >= 0 AND dd.iSunDiff < dd.iDays, 1, 0)) AS iWeekEndDays
FROM (
SELECT dd.iDiff, FLOOR(dd.iDiff / 7) AS iWeeks, dd.iDiff % 7 iDays, 5 - dd.iStartDay AS iSatDiff, 6 - dd.iStartDay AS iSunDiff
FROM (
SELECT
1 + DATEDIFF(d2, d1) AS iDiff,
WEEKDAY(d1) AS iStartDay
) AS dd
) AS dd
) AS dd ;
if(retType = 'day_diffs') then
set retdays = daydiff;
elseif(retType = 'work_days') then
set retdays = workdays;
elseif(retType = 'weekend_days') then
set retdays = weekenddays;
end if;
RETURN retdays;
END;
Thank You.
Vinod Cyriac.
Bangalore
IT my helpful to you
The bellow logic only show the how many days
like
sun mon
1 2 .....................
DELIMITER $$
DROP FUNCTION IF EXISTS `xx`.`get_weekday` $$
CREATE FUNCTION `xx`.`get_weekday` (first_date date, last_date date, curr_week_day int) RETURNS INT
BEGIN
DECLARE days_tot int;
DECLARE whole_weeks int;
DECLARE first_day int;
DECLARE last_day int;
SET whole_weeks = FLOOR(DATEDIFF(last_date,first_date)/7) ;
SET first_day = WEEKDAY(first_date) ;
SET last_day = WEEKDAY(last_date) ;
IF curr_week_day BETWEEN first_day AND last_day
AND last_day > first_day
OR ( curr_week_day BETWEEN last_day AND first_day
AND last_day < first_day )
THEN SET days_tot = whole_weeks + 1;
ELSE SET days_tot = whole_weeks ;
END IF;
RETURN days_tot;
END $$
DELIMITER ;
SELECT
`xx`.`get_weekday` ('2009-01-01', '2009-07-20', 0) as mo,
`xx`.`get_weekday` ('2009-01-01', '2009-07-20', 1) as tu,
`xx`.`get_weekday` ('2009-01-01', '2009-07-20', 2) as we,
`xx`.`get_weekday` ('2009-01-01', '2009-07-20', 3) as th,
`xx`.`get_weekday` ('2009-01-01', '2009-07-20', 4) as fr,
`xx`.`get_weekday` ('2009-01-01', '2009-07-20', 5) as sa,
`xx`.`get_weekday` ('2009-01-01', '2009-07-20', 6) as su;
Table based query
ip:
Weekday count
2 10
3 5
SELECT WEEKDAY( `req_date_time` ) AS weekday, COUNT( id ) AS id
FROM `ddd`
WHERE (
`req_date_time` >= '2014-12-01'
AND `req_date_time` <= '2014-12-31'
)
AND WEEKDAY( `req_date_time` ) != '1'
GROUP BY WEEKDAY( `req_date_time` )
You can use a dates table:
tbl_dates
dow is 'day of week' in my table. Then your query looks like this:
SELECT Count(theDate) AS numWeekDays
FROM tbl_dates
WHERE theDate >[startDate] And theDate <=[endDate] AND dow <> 1 AND dow <> 7;
In this case, 1 and 7 are Sunday, Saturday, respectively (which is the default) and, of course, you can nest that into another query if you need to calculate this for many startDate(s) and endDate(s).

MySQL Query to select data from last week?

Hi I have a table with a date field and some other information.
I want to select all entries from the past week, (week start from Sunday).
table values:
id date
2 2011-05-14 09:17:25
5 2011-05-16 09:17:25
6 2011-05-17 09:17:25
8 2011-05-20 09:17:25
15 2011-05-22 09:17:25
I want to select all ids from last week, expected output is 5, 6, 8.
(id 2 not in last week, and id 15 is in current week.)
How to write and SQL Query for the same.
select id from tbname
where date between date_sub(now(),INTERVAL 1 WEEK) and now();
SELECT id FROM tbl
WHERE date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY
SELECT id FROM table1
WHERE YEARWEEK(date) = YEARWEEK(NOW() - INTERVAL 1 WEEK)
I use the YEARWEEK function specifically to go back to the prior whole calendar week (as opposed to 7 days before today). YEARWEEK also allows a second argument that will set the start of the week or determine how the first/last week of the year are handled. YEARWEEK lets you to keep the number of weeks to go back/forward in a single variable, and will not include the same week number from prior/future years, and it's far shorter than most of the other answers on here.
Simplified form:
Last week data:
SELECT id FROM tbl
WHERE
WEEK (date) = WEEK( current_date ) - 1 AND YEAR( date) = YEAR( current_date );
2 weeks ago data:
SELECT id FROM tbl
WHERE
WEEK (date) = WEEK( current_date ) - 2 AND YEAR( date) = YEAR( current_date );
SQL Fiddle
http://sqlfiddle.com/#!8/6fa6e/2
You can make your calculation in php and then add it to your query:
$date = date('Y-m-d H:i:s',time()-(7*86400)); // 7 days ago
$sql = "SELECT * FROM table WHERE date <='$date' ";
now this will give the date for a week ago
Probably the most simple way would be:
SELECT id
FROM table
WHERE date >= current_date - 7
For 8 days (i.e. Monday - Monday)
PLEASE people... 'Last week' like the OP asked and where I was looking for (but found none of answers satisfying) is THE LAST WEEK.
If today is Tuesday, then LAST WEEK is Monday A WEEK AGO to Sunday A WEEK AGO.
So:
WHERE
WEEK(yourdate) = WEEK(NOW()) - 1
Or for ISO weeks:
WHERE
WEEK(yourdate, 3) = WEEK(NOW(), 3) - 1
If you're looking to retrieve records within the last 7 days, you can use the snippet below:
SELECT date FROM table_name WHERE DATE(date) >= CURDATE() - INTERVAL 7 DAY;
Here is a way to get last week, month, and year records in MySQL.
Last Week
SELECT UserName, InsertTime
FROM tblaccounts
WHERE WEEK(InsertTime) = WEEK(NOW()) - 1
AND MONTH(InsertTime) = MONTH(NOW())
AND YEAR(InsertTime) = YEAR(NOW())
Last Month
SELECT UserName, InsertTime
FROM tblaccounts
WHERE MONTH(InsertTime) = MONTH(NOW()) - 1
AND YEAR(InsertTime) = YEAR(NOW())
Last YEAR
SELECT UserName, InsertTime
FROM tblaccounts
WHERE YEAR(InsertTime) = YEAR(NOW()) - 1;
You'll need to calc which day relative to today is Sunday in your middleware (php, python, etc.)*
Then,
select id
from table
where date >= "$sunday-date" + interval 7 DAY
may be a way to get sunday's date relative to today in MySQL as well; that would be arguably the cleaner solution if not too expensive to perform
It can be in a single line:
SELECT * FROM table WHERE Date BETWEEN (NOW() - INTERVAL 7 DAY) AND NOW()
A simple way can be this one, this is a real example from my code and works perfectly:
where("actions.created_at >= DATE_SUB(CURDATE(), INTERVAL 1 WEEK)")
For more example Like last month, last year, last 15 days, last 3 months
Fetch Last WEEK Record
Using the below MySQL query for fetching the last week records from the mysql database table.
SELECT name, created_at
FROM employees
WHERE
YEARWEEK(`created_at`, 1) = YEARWEEK( CURDATE() - INTERVAL 1 WEEK, 1)
The above query will not work.
After the where clause, if we can not CAST the column value, then it will not work. You should cast the column value.
e.g.:
SELECT.....
WHERE CAST( yourDateColumn AS DATE ) > DATEADD( DAY, -7, CAST( GETDATE() AS DATE )
SELECT id FROM tb1
WHERE
YEARWEEK (date) = YEARWEEK( current_date -interval 1 week )
I often do a quick "last week" check as well and the following tends to work well for me and includes today.
DECLARE #StartDate DATETIME
DECLARE #EndDate DATETIME
SET #StartDate = Getdate() - 7 /* Seven Days Earlier */
SET #EndDate = Getdate() /* Now */
SELECT id
FROM mytable
WHERE date BETWEEN #StartDate AND #Enddate
If you want this to NOT include today just subtract an extra day from the #EndDate. If I select these two variables today get
#StartDate 2015-11-16 16:34:05.347 /* Last Monday */
#EndDate 2015-11-23 16:34:05.347 /* This Monday */
If I wanted Sunday to Sunday I would have the following.
SET #StartDate = Getdate() - 8 /* Eight Days Earlier */
SET #EndDate = Getdate() - 1 /* Yesterday */
#StartDate 2015-11-15 16:34:05.347 /* Previous Sunday */
#EndDate 2015-11-22 16:34:05.347 /* Last Sunday */
WHERE yourDateColumn > DATEADD(DAY, -7, GETDATE()) ;
You can also use it esay way
SELECT *
FROM inventory
WHERE YEARWEEK(`modify`, 1) = YEARWEEK(CURDATE(), 1)
i Use this for the week start from SUNDAY:
SELECT id FROM tbl
WHERE
date >= curdate() - INTERVAL DAYOFWEEK(curdate())+5 DAY
AND date < curdate() - INTERVAL DAYOFWEEK(curdate())-2 DAY
try this
WHERE trunc(DATE) <= (trunc (sysdate) -5) AND trunc(DATE) >= (trunc (sysdate) -12)
-5 is 5 days back from system date ,, -12 is 12 days back from system date for this example wednesday / or sednesday to wednesday cant recall.
Try this:
Declare #Daytype varchar(15),
#StartDate datetime,
#EndDate datetime
set #Daytype = datename(dw, getdate())
if #Daytype= 'Monday'
begin
set #StartDate = getdate()-7
set #EndDate = getdate()-1
end
else if #Daytype = 'Tuesday'
begin
set #StartDate = getdate()-8
set #EndDate = getdate()-2
end
Else if #Daytype = 'Wednesday'
begin
set #StartDate = getdate()-9
set #EndDate = getdate()-3
end
Else if #Daytype = 'Thursday'
begin
set #StartDate = getdate()-10
set #EndDate = getdate()-4
end
Else if #Daytype = 'Friday'
begin
set #StartDate = getdate()-11
set #EndDate = getdate()-5
end
Else if #Daytype = 'Saturday'
begin
set #StartDate = getdate()-12
set #EndDate = getdate()-6
end
Else if #Daytype = 'Sunday'
begin
set #StartDate = getdate()-13
set #EndDate = getdate()-7
end
select #StartDate,#EndDate
You can try this one. it worked for me :
where date(createdtime) <= date(curdate())-7
In the the above code createdtime is database field name, as individuals this name could vary.
If you already know the dates then you can simply use between, like this:
SELECT id
FROM `Mytable`
where MyDate BETWEEN "2011-05-15" AND "2011-05-21"