SQL query taking Jan Month as 13 instead of 01 - mysql

SELECT STR_TO_DATE( CONCAT( YEAR(MAX(my_date)) , '-', MONTH(MAX(my_date))+1 , '-', 01 ) , '%Y-%m-%d')
I am using this to extract date. I get following output:
Output Data Entry till
2017-07-01 2017 June entry complete
2017-08-01 2017 July entry complete
But as soon as the data entry is done for 2017 December I get 2018-13-01 instead of 2018-01-01. I know it is because of the +1 I am adding in month value. I am unsure how do I loop it back to 01 after 12?
The value in csv file for my_date looks like 2017-01-06. It is in string format.

You are doing date arithmetic on a string by adding 1 to the month. Instead, use the DateAdd() function.
SELECT date_add(STR_TO_DATE( CONCAT( YEAR(MAX(my_date)) , '-', MONTH(MAX(my_date)) , '-', 01 ) , '%Y-%m-%d'), INTERVAL 1 MONTH)

Related

How to get week number and date of that week between two dates using MySQL

I am using MySQL And I have two dates "From date" and "To date", and based on these date i want to get week number and dates of that week between "To" and "From" Dates.
I have tried the following mysql query.
SELECT count(*) as count,
CONCAT(DATE_FORMAT(DATE_ADD(FROM_DAYS(TO_DAYS(FROM_UNIXTIME(`webform_submissions`.`submitted`)) -MOD(TO_DAYS(FROM_UNIXTIME(`webform_submissions`.`submitted`)) -1, 7)),INTERVAL -6 DAY),'%M %d'), ' - ' ,DATE_FORMAT(FROM_DAYS(TO_DAYS(FROM_UNIXTIME(`webform_submissions`.`submitted`)) -MOD(TO_DAYS(FROM_UNIXTIME(`webform_submissions`.`submitted`)) -1, 7)),'%M %d')) as date ,
CONCAT(YEAR(FROM_UNIXTIME(`webform_submissions`.`submitted`)), '/', WEEK(FROM_UNIXTIME(`webform_submissions`.`submitted`))) as week
FROM `webform_submissions`
where `webform_submissions`.`nid` = 121
AND DATE_FORMAT(FROM_UNIXTIME(`webform_submissions`.`submitted`), '%Y-%m-%d') between '2019-11-01' and '2019-12-03'
GROUP BY week
ORDER BY `webform_submissions`.`submitted` ASC
The following result is display according to above query.
But it seems that it gives wrong result because week number 43 lies between 21-27 Oct and i want to get result between between '2019-11-01' and '2019-12-03'.
Expected output should be like the screenshot. Because From date "2019-11-01" lies between Oct 28- Nov 03 (Week 44). so records should be start from 44 week number.
Any Idea how to get correct number of week and dates?
Here's a somewhat easier to read version of your query (using nested subqueries since MySQL 5.6 doesn't support CTEs) and using DATE_FORMAT with the %x/%v format to generate the week to match your expected result (October 28 is the start of week 44). Note I've added a MIN into the generation of date so that the query will still work in MySQL 5.7 with SQL mode ONLY_FULL_GROUP_BY.
SELECT COUNT(*) AS count,
CONCAT(DATE_FORMAT(MIN(startofweek), '%M %d'),
' - ',
DATE_FORMAT(MIN(startofweek) + INTERVAL 6 DAY, '%M %d')) AS date,
week
FROM (SELECT submitted - INTERVAL (dayofweek + 6) % 7 DAY AS startofweek,
week
FROM (SELECT nid,
DATE(FROM_UNIXTIME(submitted)) AS submitted,
DATE_FORMAT(FROM_UNIXTIME(submitted), '%w') AS dayofweek,
DATE_FORMAT(FROM_UNIXTIME(submitted), '%x/%v') AS week
FROM webform_submissions
WHERE nid = 121
AND DATE(FROM_UNIXTIME(submitted)) BETWEEN '2019-11-01' AND '2019-12-03'
) AS dates
) AS ws
GROUP BY week
Output (for my sample data)
count date week
3 October 28 - November 03 2019/44
4 November 04 - November 10 2019/45
Demo on dbfiddle
Try to set the second parameter of WEEK() according to your expected result.
With the second parameter you can set the mode with wich you specify wether the week starts with sunday or monday, the week numbers starts with 0 or 1 and the rule defining the first week of the year.
See https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_week

SQL: Get the beginning date of the month from a Month_year name column

