Convert Mysql Date (NOW()) to bigint - mysql

i have a mysql trigger that updates a table when there's an insert in another table. My question is this, how can i convert the current date to bigint value, because it's the bigint value that needs to be updated. This is the update statement
UPDATE clocks SET last_clock_upload = NOW() WHERE clock_id = NEW.clock_id
How can i change the NOW() to bigint?

Try this:
SELECT UNIX_TIMESTAMP(NOW( ) )

Related

How to update timestamp values in column by adding a specific time (seconds) to the existing timestamp using mysql?

I am using mysql and pma. I have a table mytable and a column time, storing ~17K individual values, i.e. timestamps (integers).
I need to update each by adding 962758 to each timestamp. What does the SQL command for that look like?
SELECT (*) FROM `mytable` t1
UPDATE `mytable` SET time = + 962758
PROFIT? :)
Would you need a SELECT statement for that or does it work with UPDATE only?
I cant use php for that in this case.
Considering that it's TIMESTAMP datatype, you can say
UPDATE `mytable` SET time = time + INTERVAL 962758 seconds;
Per your comment, since it's of INT type; you can just do the addition likewise you are already doing.
UPDATE `mytable` SET `time` = `time` + 962758;
If the data is stored as a datetime value, then you simply can use:
select timestampadd(second, 962758, time)
If the value is a unix timestamp, then it is already in seconds, and you can just add 962758.
According to your comment the field is int
UPDATE `mytable`
SET time = time + 962758;
And no need select

Unable to convert varchar to datetime in MySql

In my MySql table there's a column called _time of type varchar. The values it holds are in the format: year month day hour minute without the whitespaces: 201409201945 I want to convert it to datetime so I'm doing this:
ALTER TABLE `my_table` CHANGE COLUMN `_time` `date_time` DATETIME NOT NULL;
And it throws this error for some reason:
Error Code: 1292. Incorrect datetime value: '201409201945' for column '_date_time' at row 1 0.036 sec
The three steps #Arkain mentioned would be with the help of the function STR_TO_DATE
-- add the new column
ALTER TABLE `my_table` ADD COLUMN `date_time` DATETIME;
-- update the new column with the help of the function STR_TO_DATE
UPDATE `my_table` SET `date_time` = STR_TO_DATE(`_time`, '%Y%m%d%H%i');
-- drop the old column
ALTER TABLE `my_table` DROP COLUMN `_time`;
The complete list of specifiers for STR_TO_DATE can be found at DATE_FORMAT, here an excerpt with those I used:
%d Day of the month, numeric (00..31)
%H Hour (00..23)
%i Minutes, numeric (00..59)
%m Month, numeric (00..12)
%Y Year, numeric, four digits
Demo of the UPDATE
If the new column should have the attribute NOT NOLL, one way could be to set the sql mode before the operation to '' and reset the sql_mode later on:
SET #old_mode = ##sql_mode;
SET ##sql_mode = ''; -- permits zero values in DATETIME columns
ALTER TABLE `my_table` ADD COLUMN `date_time` DATETIME NOT NULL;
UPDATE `my_table` SET `date_time` = STR_TO_DATE(`_time`, '%Y%m%d%H%i');
ALTER TABLE `my_table` DROP COLUMN `_time`;
SET ##sql_mode = #old_mode;
Updated Demo
If your varchar data were formatted like this '2014-09-20 19:45' altering your column's data type would work. Why? that's the character representation used by DATETIME and other time-oriented data types.
But it isn't. So, what choices do you have?
One is to use these four steps:
alter the table to add a new DATETIME column with a temporary name
do an UPDATE with no WHERE clause to fill in the values of that column
alter the table to drop the previous column
alter the table to rename your new column to have the same name as the column you just dropped.
Here's how that would go.
ALTER TABLE my_table ADD COLUMN tempstamp DATETIME
UPDATE my_table SET tempstamp = STR_TO_DATE(_time, '%Y%m%d%H%i')
ALTER TABLE my_table DROP COLUMN _time
ALTER TABLE my_table CHANGE tempstamp _time DATETIME NOT NULL
Another approach: Change the strings in your _time to valid datetime values, then alter your column. If your varchars() are wide enough to hold a few extra characters, try this.
UPDATE my_table SET `_time`=STR_TO_DATE(`_time`, '%Y%m%d%H%i')
ALTER TABLE my_table CHANGE `_time` `_time` DATETIME NOT NULL
This works because STR_TO_DATE() makes DATETIME values of your strings, and then MySQL casts them back to strings to store back into your varchar column. Then you can change the datatype to DATETIME.
You probably noticed I threw in NOT NULL. If you're going to put an index on that column, NOT NULL is a good thing to have. But, if some of your time values are missing, it won't work.
Because the database doesn't know what to do with 201409201945, it's not a valid DateTime format, therefore it can't change it
You can delete the data that is in it already, and then try changing it

I stupidly made a row with dates as VARCHAR, can I still do date based selects on it?

