SQL - char field with date - how to search between two dates - mysql

I have simple problem with SQL query.. I would like to search fields, where dates begin and ends in specific date and time.
Field is defined as CHAR and have structure: DD.MM.YYYY hh:mm:ss and I cannot change it.
I tried to declare some variables and search by this, also tried by Converting.
This is what I tried and didn't work:
SELECT date FROM table WHERE date BETWEEN '1.01.2017 00:00:00' AND '1.02.2017 23:59:59'
SELECT date FROM table WHERE date >= '1.01.2017 00:00:00' AND date <= '1.02.2017 00:00:00'
SELECT date FROM table WHERE date >= Convert(DATETIME, '1.01.2017', 104) AND date <= Convert(DATETIME, '1.02.2017', 104)
Always after this query I get all dates, not this what I asked.

Your field is not in a format that can be compared lexicographically as strings, so you need to convert it to a date. But it's not in the format that MySQL can parse by default (it expects dates in the format YYYY-MM-DD). So use STR_TO_DATE() to parse it with a format string.
SELECT date
FROM table
WHERE STR_TO_DATE(date, '%d.%m.%Y %H:%i:%s') BETWEEN '2017-01-01 00:00' AND '2017-01-02 23:59:59'

I solved my problem. Problem was with that HOW this fields are storage in DB.
I thought that they are storage like: DD.MM.YYYY hh:mm:ss, but it was only structure. In DB they are storage like: YYYYMMDDhhmmss and after changes in WHERE query line it works.
SELECT date FROM table WHERE date >= '20170101000000' AND date <= '20170101235959'

SELECT date FROM table WHERE STR_TO_DATE(date, '%d.%m.%Y %H:%i:%s') BETWEEN '2017-01-01 00:00:00' AND '2017-02-01 23:59:59'
OR
You can use STR_TO_DATE() function.
SELECT date FROM table WHERE STR_TO_DATE(date, '%d.%m.%Y %H:%i:%s') BETWEEN STR_TO_DATE('1.01.2017 00:00:00','%d.%m.%Y %H:%i:%s') AND STR_TO_DATE('1.02.2017 23:59:59','%d.%m.%Y %H:%i:%s')
You can try above query.

Related

comparing two dates after converting from unix format in mysql

I have a date that I am sending from controller to query in this format
start date 02/13/2019 end date 03/13/2019
First it was normal timestamp which I used and these query condition seemed to work
DATE_FORMAT(o.Timestamp, '%m/%d/%Y') >= '02/13/2019'")
DATE_FORMAT(o.Timestamp, '%m/%d/%Y') <= '03/13/2019'")
Now I store timestamp in unix format which is 1550077130
After that my query conditions become and don't work.
FROM_UNIXTIME(o.Timestamp, '%m/%d/%Y') >= '02/13/2019'")
FROM_UNIXTIME(o.Timestamp, '%m/%d/%Y') <= '03/13/2019'")
try converting string to date and compare date
select from my_table o
where FROM_UNIXTIME(o.Timestamp) >= str_to_date('02/13/2019', '%d/%m%Y')
AND FROM_UNIXTIME(o.Timestamp) <= str_to_date('03/13/2019', '%d/%m%Y')

mysql and date comparison with date function and without

I notice a strange behavior for me when using date() and without:
when I use
SELECT * FROM `mytable` WHERE date(date_add) >= '2017-08-01' and date(date_add) <= '2017-08-31'
I get all dates records within the given date range but if do:
SELECT * FROM `mytable` WHERE date_add >= '2017-08-01' and date_add <= '2017-08-31'
I don't get the rows from the last day 31, why? (the date_add field is datetime type)
EDIT:
How should I code date range correctly? Because what I understand so far is that if I don't use full time like YYYY-MM-DD HH-MM-SS I should always compare with DATE() to avoid missing results from the last day.
That's because date_add is a DATETIME field. If the time for the date 2017-08-31 is something like 08:15:00 or 13:21:00 in your table, your date is "bigger" than just 2017-08-31 00:00:00. Your comparison would just return data for the 2017-08-31 having the time 00:00:00.
Because without the date() transformation your comparison imply the values '2017-08-01 00:00:00' and '2017-08-01 00:00:00'

STR_TO_DATE from CONCAT

I have one date-field ref_event_times.end_date and one time-field ref_event_times.end_time in my table "ref_event_times"...
I try to union that as a one datetime field "end_date_time"...
Use follow
STR_TO_DATE('CONCAT(`ref_event_times`.`end_date`,' ',`ref_event_times`.`end_time`)','%m/%d/%Y %H:%i') AS `end_date_time`
return null...
Where is the mistake?
MYSQL doesn't support your given datetime format '%m/%d/%Y %H:%i'
The mysql date format should be like this '%Y-%m-%d %H:%i:%s' Datetime format in mysql
Data Type “Zero” Value
DATE '0000-00-00'
TIME '00:00:00'
DATETIME '0000-00-00 00:00:00'
TIMESTAMP '0000-00-00 00:00:00'
YEAR 0000
try this example
select STR_TO_DATE(CONCAT(current_date,' ',current_time),'%Y-%m-%d %H:%i:%s');

Get time if today else get date MYSQL timestamp

In a table I have timestamp column. The time in the timestamp looks as shown below.
2015-08-14 18:07:36
To select in format of HH:MM AM/PM I used as shown below
select TIME_FORMAT(timestamp_column, '%h:%i %p') from table_name;
What I need?
If the record inserted today(on present day) it have to retrieve timestamp in format of HH:MM AM/PM. If record is inserted on previous days then retrieve the timestamp as DD/MM/YY.
How can I write such a sql query. Is it possible?
select case when date(timestamp_column) = curdate()
then TIME_FORMAT(timestamp_column, '%h:%i %p')
else DATE_FORMAT(timestamp_column, '%d/%m/%y')
end
from table_name;

subtract 2 datetime in mysql ( one in 24 hours format and one in am/pm format )

I'm trying to create a query using mysql.
select ID,NCOde,ifnull(EndTime,now())-starttime from xxx where starttime between
'2012-05-09 00:00:00' and '2012-05-09 23:59:59'
the problem is ifnull(EndTime,now()) return datetime in 24 hours format, while the starttime using am/pm format.
I've tried using DATE_FORMAT(starttime, '%m-%d-%Y %T'), but it seems that the operation changed the datetime type to other type.
Any advice?
Use STR_TO_DATE() to convert your starttime string to a MySQL DATETIME:
STR_TO_DATE(starttime, '%m-%d-%Y %r')
and then use TIMEDIFF() to subtract two times:
select ID,NCOde,
TIMEDIFF(ifnull(EndTime,now()), STR_TO_DATE(starttime, '%m-%d-%Y %r'))
from xxx
where STR_TO_DATE(starttime,'%m-%d-%Y %r')
between '2012-05-09 00:00:00' and '2012-05-09 23:59:59'
You should probably consider changing the data type of the starttime column to DATETIME or TIMESTAMP. Note also that this assumes EndTime is already of such a data type, or else you will also have to perform a similar conversion with it too.
Use the DATE_SUB() function.
Plus what eggyal said.