How to cast this integer value to date in MySQL - mysql

i have airbnb data and i want to cast column last_reviews (which datatype is int) to date
this is my sql code http://sqlfiddle.com/#!9/b5ea42/31
how do i cast int > date?
or how do i create last_reviews column as datatype date?

The last_review date seems to be the number of days since 1900-01-01 so in MySQL you would:
SELECT '1900-01-01' + INTERVAL last_review DAY AS last_review_date
FROM ...
The result checks out for the sample data (which you should delete).

last_review values looks like VB(A) numeric representation for dates. If so then test this:
SELECT last_review, '1900-01-01' + INTERVAL last_review DAY
FROM airbnb
Adjust constant part (maybe it must be '1899-12-31'?) if needed.

Related

Have a table that includes startdate and durationminutes. Trying to get that to give new end date in SQL

Title says it all.
Table looks like this
Table: Time
First Column: startdate (shown as yyyy-mm-dd hh:mm:ss)
Second COlumn: durationminute (shown in minutes)
Since the above doesn't give me an end date, I'm trying to create a query which will do that.
I'm assuming this is going to be a dateadd function but I can't get it to work. The durationminutes is a variable to is dependent on that one row so date_add(minutes, 30, startdate) wouldn't work as there is no constant.
I've tried
date_add(days, durationminutes/1440, startdate)
But am getting a syntax error.
MySQL understands date arithmetics. Assuming that startdate is of date or datetime datatype (as it should be!), you can do:
select t.*,
startdate + interval durationminute minute as enddate
from time t

convert NOW() to d-m-Y

