MYSQL query: select between date with local format - mysql

my mysql database tb_date (varchar 20):
16 November 2014
06 December 2014
01 April 2014
12 April 2015
I want select between 01 January 2014 until 31 December 2014, how the query is with date conversion?
thanks..

This is an anti-pattern, storing date values in VARCHAR columns, rather than using datatypes specifically designed and implemented for storing date values... DATE, DATETIME or TIMESTAMP.
To answer your question, before it gets closed, you could use the STR_TO_DATE function to convert the strings into DATE datatype, and then do the comparison. MySQL won't be able to make use of an index range scan operation, it will need to evaluate that function on every flipping row in the table.
As an example:
SELECT t.mycol
FROM mytable t
WHERE STR_TO_DATE(t.mycol,'%d %M %Y') >= '2014-01-01'
AND STR_TO_DATE(t.mycol,'%d %M %Y') < '2015-01-01'
We'll need to check the MySQL Reference Manual to verify that '%M' is the right format specifier for the full month name...
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format
Yes, it looks like I guessed right. M is the month name.

As I already commented, you should store your date as Timestamp or Data format then you could simply compare.
However, there is still a solution.. You can convert the varchar to a date directly in your query :
select * from yourTable
where (str_to_date(tb_date, '%d %M %Y') between '2014-01-01' and '2014-12-31');
But please don't use this hack and change your date format...
Edit : If you are really willing to use varchar to store your date, change it to varchar(17) which is the max character possible using your string format.

Related

Time Format Problem: Comparison is not working

MYSQL
Format of time in two tables:-
Jan 1 2018 10:42:06
table1.time greater than table2.time
Comparison is not working in these tables
table1.time > table2.time
No data is coming
You seem to be looking for :
STR_TO_DATE(a.time, '%b %d %Y %H:%i:%d')
The mysql STR_TO_DATE function lets you translate strings to date.
Once strings are converted to date, you can compare them using the < operator or the-like

How to convert varchar date (Mon, 02 Jan 2017 15:46:23 GMT) to date formated data in mysql

In my database table one field upload_datetime is there and it is having a value like Mon, 02 Jan 2017 15:46:23 GMT. I want to get a formatted date like 2017-01-02 15:46:23. Please somebody help me with getting it in select query.
I tried with STR_TO_DATE,CAST but I am not getting any output. It's returning NULL.
You can use
DATE_FORMAT(NOW(),'%d %b %Y %T:%f')
The NOW() returns the present datetime and you can replace it with other datetime that you want
In my database table one field upload_datetime is there and it is having a value like Mon, 02 Jan 2017 15:46:23 GMT.
This statement might be a bit misleading, because we don't need to be particularly concerned with how a datetime is stored internally. Your exact question is to how to format that datetime in a certain way.
The DATE_FORMAT() function is one way to go here. It accepts as input a time column, and a format mask, and returns a formatted string.
SELECT DATE_FORMAT(upload_datetime, '%Y-%m-%d %H:%i:%s') AS upload_formatted
FROM yourTable
I got the answer. after doing some R&D i found the way.Thanks all for your comment.
SELECT date_format(STR_TO_DATE(concat(substr(datetime_uploaded ,6,2),"-",substr(datetime_uploaded ,9,3),"-",substr(datetime_uploaded ,13,4)),'%d-%M-%Y'),'%Y-%M')
FROM OpsMetrics

varchar date time comparison issue

