How to change date in database - mysql

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

Related

making Date Mon in one column equal to yyyy-mm-dd hh:mm:ss

In my table i'm having two fields created_date and created_month.
created_date: DATETIME e.g. 2020-12-17 17:23:56
created_month: VARCHAR e.g. 17-Aug
Now the situation- suppose for a record the created_date is 2020-12-17 17:23:56 and created_month is 17-Aug.
I am trying to achieve that the created_date should become like this 2020-08-17 17:23:56. The month from created_month gets updated in created_date. Not very much particular about time or date (17) but year yes.
If I'm doing this
UPDATE my_table SET created_date = STR_TO_DATE(created_month, '%d %b')
It is giving this 0000-08-12 00:00:00 . the year is 0000
put created_month in created_date without altering the year
UPDATE my_table
SET created_date = STR_TO_DATE(CONCAT(created_month,
'-',
YEAR(created_date),
' ',
SUBSTRING_INDEX(created_date, ' ', -1)),
'%d-%b-%Y %H:%i:%s');
fiddle

SQL query taking Jan Month as 13 instead of 01

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)

convert dd-mm date format to dd-month format in Mysql

I have Employees date-of-birth in dd/mm format and from this details i need to find employee's whose birthday is today,tomorrow and day after tomorrow and the Emp_dob column must display date in this format(eg: Employee's birthday is today then 09-NOV).
below is mysql query
SELECT
Emp_Name,Emp_Department,
Emp_DOB
FROM mldob.tbl_mldobtest WHERE
Emp_DOB IN (
DATE_FORMAT(CURDATE() + INTERVAL 1 DAY,'%d-%m'),
DATE_FORMAT(CURDATE(),'%d-%m'),
DATE_FORMAT(CURDATE()+INTERVAL 2 DAY,'%d-%m')
)
order by tbl_mldobtest.Emp_DOB;
the problem is EMP_DOB column displays dob in 09-11 format but i need in 09-Nov format
so can anybody please guide me how to achieve this.
thanks in advance.
EDIT: Sorry, didn't pay attention to DOB only holding day/month instead of full date.
Assuming Emp_DOB is CHAR(5) held as e.g. '09-11'
SELECT Emp_Name, Emp_Department, DATE_FORMAT(CONCAT(DATE_FORMAT(CURDATE(), '%Y'), '-', SUBSTRING(Emp_DOB,4,2), '-', SUBSTRING(Emp_DOB,1,2)), '%d-%b')
FROM tbl_mldobtest
WHERE Emp_DOB IN (
DATE_FORMAT(CURDATE() + INTERVAL 1 DAY,'%d-%m'),
DATE_FORMAT(CURDATE(),'%d-%m'),
DATE_FORMAT(CURDATE()+INTERVAL 2 DAY,'%d-%m')
)
ORDER BY Emp_DOB;
If you hold Emp_DOB as a date (as I originally assumed) then this will do what you want, no need to format dates in where clause:
SELECT Emp_Name, Emp_Department, DATE_FORMAT(Emp_DOB, '%d-%b')
FROM tbl_mldobtest
WHERE DATE(Emp_DOB) BETWEEN DATE(CURDATE()) AND DATE(DATE_ADD(CURDATE(), INTERVAL 2 DAY))
ORDER BY Emp_DOB;
The format for the abbreviated month name is '%b', not '%m'
Try below format
DATE_FORMAT(CURDATE(),'%d-%b')
This should work
SELECT
Emp_Name,Emp_Department,
DATE_FORMAT(Emp_DOB, '%e%a') as emp_dob
FROM mldob.tbl_mldobtest WHERE
Emp_DOB IN (
DATE_FORMAT(CURDATE() + INTERVAL 1 DAY,'%d-%m'),
DATE_FORMAT(CURDATE(),'%d-%m'),
DATE_FORMAT(CURDATE()+INTERVAL 2 DAY,'%d-%m')
)
order by tbl_mldobtest.Emp_DOB;
Also you can refer mysql date functions
http://www.mysqltutorial.org/mysql-date_format/
replace all %m in your sql with %b

How to display timestamp by using table columns

I am new to one of my project. Previous people saved date and time in two columns in the below format :
For date : 2015-04-15 (date type)
For time : 04:20 PM (varchar)
Now I want to compare the above columns with present date and time.
I tried this below query but it's showing NULL.
SELECT UNIX_TIMESTAMP(
STR_TO_DATE('evnt_endDate Event_EndTime', '%Y-%m-%d %h:%i%p')
)
FROM events
Please could any one tell me how to compare?
Use concat():
SELECT UNIX_TIMESTAMP(STR_TO_DATE(concat(evnt_endDate, ' ', Event_EndTime
), '%Y-%m-%d %h:%i%p')
)
'evnt_endDate Event_EndTime' is a string. You need to reference the fields/columns and concatenate their values like
CONCAT(`evnt_endDate`, ' ', `Event_EndTime`)
The resulting query would then be
SELECT UNIX_TIMESTAMP( STR_TO_DATE( CONCAT(`evnt_endDate`, ' ', `Event_EndTime`), '%Y-%m-%d %h:%i%p') ) from events

MYSQL: How do I set a date (makedate?) with month, day, and year

I have three columns, y, m, and d (year, month, and day) and want to store this as a date.
What function would I use on mySQL to do this?
Apparently makedate uses year and day of year (see below), but I have month.
I know I can use STR_TO_DATE(str,format), by constructing the string from (y,m,d), but I would guess there is an easier way to do it.
REFERENCES
MAKEDATE(year,dayofyear)
Returns a date, given year and day-of-year values. dayofyear must be greater than 0 or the result is NULL.
I believe you can use a string in the proper format:
UPDATE table SET my_date = '2009-12-31';
Edit: Yeah you can, just verified it in MySQL 5.1.
It isn't high on readability but the following would work:
SELECT'1900-01-01' + INTERVAL y-1900 YEAR + INTERVAL m-1 MONTH + INTERVAL d-1
DAY FROM ...
Not sure that this any more efficient than using CONCAT and STR_TO_DATE.
i hope this wil work out..
str_to_date( concat( year( curdate( ) ) , '-', month( a.dob ) , '-', day( a.dob ) ) , '%Y-%m-%d' )
Where a.dob is my column's name which has DATE