Changing year in mysql date - mysql

I have a bunch of dates in our database stored in the standard mysql date type.
How can I covert a year to 2013, regardless of original date.
So if a date is 2009-01-01 it would be 2013-01-01, but if it's 2012-01-04, it'd convert to 2013-01-14.
I figured it'd be simple and obvious, but I couldn't figure it out =/

That's simple:
for DATETIME:
UPDATE table_name
SET date_col=DATE_FORMAT(date_col,'2013-%m-%d %T');
for DATE:
UPDATE table_name
SET date_col=DATE_FORMAT(date_col,'2013-%m-%d');

The problem with the current answers is that none of them take leap year into account. If you take the date '2016-02-29' and convert it to the year 2013 through concatenation, you get '2013-02-29', which is not a valid date. If you run DATE_FORMAT('2013-02-29', '%Y-%m-%d') the result is null. See an example here:
http://sqlfiddle.com/#!9/c5358/11
A better way to change the year is to use DATE_ADD since it accounts for daylight savings. For example:
SELECT
DATE_FORMAT(DATE_ADD(datecol, INTERVAL (YEAR(CURRENT_DATE()) - YEAR(datecol)) YEAR), '%Y-%m-%d') `date`
FROM t;
You could substitute CURRENT_DATE() with '2013-01-01' if you still wanted to convert all dates to 2013 instead of the current year. An example of this solution is here:
http://sqlfiddle.com/#!9/c5358/12

UPDATE tableName
SET dateColumn = dateColumn + INTERVAL 4 YEAR
SQLFiddle Demo
other way is to concatenate it,
UPDATE Table1
SET DateColumn = CONCAT(YEAR(CURDATE()), '-', DATE_FORMAT(dateColumn, '%m-%d'))
SQLFiddle Demo

If its a date field:
UPDATE table_name SET date_field_name = CONCAT("2013", RIGHT(date_field_name,6));
If its a date time field:
UPDATE table_name SET date_field_name = CONCAT("2013", RIGHT(date_field_name,15));

Current date from quest was 2013, I understand that you wish set current YEAR in date.
UPDATE table_name SET date_col=DATE_FORMAT('2013-05-06',YEAR(CURRENT_DATE)-%m-%d);

Related

DateTime condition [duplicate]

I get a datetime field, that's currently in the query as:
SELECT DATE_FORMAT(x.date_entered, '%Y-%m-%d') AS date FROM x ORDER BY date ASC
What I want to do is to subtract 3 hours from that date (GMT issues), but I can't do it in PHP as PHP only knows the date part, not the time.
mySQL has DATE_SUB():
SELECT DATE_SUB(column, INTERVAL 3 HOUR)....
but would it not be better to try and sort out the underlying time zone issue instead?
Assuming you have some timezone issue and know source and destination timezone, you could convert it like so
SELECT DATE_FORMAT(CONVERT_TZ(x.date_entered, 'UTC', 'Europe/Berlin'),
'%Y-%m-%d') AS date
FROM x ORDER BY date ASC;
Normal select query.
Once applied DATE_ADD() function in MySQL
select lastname,
date_add(changedat, interval -24 hour) as newdate
from employee_audit;
lastname and changedat is field name and employee_audit is table name.

get all row entries based on month/year in MySQL

I have a database with the following structure and I am trying to get all rows from this table based on passing both the month and year using a where on the timestamp column (this will be a unix standard timestamp)
e.g month - 3, year - 2018 // get all rows for March 2018 only.
// db structure
id, timestamp, post_title
If you want rows for a given month using a unix timestamp, I would recommend:
where timestamp >= unix_timestamp('2018-03-01') and
timestamp < unix_timestamp('2018-04-01')
If you are passing in a variable, I would recommend passing in the first day of the month and doing:
where timestamp >= unix_timestamp(?) and
timestamp < unix_timestamp(? + interval 1 month)
Use convert function
SELECT * FROM dbo.YourTable WHERE CONVERT(VARCHAR(5),DATEPART(mm,timestamp))+'-'+CONVERT(VARCHAR(5),DATEPART(yy,timestamp)) ='3-2018'
I guess this can help you.
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

How to convert a year to date in mysql?