So I have a large table that simply cannot be altered without breaking my PHP app. Stupidly (yes I know), I made a start date and end date as VARCHAR with data stored as '03/04/2013' for example.
Now i need to be able to search to see which rows are currently 'active' meaning which rows have a start date before today AND and end date after today.
Is this at all possible with an SQL query?
Action plan to migrate VARCHAR columns to DATE without breaking the application:
Create new indexed DATE columns and fill them with the respective values in the VARCHAR columns:
-- new column
ALTER TABLE MY_TABLE ADD `NEW_DATE_COLUMN` DATE;
-- index
CREATE INDEX `MY_TABLE_NEW_DATE_IDX` ON MY_TABLE(`NEW_DATE_COLUMN`);
-- initial values
UPDATE MY_TABLE
SET `NEW_DATE_COLUMN` = STR_TO_DATE(`VARCHAR_DATE`, '%d/%m/%Y')
WHERE `NEW_DATE_COLUMN` IS NULL;
Create insert / update triggers to cast your VARCHAR columns to DATE and update your new DATE columns with their respective values:
-- triggers
DELIMITER //
CREATE TRIGGER `MY_TABLE_VARCHAR_DATE_BI` BEFORE INSERT ON MY_TABLE
FOR EACH ROW
BEGIN
IF NEW.`NEW_DATE_COLUMN` IS NULL AND NEW.`VARCHAR_DATE` IS NOT NULL THEN
SET NEW.NEW_DATE_COLUMN = STR_TO_DATE(NEW.`VARCHAR_DATE`, '%d/%m/%Y');
END IF;
END;
//
CREATE TRIGGER `MY_TABLE_VARCHAR_DATE_BU` BEFORE UPDATE ON MY_TABLE
FOR EACH ROW
BEGIN
IF NEW.`NEW_DATE_COLUMN` IS NULL AND NEW.`VARCHAR_DATE` IS NOT NULL THEN
SET NEW.NEW_DATE_COLUMN = STR_TO_DATE(NEW.`VARCHAR_DATE`, '%d/%m/%Y');
END IF;
END;
//
DELIMITER;
Use the DATE columns in your queries:
SELECT * FROM MY_TABLE
WHERE `NEW_DATE_COLUMN` BETWEEN
CURRENT_DATE AND DATE_ADD(CURRENT_DATE, INTERVAL 1 DAY);
Take your time and update your application to get ride of places that uses the original VARCHAR columns directly, meanwhile nothing will be broken.
When you are done remove the triggers and the VARCHAR columns:
DROP TRIGGER `MY_TABLE_VARCHAR_DATE_BI`;
DROP TRIGGER `MY_TABLE_VARCHAR_DATE_BU`;
ALTER TABLE MY_TABLE DROP `VARCHAR_DATE`;
Working SQL Fiddle.
Yes you can do that.
Try something like this:-
select date_format(str_to_date('03/04/2013', '%d/%m/%Y'), '%Y%m');
or may be this:-(Just a small change with month and days as I am confused with 03 and 04)
select date_format(str_to_date('03/04/2013', '%m/%d/%Y'), '%Y%m');
OR you may also try to convert your column back to date like this:
UPDATE `table`
SET `column` = str_to_date( `column`, '%d-%m-%Y' );
Use STR_TO_DATE as follows:
WHERE STR_TO_DATE(start, '%d/%m/%Y') < DATE(NOW())
AND STR_TO_DATE(end, '%d/%m/%Y') > DATE_ADD(DATE(NOW()), INTERVAL 1 DAY)
UPDATE `table`
SET `yourColumn` = str_to_date( `yourColumn`, '%d-%m-%Y' );
Convert it do date type without losing your data,add minutes or seconds as needed it.IT will be easier in the long run,but if you prefer dabbling in php..
Or create a new column date type from the varchar one.

why does mysql update erases field data?

mysql update deletes the field data on some rows rather than update them when i run this mysql statement
UPDATE
tablename
SET
date = DATE_FORMAT(STR_TO_DATE(date, "%d/%m/%Y"), "%Y-%m-%d")
I am trying to update date formats from 05/08/2013 to 2013-05-08 to make it so I can search where date between $date and $date
try this:
UPDATE tablename SET `date` = STR_TO_DATE(`date`, '%d/%m/%Y')
and then change the datatype of the column like this
ALTER TABLE tablename CHANGE COLUMN `date` `date` DATE
OR ALternatively
update tablename SET date = DATE_FORMAT(STR_TO_DATE(date,'%d/%m/%y'),'%y-%m-%d')
You should make sure that your date field is true DATE or DATETIME datatype. After that,
UPDATE tablename
SET `date` = STR_TO_DATE(old_date, '%d/%m/%Y')
Then, you would be able to do all normal math on dates: add, subtract, compare, sort, etc.
MySQL STR_TO_DATE() returns a datetime value by taking a string and a specific format string as arguments.
If the date or time or datetime value specified as string is illegal, the function returns NULL so check all value in select statement first to know if its returning null
Select DATE_FORMAT(STR_TO_DATE(date, "%d/%m/%Y"), "%Y-%m-%d") from tablename

Convert varchar column to date in mysql at database level

I have one column date1 which is varchar type
I want this column to date type.
I tried changing field but all date is converted to 0000-00-00.
format is dd-mm-yyyy but in varchar.
How can I convert the same date format but with date format using sql queries or similar but at database level ?
UPDATE `table`
SET `column` = str_to_date( `column`, '%d-%m-%Y' );
More about STR_TO_DATE function.
Since your column name is date1, you can replace column with date1 in the above syntax, and the code shall be:
UPDATE `table`
SET `date1` = str_to_date( `date1`, '%d-%m-%Y' );
The other answers here are risky, because if they go wrong you'll lose your data. A safer way to do this is to create a new field on your database with a DATE (or DATETIME if you need time as well) format, then to run a query like
UPDATE `table` SET `my_new_date_field` = STR_TO_DATE( `my_old_data_field`, '%d/%m/%Y');
In this way, if the %d/%m/%Y bit is wrong, you won't lose your data.
Once you're happy, you can delete the old data field and rename the new one.
use STR_TO_DATE Function of MySQL
FIRST you will need to update the value in date format.
UPDATE `tbl` SET `date1` = STR_TO_DATE(`date1`, '%d-%m-%Y') WHERE 1=1
THEN Convert the field to date.
Most importantly remember to insert date as Y-m-d format, after then.