Compare date varchar format in MYSQL - mysql

I have a table with a few columns. One of the column is named flight_date is varchar format.
It has a date stored in it in format like '08/12/2015' which is mm/dd/yyyy
I want to run a query which will return me a list of the records with date in 2016.
Can someone help me out please.
Thanks.

In this case, you can use SUBSTR function for just cut last 4 character.
SELECT * FROM yourtable where SUBSTR(flight_date, -4) = '2016'
Though, it is recommended to store date value in appropriate type column and not in varchar.

Related

How can I convert date format from YYYY-MM-DD to YYYY-MM in sql

I have a specific date in the format of YYYY-MM-DD but want to convert it to YYYY-MM format in a table. I want this change to apply to the full table. can anyone help me out, please?
Thank you.
Assuming that you have a date datatype or the-like (datetime, timestamp), you can use date_format() to represent your date in the target format:
date_format(mydate, '%Y-%m')
This returns a string in the target format. It does not make sense to convert your date column to a string though. Keep that column as it is, and maybe use a computed column to automatically derived the string representation you want:
create table mytable (
...
mydate date,
mynewcol varchar(7) as (date_format(mydate, '%Y-%m'))
)
You can use DATE_FORMAT function of MySQL, for more information please visit
SELECT DATE_FORMAT("2017-06-15", "%Y-%m");

There is a solution to make mysql select a varchar type as date?

I have column inside the database called proxima_cal and its varchar(5) that has a value like 11/16 that represent month/year and I need to select this table.
WHERE proxima_cal BETWEEN "11/16" AND "11/19"
as a varchar BETWEEN doesn't work so what I have to do to mysql conceder this column as date and get correct result ?
You can use the STR_TO_DATE method, something like:
WHERE STR_TO_DATE(CONCAT('01/', proxima_cal), '%d/%m/%y') BETWEEN '2016/11/01' AND '2019/11/01'

Date between query in mysql not generated correct output

I stored date field as Varchar.
When I use this query :
SELECT date
FROM g_m_tit
WHERE date BETWEEN '01.10.2015' AND '31.10.2015';
it generates the wrong output, as shown below
With VARCHAR column, comparing to strings, that will be a character by character comparison, from left to right.
If you want string comparisons to be used for "date" comparisons, the date values will need to be stored in a consistent and canonical format, with the year first, then the month, then the day. e.g. '2016.01.13'.
MySQL provides datatypes other than VARCHAR specifically for storing date and time values... DATE, DATETIME, TIMESTAMP.
Dealing with date values stored in VARCHAR columns, in the format you have, is going to be some messy SQL. And MySQL is going to have to scan all rows to evaluate the expression; it won't be able to use a range scan operation.
One way to do it is to convert the strings into DATE values, and compare the DATE values.
WHERE STR_TO_DATE(`date` ,'DD.MM.YYYY')
BETWEEN STR_TO_DATE('01.10.2015','DD.MM.YYYY')
AND STR_TO_DATE('31.10.2015','DD.MM.YYYY')
If there are any string values in date that can't be converted to a DATE, because the format doesn't match the specification, or an "invalid" date value, e.g. 32.13.2015, the STR_TO_DATE function will return a NULL or throw an error (depending the SQL_MODE setting).
In your image note how the first 2 characters are between '01' and '31'. The between operator works on varchars using varchar "rules" e.g. '19' IS between '01' and '31' and that is why you are getting unwanted results. You are expecting date rules to be applied but your expectation isn't accurate.
Do not store dates as a string; but if you simply had to do it for some reason only a sequence such as YYYYMMDD allows you to reliably use between.
If you persist in storing the column as varchar with the pattern dd.mm.yyyy then try these:
SELECT
`date`
FROM g_m_tit
WHERE str_to_date(`date`,'%d.%m.%Y') BETWEEN '2015-10-01' AND '2015-10-31';
SELECT
`date`
FROM g_m_tit
WHERE str_to_date(`date`,'%d.%m.%Y') >= '2015-10-01')
AND str_to_date(`date`,'%d.%m.%Y') < '2015-11-01';
both of these, as you can see, force you into changing the data, and you would need to do that every time you reference that "date" column - that is very inefficient.
Also, date is a reserved word, please avoid using such words as column names.
A final note: I prefer the second query above as I never use between for date ranges.
create new column with "date" or "bigint" type
Update all rows inserting value from old column to new (based on new column type)
Delete old column
Rename new one to "g_m_tit"
do not use varchar for dates! It causing probelms as u can see and its slower than date/bigint
To store date in:
"varchar(10)" u need 11 bytes + charset collate every query
"bigint" u need 8 bytes (available range condition)
"date" u need 3 bytes (available range, by part of date conditions and much more )

Grouping only by date in a varchar column in mysql

I have a mysql column where the data is stored as VARCHAR though the data values are of datetime in the format of yyyy-mm-dd hh:mm:ss.
Now my task is to group by the date part i.e yyyy-mm-dd by converting VARCHAR to date-time and then just taking date part out of it
QUERY
SELECT SUM(value)
FROM table
GROUP BY name , [date part of the varchar field]
Please let me know if this is at all possible and if yes, how?
Assuming that your data in this varchar field is properly formatted, you can work with the left function, like this:
SELECT LEFT(mydate, 10) AS myval,
SUM(myvalue)
FROM mytable
GROUP BY myval;
If this isn't a big issue; I'd advise converting your varchar column to datetime or timestamp. If not only for the possibly better data storage usage, it'll be way easier to do work with date and time related functions.
Just use the left function. You can leave the date as a string:
SELECT left(datecol, 10) as YYYYMMDD, SUM(value)
FROM table
GROUP BY left(datecol, 10);
I removed name from the group by because it doesn't seem relevant to the question. You can, of course, add it back in.
By the way, MySQL understands this format for dates, so if you really, really want a date:
SELECT date(left(datecol, 10)) as RealDate, SUM(value)
FROM table
GROUP BY RealDate;

Filter dates stored as varchar in mysql

I am working with a MySQL database where dates are stored as varchar like this:
'2013-01-31' in column cl_223
I need to select only records from 2013 so I tried:
SELECT ..
FROM ....
Where cl_223 Like '2013'
But that does not seem to work.
Thanks for all help!
You must add % as a wildcard :
SELECT ..
FROM ....
WHERE cl_223 LIKE '2013%'
Storing a datettime value in a varchar column complicates some functionality on date time operations. But of course you can select your values writing such a query as follow
SELECT * FROM table_name WHERE cl_223 LIKE '2013%'
But if you don't have any performance issue you can convert the varchar column to a datetime value and write stronger typed query like this:
SELECT * FROM table_name WHERE STR_TO_DATE(cl_223,'%Y-%m-%d2') BETWEEN '2013-01-01' AND '2013-12-31'
But if you need a date time value as a date time in your process you'd better store it in a datetime column instead of a varchar column.
The query should be
SELECT ..
FROM ....
Where cl_223 Like '2013%'
However, the better solution would be to store the dates as DATE data types. If the dates in that column are always used in the format they're in now, the change would be backwards compatible. It would also allow for easier processing of the date values.