I am trying to compare dates in this format : 'dd-mm-yyyy' that are actually VARCHAR in the DB.
example of dates i have :
26-11-2011
14-04-2009
27-02-2010
03-11-2020
04-06-2021
my query :
SELECT table
FROM database
WHERE STR_TO_DATE(expiration, '%d-%m-%Y') <= DATE_FORMAT(NOW(), '%d-%m-%Y');
when I run this query it returns 0 rows when I know I have dates corresponding to the condition.
(as shown above, should returns rows like 26-11-2011
Thing is, when i run the query with >= it returns some good results :
03-05-2021
05-05-2021
01-05-2021
Thanks!
You are mixing DATE types and Strings holding a date representation.
You are converting your VARCHAR date to a DATE type using STR_TO_DATE() so use CURDATE() which also returns a valid DATE type so the comparison is comparing Apples with Apples
WHERE STR_TO_DATE(expiration, '%d-%m-%Y') <= CURDATE();

Only SELECT yesterdays date with weird date format

I need to only SELECT data where the date field is yesterdays date. The only problem I'm having is that the data in the date field looks like the following 20160412 062815.000
I don't really care about the time, I just want to search dynamically for anything with yesterdays date. I've tried a multitude of CURDATE() -1 but I'm unsure how to just search the first 8 digits of the field.
Assuming the date value is stored as a string, and the first 8 characters are always the date in YYYYMMDD format, then you can use a query like this:
select *
from your_table
where your_column like concat(date_format(current_date() - interval 1 day,'%Y%m%d'),'%')
One advantage of this query is that it can leverage an index on your date field, unlike the other answers so far.
Format yesterday's date to a number and convert the date string also to a number.
select * from your_table
where date_format(curdate() - interval 1 day, '%Y%m%d') * 1 = date_col * 1
SQLFiddle demo
*1 is a math operation that forces MySQL to convert strings to a number.
you can use subdate(currentDate, 1)
select * from your_table
where subdate(currentDate, 1) = DATE(your_date)

how do I select on date equality in a mySQL query?

I want to select rows from a table given a particular date of record in mysql
SELECT * from TABLENAME WHERE FROM_DATE='06/11/2012'
I am not getting anything useful.
First of all, you should use the standard date format Y-m-d - otherwise you have to make some nasty queries and sorting is a real b*tch.
Using the standard date format you can easily do something like this:
SELECT * FROM tablename WHERE from_date > '2012-06-11'
DATE comparisons are very likely what you want here. If your from_date column has the data type of DATE, then your code should be safe and robust if you do this:
WHERE from_date = STR_TO_DATE('06/11/2012', '%m/%d/%Y')
#Repox pointed out that you might consider putting your date literals in the canonical format '2012-06-11'. That's true, if you can do it. But STR_TO_DATE will do it for you if you need it to. There's a list of the %x conversion items here. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
If you're using DATETIME data types, beware: comparisons are more complex than they seem. DATETIME items are like floating point numbers: if one of them exactly equals another it's only by coincidence. That's because they represent moments (milliseconds) in time, not just days.
Presuming your from_date column has the DATETIME type, you should use
WHERE from_date >= STR_TO_DATE('06/11/2012', '%m/%d/%Y')
AND from_date < STR_TO_DATE('06/11/2012', '%m/%d/%Y') + INTERVAL 1 DAY
This will catch all moments in time on the day you want, up to but not including the first moment of the next day.
If your from_date items are represented as character strings, take the trouble to convert them to DATE or DATETIME data types. Seriously. Your results will be far better.
SELECT * from TABLENAME WHERE FROM_DATE='2012/06/13'
It would be better if you use the DATE() function of mysql
SELECT * FROM tablename WHERE DATE(from_date) > '2012-06-11'
Because, if the datatype of the from_date you set as TIMESTAMP or DATETIME then it won't return the correct results sometimes when you directly use the '>' symbol

Mysql date interval

I am trying to select rows which are within a certain time period:
$sql = mysql_query(" SELECT * FROM table WHERE ... AND
date BETWEEN ('$date' - Interval 2 YEAR) AND ('$date' + Interval 2 YEAR)
OR date ISNULL");
The format of the dates saved in this column are mm/dd/yyyy
a. how do I select the time interval? currently it is selecting all the rows, not just those within this date range.
b. Am I using the corret syntax to make sure we include all null options as well?
Update.
Following the answers and comments I changed the column type to "Date" (it was varchar), and have also changed the current type of the existing entries to the correct date format.
I now have 2 questions:
a. This is still not working... What else might be the problem?
b. I want to change in PHP where the entry is saved. How ahould I go about this? Is the following correct:
$new_date = mysql_query("STR_TO_DATE($old_date_string,'%m/%d/%Y')");
Solution.
First, I changed the column type to DATE (from VARCHAR).
Second, I moved and converted all the existing entries from their existing column and type (str) to new type (date) and new column.
Third, I added parentheses to the "date" section.
Fourth, I changed ISNULL to IS NULL - addin a space, which changes it from a function to a statement.
Fifth, I am now updating my script so that in the future dates will be saved to the new column in the new type, converting them to the correct fprmat before saving them to the database.
Thankyou #nnicholas and everybody.
If you are really storing your dates as strings I suggest you update your table with something like the following -
-- add a new column of type DATE
ALTER TABLE `table` ADD COLUMN `date_new` DATE AFTER `date`;
-- populate the new column from the old one
UPDATE `table` SET `date_new` = STR_TO_DATE(`date`,'%m/%d/%Y');
-- drop the old column
ALTER TABLE `table` DROP COLUMN `date`;
-- rename the new column
ALTER TABLE `table` CHANGE `date_new` `date` DATE;
After making these changes handling dates will be much easier for you. When inserting the dates that you are receiving you simply use STR_TO_DATE('new_value','%m/%d/%Y') to convert the date during the insert -
mysql_query("INSERT INTO `table` (field1, field2, `date`)
VALUES('value1', 'value2', STR_TO_DATE('$old_date_string','%m/%d/%Y')");
This assumes that $old_date_string has already been sanitised.
With your table structure updated the standard date arithmetic functions will work as intended.
$sql = mysql_query("SELECT *
FROM table
WHERE ...
AND (`date` BETWEEN ('$date' - INTERVAL 2 YEAR) AND ('$date' + INTERVAL 2 YEAR) OR `date` IS NULL)");
$date must be in Y-m-d format.
You can't do date comparisons on it if it's not stored in the database as a date.
You could do something very ugly casting it to a date with STR_TO_DATE() and then comparing but I'd suggest reworking your table structure to store this date as a DATE object.
SELECT * FROM table WHERE ... AND
STR_TO_DATE(date, '%m/%d/%Y') BETWEEN ('$date' - Interval 2 YEAR) AND ('$date' + Interval 2 YEAR)
OR date ISNULL
This assumes that $date is in the correct MySQL DATE format of 2012-03-14 (for 14th March 2012)
You have to CAST/CONVERT your date field that you are getting from MySQL (you really called a date field 'date'? that's a bad idea).
The best answer is to convert your date column to an actual DATE type. You should also not really use the mm/dd/yyyy format as its ambiguous.
You need to enclose the entire date checking part in brackets, like so:
$sql = mysql_query(" SELECT * FROM table WHERE ... AND
(
date BETWEEN ('$date' - Interval 2 YEAR) AND ('$date' + Interval 2 YEAR)
OR date ISNULL
)");
And it should work as expected.
Apart from the missing brackets, your condition seems to take into account NULLs all right (i.e. NULLs would be included in the output regardless of the argument passed).
UPDATE
It seems I was mistaken in presuming that the date column was the date type. #James C has got the point, as well as #nnichols, and their suggestion about changing the column type seems the way to go in your situation.