I have a legacy table which has a varchar column represent date, format is MM/DD/YYYY (e.g. 01/08/2015). It is not convenient to perform data range selection since it is a varchar (when I use < or > kinds comparison, it goes to varchar/string comparison, which have different results from date comparision).
For example, I want to select only rows which dates are between 01/08/2015 and 01/10/2015. Any smart solution is appreciated, and I cannot change the data type of varchar to date in my existing table.
I am using MySQL Workbench/MySQL.
Varchar dates are evil and they are not real date, the best solution is to use mysql's native date data types.
Since you can't change the datatype you may use str_to_date() function and here how it works
mysql> select str_to_date('01/08/2015','%d/%m/%Y') as d ;
+------------+
| d |
+------------+
| 2015-08-01 |
+------------+
So the query for select would be
select * from table_name
where
str_to_date(date_column,'%d/%m/%Y')
between
str_to_date('01/08/2015','%d/%m/%Y')
and
str_to_date('01/10/2015','%d/%m/%Y')
There are many answers which addresses many different way of converting the string to date.
You may choose whichever is perfect for your need
SELECT * FROM `your_table` WHERE DATE_FORMAT(my_column_with_the_string_date, "%Y-%m-%d") <= '2011-09-30'
DATE_FORMAT can be used to convert your date string to any format: I will use the NOW() function instead of string to list different
formats that are supported
DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')
DATE_FORMAT(NOW(),'%m-%d-%Y')
DATE_FORMAT(NOW(),'%d %b %y')
DATE_FORMAT(NOW(),'%d %b %Y %T:%f')
The output of the above is:
Nov 04 2014 11:45 PM
11-04-2014
04 Nov 14
04 Nov 2014 11:45:34:243
You can modify your query accordingly
You can cast your dates as strings using STR_TO_DATE:
STR_TO_DATE(yourdatefield, '%m/%d/%Y')
SELECT STR_TO_DATE(got_fired_at, '%m/%d/%Y') BETWEEN ? AND ? FROM firings;
(field/table names guaranteed to have been chosen randomly)
Use MySQL's STR_TO_DATE function to parse the date strings to date objects then do the comparison.

sql search between date Error

I want to retrieve data between two specific date and I am getting that perfectly right with this query
select * from POS.dbo.voucher where date_time between '10 october 2014 00.00.00 ' and '11 october 2014 12.00.00'
but the issue is if I'm changing the month something like
select * from POS.dbo.voucher where date_time between '10 march 2014 00.00.00 ' and '11 march 2014 12.00.00'
It's still returning me with the same records. Seems like its only comparing the date and returning me the records.
Any idea where i m doing it wrong?
I am taking date as varchar in database.
Thanks!!
You appear to be using SQL Server (based on the dbo). Use a standard date format for date constants:
select *
from POS.dbo.voucher
where date_time between '2014-10-10 00:00:00' and '2014-10-11 12:00:00'
However, for this to work, you need to store dates using proper date formats in the database. So, you should fix the data structure to store dates using the correct data type.
I agree you would be better off changing the field from varchar to datetime. If that's not an option then you should convert the varchar field in the query into a datetime as appropriate in MS SQL Server, probably through a function, perhaps through a cast. BTW, best to tag with actually db used, and maybe SQL too.

MYSQL Date Range RFC 2822

THE SITUATION
I pull timestamps formatted in RFC 2822 (Sat, 01 Dec 2012 05:49:45 +0000) and store them in a VARCHAR field in MYSQL. I have a start_date and end_date.
THE GOAL
Search BETWEEN two dates (like start_date BETWEEN '2012-11-01' AND '2012-12-01')
THE CONDITIONS
I want to do this with pure SQL and not do post processing in PHP
THE ACCEPTABLE COMPROMISE
I don't want to, but I will convert and store them as DATETIME by using PHP if needed.
Can anyone help me accomplish my goal (listed above).
Rick
You could convert your string dates do datetime using str_to_date:
select str_to_date('Sat, 01 Dec 2012 05:49:45 +0000','%a, %d %b %Y %T')
If you need to convert also timezone, try this:
set #datestring='Sat, 01 Dec 2012 05:49:45 +0000';
select
CONVERT_TZ(
str_to_date(#datestring,'%a, %d %b %Y %T'),
concat(mid(#datestring, 27, 3), ':', mid(#datestring, 30, 2)),
'+00:00'
)
Store them as a native DATETIME. This is the only sane approach.
Why are you so opposed to using the proper tool for the job?
Storing timestamps as a string is poor use of the database features. Since they were all the same format, they could have easily been converted to a datetime on input.