I have a table with year data in a year datatype field, now I want to insert new data with date data and I want to convert the year from the old data to date with default month and day 1.1.YearFromOldData.
I'm looking for something like the function STR_TO_DATE but for the datatype year NOT VARCHAR NOT VARCHAR and I fail to find it. How would I do this?
I want to do this
SELECT YEAR_TO_DATE(myYearField, '%1/%1/%Y')
FROM myTable
Assuming that you have a varchar field named year_dt with old years, use the following query to get a date with default day and month
SELECT DATE(CONCAT(table.year_dt, '-01-01')) as 'date' FROM table
this will return date in default format i.e. YYYY-MM-DD
If you want the first day of the year, I think the easiest way is with makedate():
select makedate(year, 1)
You can get the timestamp to insert to the database using
timestamp = (year - 1970) / 31557600
STR_TO_DATE() is exactly what you need. Try this if you have four-digit years, in a string, an integer, or a YEAR column. It works for them all.
select str_to_date(CONCAT(year_column,'-01-01'), '%Y-%m-%d')
If you have two-digit years try this (lower case %y)
select str_to_date(CONCAT(year_column,'-01-01'), '%y-%m-%d')
This is cool because you can do all sorts of date arithmetic: for example
select str_to_date(CONCAT(year_column,'-01-01'), '%Y-%m-%d')
+ INTERVAL 1 QUARTER
- INTERVAL 1 DAY
will give you the last day of the first quarter of your year.

mysql convert date to same date of current year

How can I replace the year of a date column with that of the current year?
the following returns NULL
SELECT str_to_date(concat(year(now()), '-',
month(datecolumn), '-' ,day(datecolumn)), '%Y-%M-%D')
FROM TABLE
Khalid's answer is correct most of the time. Leap year messes things up! If you run the proposed query where the value of datecol is '2016-02-29' and the CURRENT_DATE is '2017-01-01', for example, you get null.
An alternate way to do this that handles leap year more gracefully is like this:
SELECT DATE_FORMAT(
MAKEDATE(YEAR(CURRENT_DATE()), DAYOFYEAR(datecol)),
'%Y-%m-%d'
) `date`
FROM t
The value of date here would be 2017-03-01.
Edit/clarification: The problem is that changing the year of '2016-02-29' to 2017, for example, produces '2017-02-29', which is not a valid date. Then, running DATE_FORMAT('2017-02-29', '%Y-%m-%d') results in null. A demo of the problem is here:
http://sqlfiddle.com/#!9/c5358/11
However, after reviewing my answer I realized that I another problem by using MAKEDATE since any date on a leap year after Feb 28 is days+1 for a "normal" year with 365 days. For example, if datecol = '2016-03-01' and the current year were 2017 then the converted date would be '2017-03-02', not '2017-03-01' as desired. A better approach is as follows:
SELECT
DATE_FORMAT(DATE_ADD(datecol, INTERVAL (YEAR(CURRENT_DATE()) - YEAR(datecol)) YEAR), '%Y-%m-%d') `date`
FROM t;
This method turns any Feb 29th into the 28th, and otherwise keeps all other dates exactly as you'd expect them. A demo of the solution is here:
http://sqlfiddle.com/#!9/c5358/12
You can do so
SELECT
CONCAT(YEAR(CURRENT_DATE()),RIGHT(datecol,15)) `date`
FROM t
Demo
Or for date only
SELECT
DATE_FORMAT(
CONCAT(YEAR(CURRENT_DATE()),RIGHT(datecol,15))
,'%Y-%m-%d') `date`
FROM t
Demo 2

How can I UPDATE an expiration date in MySQL?

So, I need to reset the expiration dates for a bunch of coupon codes in our database. Our expirations dates are field "to_date" and are displayed as the following: to_date = '2013-04-14'
I need to set the to_date as 28 days after the from_date. So basically, something like this:
UPDATE salesrule
SET name = 'New coupon code', to_date = 'from_date + 28 days'
I know this would work for a simple int value, but I'm not sure how to do this give that the data displays as an actual date. I have no control over how the date itself displays, that's a built in Magento functionality.
I'm a big noob when it comes to MySQL, but I've done some research and I've found the format function: FORMAT(Now(),'YYYY-MM-DD') I have a feeling this may be the key... can someone point me in the right direction it terms of formatting or writing this command correctly? Thank you!
UPDATE salesrule
SET name = 'New coupon code', to_date = DATE_ADD(from_date, INTERVAL 28 DAY);
More info about the DATE_ADD() function here:
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-add
to_date = DATE_ADD(from_date, INTERVAL 28 DAY)
Check this question out, it does what you want.
You can use the DATE_ADD() function:
... WHERE DATE(DATE_ADD(eventdate, INTERVAL -1 DAY)) = CURRENT_DATE
It can also be used in the SELECT statement:
SELECT DATE_ADD('2010-05-11', INTERVAL 1 DAY) AS Tomorrow;