How to update only month in date column? - mysql

I have one table with 30 entries and date column is like following
2014-11-01
2014-11-02
2014-11-03
.
.
.
2014-11-30
Now i want to write MySQL query to update month from 11 to 10 or you can say from month November to October.
I want to change only month in all these dates from 11 to 10.

Use DATE_ADD function for changing the month and MONTH function for filtering the records. Assuming the table name is tbl and the column name is date, here's what the query will look like
UPDATE `tbl`
SET `date` = DATE_ADD(`date`, INTERVAL -1 MONTH)
WHERE MONTH(`date`) = 11

Related

Fetch distinct available dates from table

I have a data table where data is present for every date (for 50 branches) except saturday and sunday. I am trying to get the last valid date from table from multiple given dates.
select distinct BW_DATE from backdated_weekly where BW_DATE <='2021-09-30' or BW_DATE<='2021-09-26' order by BW_DATE desc;
As 2021-09-30 is a valid date but 2021-09-26 (Sunday) is not present in table. I am trying to get output as
2021-09-30
2021-09-24
But above query gives all results below given dates.
If it is confirmed there are dates continuously in the table for all mon-fri only, simply select the maximum date up to the given date
SELECT MAX(BW_DATE)
FROM backdated_weekly
WHERE BW_DATE <= '2021-09-30'
UNION
SELECT MAX(BW_DATE)
FROM backdated_weekly
WHERE BW_DATE <= '2021-09-26'
Also we can calculate newest date in mon-fri for a given date directly without any table
WEEKDAY is the function to be used
Returns the weekday index for date (0 = Monday, 1 = Tuesday, … 6 =
Sunday).
SELECT CASE WHEN WEEKDAY('2021-09-30') IN ( 5, 6 ) THEN DATE('2021-09-30') - INTERVAL WEEKDAY('2021-09-30') - 4 DAY ELSE DATE('2021-09-30') END
UNION
SELECT CASE WHEN WEEKDAY('2021-09-26') IN ( 5, 6 ) THEN DATE('2021-09-26') - INTERVAL WEEKDAY('2021-09-26') - 4 DAY ELSE DATE('2021-09-26') END
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=477775c0eddbfa733e60bc629a8a68d4

SQL - Last Day of Month

I got a Table which looks like this:
DATE | Number
01-01-16 00:00:00 10
02-01-16 00:00:00 10
03-01-16 00:00:00 11
04-01-16 00:00:00 12
05-01-16 00:00:00 13
....
31-01-16 00:00.00 15
........
29-02-16 00:00:00 18
I got this table for the last few months.
I now want to retrieve the value of the rows, which contain the last day of the previous month and the month before the last month. So for today I would like to retrieve the Value of the 31-1-16 and 29-2-16.
My result should look like:
lastmonth | lastmonth2
18-> Corresponding value to Date: 29-02-16 | 15 -> value for 31-01-16
Would appreciate any help.
Cheers
Here is logic for the last day of this month and the previous month:
select last_day(curdate()) as last_day_of_this_month,
last_day(date_sub(curdate(), interval 1 month)) as last_day_of_prev_month
You can get the last day of any month relative to the current month by changing the "1".
And, I have no idea what date "30-2-16". When describing dates, you should use ISO standard formats. The last day of February 2016 was 2016-02-29.
This is Gordon's code for determining the correct dates plus subqueries to fetch the Number values for those rows:
SELECT
(SELECT Number FROM cc_open_csi_view
WHERE last_day(date_sub(curdate(), interval 1 month)) = date(`DATE`)) as lastmonth,
(SELECT Number FROM cc_open_csi_view
WHERE last_day(date_sub(curdate(), interval 2 month)) = date(`DATE`)) as lastmonth2
FROM DUAL;
Hope that's what you wanted! Works for me in a simple example. I don't know if you need the date() part around DATE but it seemed safest.
SELECT CASE
WHEN last_day(curdate()) = `DATE` THEN number
END as number_last_month,
CASE
WHEN last_day(date_sub(curdate(), interval 1 month)) = `DATE`
THEN number
END as number_last_month2
FROM cc_open_csi_view
I can't test it right now on sqlfiddle.

Count number of Distinct days in query

We have a table that has a StartDate field which holds a type of datetime. There are thousands of records and I am looking for a way to find the number of days within a given result returned from this table. For instance, if my table had this data:
ID | StartDate
--------------
1 01/01/2013 09:34:54
2 01/01/2013 11:23:21
3 04/11/2013 14:43:23
4 04/11/2013 17:13:03
5 04/25/2013 18:02:59
6 07/21/2013 02:56:12
7 10/01/2013 19:43:10
Then the query should return 5 as the 2 dates on 01/01/2013 count as 1 and the same for 04/11/2013.
The only SQL I've been able to come up with is:
SELECT COUNT(DISTINCT(DATEPART(DAY, StartDate)))
FROM Stats
WHERE StartDate BETWEEN '01/01/2013' AND '12/31/2013' --This is just for filtering
But this returns 4 because it doesn't take the month into account.
Any help is appreciated.
You can CAST as date
SELECT COUNT(DISTINCT CAST(StartDate AS DATE))
FROM Stats
WHERE StartDate >= '20130101' AND StartDate < '20140101'
Also use an unambiguous date format such as yyyymmdd and >= < not BETWEEN.
Your current query would include the 31st December if there was a row with exactly the value 20131231 00:00:00 but not any with different times on that date. I doubt that is intentional.

Limiting MySQL results to prior days of the week

I have a table like so:
id | gallons_used | date
----------------------
1 2 157263300
2 5 157262000
...
I want to get a result set containing only records that took place on X day of the week (Monday or Tuesday or Wednesday, etc etc)
Use DAYNAME() in your WHERE clause
WHERE DAYNAME(FROM_UNIXTIME(`date`)) = 'Monday' <-- by day of the week
AND `date` < INTERVAL CURRENT_DATE - 7 DAY <-- within the last week
You can use DAYOFWEEK() as well but this is more readable.
Something that would check for Wednesday as an example given that your date column is a timestamp:
DAYOFWEEK(date) = 4
Reference to documentation: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_dayofweek
SQLFiddle: http://www.sqlfiddle.com/#!2/12762/3
I had a FROM_UNIXTIME() call around date as well but I don't think it is required(not 100% sure). http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
Since you're using a Unix timestamp, you can use the following:
SELECT id, UNIX_TIMESTAMP(`date`)
FROM ...
WHERE DAYNAME(FROM_UNIXTIME(`date`)) = 'Monday'
AND `date` > UNIX_TIMESTAMP() - 604800
See the demo

Getting a next week date

Using MySQL
ID Date
001 2010-08-01
002 2010-08-15
003 2010-08-22
...
....
Query
select ID, Date from table where date < curdate + 7;
The above query is not working, it showing error.
How to get date upto nextweek date, I don't want to mentioned the date, it should calculate the systemdate + 7 days.
For Example
Today is 2010-06-30,
So it should take a value upto 2010-07-06
How to make a query for this condition....?
Using the DATE_ADD() function:
... WHERE date < DATE_ADD(CURDATE(), INTERVAL 7 DAY);
using an operator:
.... WHERE date < CURDATE() + INTERVAL 7 DAY
reference on date_add
I'm assuming that by curdate, you mean the function and not a column name. If it's a column name, change accordingly (although I wouldn't name a column after an existing mySQL function.)
Please try this
select ID, Date from table where Date < DATE_ADD(CURDATE(), INTERVAL 7 DAY)