I have a date data as below:
Monthyear
Jan 2018
Feb 2018
Mar 2018
I want to convert this into this format:
Monthyear
01-01-2018
01-02-2018
01-03-2018
That is, as a date with the first date of the month.
I tried casting the above and it doesnt work.
Kindly help me out.
You can use STR_TO_DATE:
SELECT STR_TO_DATE(CONCAT('1 ', Monthyear), '%e %b %Y')
e.g.
SELECT STR_TO_DATE(CONCAT('1 ', 'Jan 2018'), '%e %b %Y')
Output:
2018-01-01
The reason for CONCATing a '1 ' to the string is to avoid issues with SQL NO_ZERO_DATE mode (this is discussed in the manual entry for STR_TO_DATE).
use str_to_date()
select str_to_Date(concat('01',Monthyear),"%d %b %Y")

Mysql between code is not showing the correct output

I have written a query to get the data between a particular month and year with another month and year.
For example I want to get data between Nov 2014 to Nov 2015.
However, my query is just showing the output for just Nov 2014 and Nov 2015.
The data in between this two dates is not showing. Below is my query:
SELECT * FROM prc.tbictrepairsustainability
WHERE sustainRegion = 'America'
AND (DATE_FORMAT(sustainDate, '%m %Y') BETWEEN '11 2014' AND '11 2015') ;
My sustainDate column data type is date.
Pls correct me if there is something wrong with my query.
Thanks
It worked after i changed the date format to '%d %m %Y' with STR_TO_DATE.
SELECT *
FROM prc.tbictrepairsustainability
WHERE sustainRegion = 'America'
AND sustainDate Between STR_TO_DATE('01 11 2014','%d %m %Y')
AND STR_TO_DATE('30 11 2015','%d %m %Y')
You need to convert the strings to dates in order to compare them to the date in the table.
SELECT *
FROM prc.tbictrepairsustainability
WHERE sustainRegion = 'America'
AND (sustainDate BETWEEN STR_TO_DATE('30 11 2014','%d %m %Y')
AND STR_TO_DATE('30 11 2015','%d %m %Y'))
Updated per Ronald's suggestion.

MYSQL query between two unix timestamp

How to query data in specific time like: I want to select all records stored between 1 pm and 5 pm during a month , when the time stored in datetime column in Unix timestamp format like so "1403830861".
The FROM_UNIXTIME function can convert a unix timestamp to a date. The %k format (hour represented as an in 0..23) seems to fit the bill nicely. The month could be easily extracted in the same fashion, using the %m format and the year using %Y. E.g., the following query would return only results from November 2014:
SELECT *
FROM my_table
WHERE FROM_UNIXTIME (timestamp_column, '%k') BETWEEN 13 AND 17 AND
FROM_UNIXTIME (timestamp_column, '%m') = 11 AND
FROM_UNIXTIME (timestamp_column, '%Y') = 2014

How to change date in database

In my Mysql database, i have a set of date.
How can I increase for each and every date in my database by 3 years ?
For example the current date is :
2 January 2001
I want the date to be increased by three years:
2 January 2004
Try this -
UPDATE TABLE set fieldname = DATE_ADD( fieldname, INTERVAL 3 YEAR )
For more information and play part with dates you can check this link :-
function_date-add
Working Fiddle -- http://sqlfiddle.com/#!2/9c669/1
EDIT
This solution updates date type is VARCHAR and structure of date like - 2 January 2001
It will update date to 2 January 2004 by the interval of 3
Although the best way to handle date is use date DATATYPEs(ex timestamp, datetime etc) instead of saving it in VARCHARs
Tested code --
UPDATE date
SET `varchardate`= DATE_FORMAT(DATE_ADD( str_to_date(`varchardate`, '%d %M %Y'), INTERVAL 3 YEAR ) , '%d %M %Y')
Check date_add function, it should do the thing -http://www.w3schools.com/sql/func_date_add.asp
Below query is to update the date column and also changed to date format.
update date1 set dates =
DATE_ADD( (str_to_date(concat(
SUBSTRING_INDEX( dates, ' ', 1 ),'-',
Substring(SUBSTRING_INDEX( SUBSTRING_INDEX( dates, ' ', 2 ) , ' ', -1 ),1,3) ,
'-',
SUBSTRING_INDEX( dates, ' ', -1 )),'%d-%M-%Y')), INTERVAL 3 YEAR )
OR
The second query is simply update the year by 3.
update date1 set dates =
concat( SUBSTRING_INDEX( dates, ' ', 2 ) , ' ',
(SUBSTRING_INDEX( dates, ' ', -1 ) +3) )
SQL Fiddle Demo