MYSql change only year from timestamp - mysql

Accidentally, my host had set the time for 2012 and in my database, there are more than 4000 records updated timestamp column as 2012-11-21 11:24:40.
I cant change time on all columns while updating, I need to keep the same time as per the table column, I just need to change only year 2012 to 2016 in that table.
I listed all columns using this query,
SELECT * FROM `table` WHERE `del_date` LIKE '%2012-11-21%';
but do not know to change the year only in that column.
Please help how can I change the same.

You can use ADDDATE(), like
UPDATE yourTableName SET del_date = ADDDATE(del_date, INTERVAL 4 YEAR);

Related

SQL Query to get data between two weeks?

I have a week column with week numbers as w0, w1, w2.... I am trying to get last last six weeks data. Here's the sql query I am using.
SELECT * FROM week
WHERE uid = '9df984da-4318-1035-9589-493e89385fad'
AND report_week BETWEEN `'w52' AND 'w5'`;
'w52' is essentially week 52 in December 2015 and 'w5' is Jan 2016. The 'between' seems to not work. Whats the best way to get data from the above two weeks?
Here's the CREATE TABLE statement:
CREATE TABLE `week` (`uid` VARCHAR(255) DEFAULT '' NOT NULL,
`report_week` VARCHAR(7) NOT NULL,
`report_files_active` BIGINT DEFAULT NULL);
Essentially this table is getting populated from other table which has date column. It uses dates from other table and summarizes weekly data into this.
Any help is appreciated.
Refer to this SO Discussion which details the reasons for a problem similar to yours.
BETWEEN 'a' and 'b' actually matches to columnValue >='a' and columnValue <= 'b'
In your case w52 is greater than w5 due to lexicographic ordering of Strings - this means that the BETWEEN clause will never return a true (think about it as equivalent to saying BETWEEN 10 and 1 instead of BETWEEN 1 and 10.
Edit to my response:
Refrain from storing the week value as a string. Instead here are a couple of approaches in order of their preference:
Have a timestamp column. You can easily then use MySQL query
facilities to extract the week information out of this. For a
reference see this post.
Maintain two columns - YEAR, WEEKNO where YEAR will store values
like 2015, 2016 etc and WEEKNO will store the week number.
This way you can query data for any week in any year.
please show me table structure and DB name because it different for other, if it is any timestamp then we can use BETWEEN 'systemdate' AND 'systemdate-6'

Incrementally update date field column based on order of a second column

MSQL DB for WordPress site.
I would like to update a date column for each row in a table, setting the date/time to an incrementing value (e.g. increment value by 1 day). The order that I do the updates should be based on the textual sort of another column in the row. The actual date value used isn't relevant (it could start at 2001-01-01 00:00:00 and increment in seconds, minutes, hours or days).
Example of two columns in the table:
field_date , title_text
2014-08-20 09:00:27, AAA-Entry
2014-08-24 10:00:00, ZZZ-Entry
2014-08-27 10:15:00, MMM-Entry
So, the row with the first 'title_text' (alphabetical sorting) should get the newest date. The next row based on title_text should get a slightly older date and so on until the last row (based on title_text) has the oldest date. So the data shown above would end up looking something like:
field_date , title_text
2000-01-03 00:00:00, AAA-Entry
2000-01-01 00:00:00, ZZZ-Entry
2000-01-02 00:00:00, MMM-Entry
After the update then the command:
select * from tbl_name order by field_date
would show output that would also be in alphabetical order of the title_text field. This would be equivalent to running:
select * from tbl_name order by title_text
I'm looking at:
[Incrementing datetime field with an update statement for SQL2005 and trying to get this to work on mySQL as I think this would work (I just change the 'order by' statement to 'order by title_text'). But I'm having problems with converting this to mySQL.
I'd appreciate any suggestions on different approaches or with getting the above solution to work in mySQL.
Thanks
Ian
Note: The reason behind this is that Wordpress populates list boxes/search results, based on the posting date, but I want it to be ordered based on the title text. There may be a 'Wordpress' answer (or plugin) to do this (and if you know, please feel free to tell me ;-) ), but from a learning viewpoint, I'd also like to understand how this was possible using mySQL.
In MySQL, you can do updates with sorts. The easiest way is to define a variable first and then do the update:
set #rn = -1;
update t
set field_date = date('2000-01-01') + interval (#rn := #rn + 1) day
order by title_text desc;

Mysql - data manipulation - update part of value

Mysql table contain column with date type. There are some rows with different dates (i.e. 2008-01-20, 2007-02-25)
Is there some mysql queue syntax to update only values with 2007 year to 2010 without changing month and day.
And the same question about changing only month.
Thanks
You could use something like:
UPDATE table
SET date_column = concat(concat(concat('2010-',MONTH(date_column)),'-'),DAY(date_column))
WHERE YEAR(date_column)=2007
Try this:
UPDATE table1 SET datecolumn = DATE_ADD(datecolumn, INTERVAL 3 year)
WHERE YEAR(datecolumn) = 2007
you can try like this
update table set column = value where YEAR(datecolumn) in(2007, 2008, 2009, 2010)

mysql: update timestamp if timestamp is beyond 10 hours

Wondering if there is a way to do this without using two hits to the sql database.
If a person views content the timestamp is recorded.
If the person views the same content again 2 hours later the timestamp is not updated.
If the person views the same content 10 hours after first viewing the content, update the timestamp db table field.
Any method of doing this via SQL and not doing a "select" than php comparison than an "update" ??
update mytable
set lastvisited=now()
where person='john' and lastvisited<(now()-interval 10 hour);
Try
UPDATE tabel_name SET column_name=NOW() WHERE TIMESTAMPDIFF(HOUR, NOW(), column_name) >= 10

How to calculate time difference in MYSQL

I have Rails application which using MYSQL as database. For some condition, I have to delete all the records from table which was stored exactly 2 hours before the current time.
My query is :
DELETE FROM TABLE_NAME WHERE (NOW() - created_at) > 7200;
Here create_at is datetime column type. Storing the value in the format "2012-12-04 06:39:44"
My problem is, the above query fetch the records even though the record created time is just 40 to 50 minutes and got deleted. The only problem is the record got delete after it reach 40 to 50 minx from it create time.
Can any one please correct my query. I want the MySQL solution. Please help me
You probably need this if you want to delete records created exactly 2 hours ago:
DELETE FROM TABLE_NAME WHERE created_at = NOW() - INTERVAL 2 HOUR
or this, that will delete all records created more than 2 hours ago:
DELETE FROM TABLE_NAME WHERE created_at < NOW() - INTERVAL 2 HOUR
Try this ::
DELETE FROM TABLE_NAME WHERE TIMEDIFF(NOW(),created_at) < '02:00:00';
Try:
DELETE FROM TABLE_NAME WHERE created_at<DATE_SUB(NOW(),INTERVAL 2 HOUR)
This query will delete everything created MORE THAN 2 hours ago. Putting an equal sign would mean EXACTLY 2 hours ago (in second). Of course you can format date to consider only minutes, but that would slow down the query.
If created_at is indexed (and I think it should be) don't perform any functions on it so it can use index to perform delete faster.
I understand you want to delete all records created within a time lapse. So, you shouldn't apply a "greater than" operator to the subtract operation. Instead you should try to specify an appropriated time frame.
You could also take a look to the timediff function http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff
Sorry I'm not able to post the right statement for you, since I don't have a mysql server at hand.