I'm looking to create a date time field in a MySQL script that has a specific date and time.
I've tried using
CONCAT(DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 9 DAY) ,'%Y%m%d'), ' 13:00:00')
but it doesn't insert correctly.
How can I achieve this so that it will insert a date time with the time as above?
It inserts the record as 0000-00-00 00:00:00 with the above
mysql accepts datetime values in yyyy-mm-dd hh:mm:ss format. In your formula you do not separate the year, month, day values, hance the result is not in the date format mysql expects the dates in. Change it to:
CONCAT(DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 9 DAY) ,'%Y-%m-%d'), ' 13:00:00')
But I do not really understand why you need to do the formatting, just use CURDATE() function instead of the NOW():
CONCAT(DATE_SUB(CURDATE(), INTERVAL 9 DAY), ' 13:00:00')
Related
I have a database where data_fine is defined as TEXT and contains values such as "25-05-2021". I need to find all the records between the current date up to 8 days.
I tried the following query, but nothing is displayed.
SELECT * from tabella_raw where data_fine > DATE(NOW) and data_fine < DATE(NOW() + INTERVAL 8 DAYS)
What is the best and safe way to compare the date stored as TEXT with the current date?
You have to convert dates which cost a lot of processor time, so you should avoid that and save all in MySQL date yyyy-MM-dd hh:mm:ss
Also you can use CURDATE() to get the the current date
Last the parameter fo INTERVAL IS DAYnot DAYS
SELECT STR_TO_DATE("25-05-2021",'%d-%m-%Y');
SELECT * from tabella_raw where STR_TO_DATE(data_fine,'%d-%m-%Y') > Curdate() and STR_TO_DATE(data_fine,'%d-%m-%Y') < CURDATE() + INTERVAL 8 DAY;
Try use STR_TO_DATE function
SELECT * from tabella_raw where STR_TO_DATE(data_fine, '%d-%m-%Y') > DATE(NOW) and STR_TO_DATE(data_fine, '%d-%m-%Y') < DATE(NOW() + INTERVAL 8 DAYS)
You are trying to use a text field as a date, so you need to convert the text to a date to use date functions.
I want to fetch all the products which are near to "expire".
Date format on expiry date column exDate is (Y-m) or YYYY-MM, for example: 2018-03.
I am trying with this query but it fetches all record from the table.
SELECT * FROM product WHERE (exDate BETWEEN DATE_FORMAT(NOW() ,'Y-m') AND NOW()+ interval 2 month)
Turn exDate from the YYYY-MM format into the mySql date format YYYY-MM-DD then perform the query on that value:
SELECT * FROM product WHERE STR_TO_DATE( `exDate`, "%Y-%m" ) BETWEEN CURDATE() AND DATE_ADD( CURDATE(), INTERVAL 2 MONTH)
Take exDate and turn into mySql date format YYYY-MM-DD with
STR_TO_DATE( `exDate`, "%Y-%m" )
Note the % symbol is needed to specify the format.
Then this is the value that must fall into the interval.
Note you can just use CURDATE() to get the current date into date format.
To add a time interval to a date you have to use the mySql function DATE_ADD()
Note that as of today 2018-03-17 you will catch products with exDate with values 2018-04 and 2018-05.
2018-03 is not part of the results as it's turned into 2018-03-01 and is before today (the lower limit).
2018-05 is part of the results because it's turned into 2018-05-01 and is before the upper limit DATE_ADD( CURDATE(), INTERVAL 2 MONTH) that evaluates to 2018-05-17
I am trying to update the filed date in mysql database , this field contains unix-timestamp date.
But I do not want to convert the whole of date 1364562954 , I want to replace only the year and leave the day, month and time same.
For Example - 19-03-2015, 03:43 PM to 19-03-2016, 03:43 PM
You can use FROM_UNIXTIME and UNIX_TIMESTAMP to convert a Unix timestamp to MySQL's DATETIME format.
So for example to get a timestamp incremented by one year:
SELECT UNIX_TIMESTAMP(FROM_UNIXTIME(1364562954) + INTERVAL 1 YEAR);
Result: 1396098954
For UPDATE it is the same:
UPDATE t SET tstamp= UNIX_TIMESTAMP(FROM_UNIXTIME(tstamp) + INTERVAL 1 YEAR);
Reference: Date and Time Functions
I have date column field it's data look like 2014-05. how i get previous 6th month interval. My mysql query below
SELECT name,grade from users where data_year BETWEEN DATE_FORMAT('2014-04', '%Y-%m') - INTERVAL 12 MONTH AND DATE_FORMAT('2014-04', '%Y-%m')
I got result when i put with days like 2014-04-31
The strings you used aren't valid dates as they don't contain the day. If you want to use a date literal in a SQL statement you should use the form DATE '2014-04-01', eg
SELECT name,grade
from users
where date_column BETWEEN DATE '2014-04-01' - INTERVAL 6 MONTH
AND DATE '2014-04-01'
UPDATE
From the comments it seems the question actually is - how to parse an incomplete date string. You can use STR_TO_DATE for that, but you'll have to append -01 at the end, otherwise you'll get an invalid date with 0 for the day of month, ie 20140-04-00 instead of 2014-04-01.
I have a MySQL column which holds expiration dates in mm/dd/yy format.
How can I get records that are going to expire within 2 days from today?
Try:
SELECT * FROM TABLE WHERE CAST(EXPIREDATE AS DATETIME)
BETWEEN NOW() AND DATE_ADD(NOW(), INTERVAL 2 DAY);
http://www.roseindia.net/sql/sqldate/sql-date-add-days.shtml