ORDER BY STR_TO_DATE() not working in phpmyadmin sql - mysql

I have dates in varchar type like:
201601
201602
201603
201701
201702 and so on
I am trying to view all my records where the dates are in ascending order. So I am using this query:
SELECT * FROM emp_pp GROUP BY YEARMM ORDER BY STR_TO_DATE(YEARMM,'%Y%m')
Here YEARMM is my column name. The query isn't working properly when I run it. Instead I keep getting all these notices:
Incorrect datetime value: '201601' for function str_to_date
Incorrect datetime value: '201602' for function str_to_date...
Why is that? Please help me

As mentioned by Akina, STR_TO_DATE function requires enough data to generate at minimum a full date value to work correctly. You do not have that (you can not have a date 2016-02-00, for example).
What you do have is a numerical Year and Month integer, 201601, 201602, 201603, 201701 etc. which orders exactly as you want by standard numerical ordering so all you need to do is remove the STR_TO_DATE part entirely:
SELECT * FROM emp_pp GROUP BY YEARMM ORDER BY YEARMM ASC /* Oldest date first */
Of note:
SELECT * is highly inefficient. You should name each column you want to collect.
Column names in SQL should not be upper case, as this is hard to read with the correct case syntax, SQL column should be only lower case; yearmm would be more readable in your SQL code.

Related

How to Query between a date range in MySQL

I'm querying
SELECT * FROM tempLog WHERE date BETWEEN '23-03-2017' AND '02-04-2017'
and the result is null. How to fix this. But
SELECT * FROM tempLog WHERE date BETWEEN '23-03-2017' AND '30-03-2017'
giving me the correct result.
Note:- tempLog is the table name.
You should store dates in date format or atleast correctly formatted string (YYYY-MM-DD).
For now you can use str_to_date to convert the string to date and compare:
select *
from tempLog
where str_to_date(date, '%d-%m-%Y') between '2017-03-23' and '2017-04-02';
However note that this will hinder the optimizer from using index on the column if any.
The correct remedy of the situation is fixing the table structure.
According to the documentation, you're supposed to use this format when writing a date: 'YYYY-MM-DD' (although it says it may accept 'YYYYMMDD' or even YYYYMMDD in some contexts).

sql error using between operator with date between two dates

i have an events table having start date and end date I am trying retrieve all the records by giving a date that is between start and end dates.
eg :
SELECT *
FROM `events`
WHERE '2017-01-29' BETWEEN start_date='2017-01-28'
AND end_date='2017-01-31'
but response is syntax error can any one help me to finish the query
Just list the columns.
WHERE '2017-01-29' BETWEEN start_date AND end_date
The values come from the table, you don't put them into the query.
According to mysql documentation (https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html#operator_between) the syntax for BETWEN is
expr BETWEEN min AND max
it is not
expr BETWEEN blabla=min AND stuff=max
Also, it is rather pointless to be using constants in all three expressions, because in this case the result will be known in advance (either always TRUE or always FALSE) without having to consult the values in your table.
It is kind of hard to give you an example without knowing the structure of your table, but what you probably want is something like
WHERE '2017-01-29' BETWEEN start_date
AND end_date
(assuming start_date and end_date are columns in your table)
or something like
WHERE some_column BETWEEN '2017-01-28'
AND '2017-01-31'
(assuming some_column is a column in your table.)
I believe you're trying to find all the rows where a date is 2017-01-29, and so, your query could be:
SELECT *
FROM `events`
WHERE
date = '2017-01-29';
If, however, you want all rows with date between 2017-01-28 and 2017-01-31, then you could do:
SELECT *
FROM `events`
WHERE
date BETWEEN '2017-01-28' AND '2017-01-31';
Instead of putting 2017-01-29 before WHERE, put the name of the field you want to filter by date, such as EventDate (or whatever your field is named).

SQL Server: Want to use between clause with dates, but dates in string form (YYYY.MM.DD)

Help! One column in my database is for dates. All of my dates are unfortunately in the String form (YYYY.MM.DD). I have a MASSIVE database (300+GB) so ideally would like to avoid transformations.
Is there a way I can select rows for dates in between YYYY.MM.DD and YYYY.MM.DD? What would the script look like?
Thank you!
If the months and days are stored with leading zeroes, the BETWEEN operator will work as expected. So will ORDER BY.
create table your_table (
date_value varchar(10) not null
);
insert into your_table values
('2013.01.01'), ('2013.01.13'), ('2013.01.30'), ('2013.01.31'),
('2013.02.01'), ('2013.02.13'), ('2013.02.28'), ('2013.02.31'),
('2013.03.01'), ('2013.03.15'), ('2013.03.30'), ('2013.03.31');
select date_value
from your_table
where date_value between '2013.01.01' and '2013-01-31'
order by date_value;
2013.01.01
2013.01.13
2013.01.30
One of the main problems with your structure is that you lose type safety. Look at this query.
select date_value
from your_table
where date_value between '2013.02.01' and '2013.02.31'
order by date_value;
2013.02.01
2013.02.13
2013.02.28
2013.02.31
If you'd used a column of type date or datetime or timestamp, the dbms would not have allowed inserting the values '2013.02.31', because that's not a value in the domain of date. It is a value in the domain of varchar. (And so is "Arrrrgh!", unless you've got a CHECK constraint on that column that severely restricts the acceptable values.)
Not good solution, but works (cost much performance).
You have formated date in order year, month, day (good order to compare strings, without transformation to datetime), so you can try
SELECT * FROM Table WHERE StringDate > '2013.07.10' AND StringDate < '2013.07.14'
It returns bad results if there are dates before year 1000 without leading zero ('999.07.14').
But I dont know how it works on big database.
SQL Fiddle
Between in SQL is inclusive of both bounds. If that is what you want, you can just use between:
where col between 'YYYY.MM.DD' and 'YYYY.MM.DD'
Where the two constants are whatever values you are looking for.
If you have an index on the column, then between (as well as >, >=, and so on) will use the index. You do not need to transform the values. If your constants are dates of one form or another, then you can use date_format() to create a string in the right format. For instance, to get dates within the past week:
where col >= date_format(adddate(now(), -7), '%Y.%m.%d')

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.

Mysql Date manipulation

I have a table with different dates and one of them is separated in 3 fields:
PassDay, PassMonth, PassYear.
I want to compare these dates with an other datetime orig_date so what I did is
select `orig_date`, CONCAT_WS('-',`PassYear`,`PassMonth`,`PassDay`) as `pass_expiration` where `pass_expiration` < `orig_date`;
The problem is that the month is stored as a month name (January) and not a number (01), so my result for pass_expiration is 2013-April-15 for example.
Is there a way to transform this (2013-April-15) to a regular datetime directly from the query?
You want str_to_date:
where STR_TO_DATE(CONCAT_WS('-',PassYear,PassMonth,PassDay),'%Y-%M-%d') < orig_date
Also, as a note, you can't reference a computed column in the where clause. Only in the order by clause (where is a predicate that's used to filter results from the table; order by is run after the column set is calc'd).