The time is stored in the database as '09:21 am'
Now how can i get the range result
for instance
select * from tablename where stored_time between '09:21 am' and '10:05 pm';
I tried the above query but it's not giving the result properly. this is because the time is stored as varchar.
is there any function to convert this string to time and query it in mysql?
Using STR_TO_DATE() with the following format:
SELECT STR_TO_DATE(UPPER('09:21 am'), '%h:%i %p');
/* 09:21:00 */
%h = Hours 00-12
%i = Minutes
%p = AM/PM (UPPER() converts to uppercase so it matches your lowercase version)
So the full query:
SELECT *
FROM tablename
WHERE
/* Use 24h time for the comparison values and wrap stored_time in the STR_TO_DATE() */
STR_TO_DATE(UPPER(stored_time)) between '09:21:00' and '22:05:00';
It is recommended that you switch this column to a real TIME or DATETIME type rather than storing the value as a string.
you can make use of DATE_FORMAT(date,format). Documentation
select * from tablename where DATE_FORMAT(stored_time,'%h:%i %p') between
DATE_FORMAT('09:21 am','%h:%i %p') and DATE_FORMAT('10:05 pm','%h:%i %p');
Related
I have a column of dates written as strings with "am" and "pm" at the end, for example:
1/1/2016 12:00:00.000 AM
The type of the column is currently varchar, I want to change the type to datetime format.
Attempt 1
Changing the column type from phpmyadmin dashboard gives the following error:
Query error:
#1292 - Incorrect datetime value: '1/1/2016 12:00:00.000 AM'
Attempt 2
Considering the format of the date, I tried to use the STR_TO_DATE function. But it does not give the expected result. It instead returns the same date for each row.
SELECT STR_TO_DATE(`assembling-machine-tag`.`Time`, "%Y") from `assembling-machine-tag`
Result
So, how can I change the format of my column from varchar (with format = dd/mm/yy hh:mm:ss.ms AM) to datetime (with format = yyyy-mm-dd hh:mm:ss) in MySQL 7.4.1?
You just need to use the correct pattern:
SELECT STR_TO_DATE('1/2/2016 01:02:03.456 AM', '%e/%c/%Y %h:%i:%s.%f %p')
-- 2016-02-01 01:02:03.456000
In order to convert varchar data to datetime, you first need to create a temporary datetime column and update it like so:
UPDATE t SET `datetimetemp` = STR_TO_DATE(`varchardate`, '%c/%e/%Y %h:%i:%s.%f %p')
Once satisfied with result drop, the varchar column and rename the temporary column.
I would use this version:
SELECT STR_TO_DATE('1/1/2016 12:00:00.000 PM', '%d/%m/%Y %h:%i:%s.%f %p')
In mysql database,column name created.This "created " column is text datatype,I need to change this to datetime.Now this column have so many datas.Is it possible to convert it or?
Database look like
created
18-11-15 18:21:25
Expecting ouput is
created
2018-11-15 18:21:25
When am doing
ALTER TABLE invoices MODIFY created datetime
This query giving wrong data.its converting from 15-09-18 03:03:43 to 2015-09-18 03:03:43
If the original data is not in MySQL Datetime format (YYYY-MM-DD HH:MM:SS), you cannot just change the column datatype from Varchar/Text to Date/Datetime. Otherwise, there will be an irreparable Data loss.
This will be a multi-step process. You will first need to convert the date string to MySQL date format (YYYY-MM-DD HH:MM:SS). We can use STR_TO_DATE() function for this.
Your sample date string (18-11-15 18:21:25) is basically in %y-%m-%d %T format. Following format specifiers can be used:
%d Day of the month as a numeric value (01 to 31)
%m Month name as a numeric value (00 to 12)
%y Year as a numeric, 2-digit value
%T Time in 24 hour format (hh:mm:ss)
The query to update the date would look as follows:
UPDATE invoices
SET created = STR_TO_DATE(created, '%y-%m-%d %T');
Now, you can use Alter Table to change the data type from Text type to Datetime.
ALTER TABLE invoices
MODIFY COLUMN created datetime;
The best thing to do here is to not store your dates as text. Assuming you have already done this, we can cope by calling STR_TO_DATE to generate a bona fide date:
SELECT
STR_TO_DATE(created, '%y-%m-%d %h:%i:%s') AS created_out
FROM yourTable;
Since the output you expect is standard date output, we can stop here and avoid also calling DATE_FORMAT to generate a different output.
you want to convert output or database records ? for second you can use sql query :
UPDATE 'table_name' SET 'created' = CONCAT('20', 'created')
You will need first to interchange the day with the year in the created column, as follows:
UPDATE invoices
SET created = CONCAT(SUBSTR(created, 7, 2), '-', SUBSTR(created, 4, 2), '-', SUBSTR(created, 1, 2));
Then, you convert the column to DATETIME, as follows:
ALTER TABLE invoices MODIFY created DATETIME;
Hope this helps.
I have a MySQL DB to work with which has a LASTUPDATE column as VARCHAR.
This field has this format: YYYY-MM-DD HH:mm:ss
I need to set up a SELECT query to find all rows after e certain date, so i was trying to use the CONVERT function in this way
SELECT * from POWER WHERE CONVERT(DATETIME, LASTUPDATE) > 'CONVERT(DATETIME, '2016-12-29 17:24:22')'
but i'm getting the error
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LASTUPDATE) > 'CONVERT(DATETIME, '2016-12-29 17:32:52')'' at line 1
I have also tried to use:
SELECT * from POWER WHERE CONVERT(DATETIME, LASTUPDATE) > '2016-12-29 17:32:52' but i'm getting the same error
You could use the str_to_date
SELECT *
from POWER
WHERE str_to_date(LASTUPDATE, '%Y-%m-%d %H:%i:%s' ) >
str_to_date('2016-12-29 17:24:22', '%Y-%m-%d %H:%i:%s' )
Use str_to_date()
The query should use str_to_date() like this. See MySQL Manual for more details
SELECT
*
from POWER
WHERE
str_to_date(LASTUPDATE, '%Y-%m-%d %H:%i:%s') > '2016-12-29 17:24:22'
Date and time literial
Note that MySQL accepts date and time literals, so you do not need another str_to_date on the right-hand side of the > operator. See this for more details.
About the form between ... and ... on a string field storing date value
Some suggested using the form of between ... and ..., such as the one
shown below. Note that this method would work only if the string
values happen to use a format in which sorting the string alphabetically (in the given collation) happens to give the same sorting order as date (the format used in the original question -- '%Y-%m-%d %H:%i:%s' -- happens to satisfy this condition, but this is not guaranteed (see an example below) for many string representations of date/time values).
SELECT
*
from POWER
WHERE
LASTUPDATE between '2016-12-29 17:24:22' AND '2016-12-31 23:59:59';
And it would fail if the string values are using a different format, such as these:
create table power (
lastupdate varchar(40) not null default ''
) engine=innoDB;
insert into power
values
('Dec 30, 2016 07:24:22')
, ('Jan 28, 2016 07:24:22')
For the above, we must still use the str_to_date() function (this time with a format argument %b %d, %Y %H:%i:%s that is matching the format used with the string values)
SELECT
*
from POWER
WHERE
str_to_date(LASTUPDATE, '%b %d, %Y %H:%i:%s') > '2016-12-29 17:24:22';
I have an sqlfiddle.com page showing the above example.
convert is an MS-SQL Server function. In MySQL, you can use the str_to_date function with similar semantics:
SELECT *
FROM power
WHERE STR_TO_DATE(lastupdate, '%Y-%m-%d %H-%i-%s') >
STR_TO_DATE('2016-12-29 17:24:22', '%Y-%m-%d %H-%i-%s')
I have this query
SELECT * FROM tracklogs.sms_outbound
WHERE gsmno = 'rk4#*******.com.ph'
AND cdate > cast('2013/11/14 09:44:48 PM' as datetime)
where cdate format is in %Y-%m-%d %h:%i:%s %p.
I have tried converting the date into that format then cast it as datetime but still doesn't working.
Use STR_TO_DATE() to correctly convert the datetime literal you have provided to a proper DATETIME value. It seems that your cdate column is a char() or varchar() column. So you will also need to convert that to DATETIME to compare it.
What you need is this:
That works like this (http://sqlfiddle.com/#!2/d41d8/48741/0)
STR_TO_DATE(cdate, '%Y-%m-%d %h:%i:%s %p') >
STR_TO_DATE('2013/11/14 09:44:48 PM', '%Y/%m/%d %h:%i:%s %p')
Converting these strings to DATETIME data items ensures that the comparison handles both the date and the time correctly. See this fiddle (http://sqlfiddle.com/#!2/d41d8/48743/0)
But, you should consider changing your cdate item to a DATETIME, because then you'll be able to index it and speed up your search.
SELECT * FROM tracklogs.sms_outbound
WHERE gsmno = 'rk4#*******.com.ph'
AND UNIX_TIMESTAMP(str_to_date(cdate,'%Y-%m-%d %H:%i:%s')) > UNIX_TIMESTAMP('2013-11-14 09:44:48')
I've got a date format in a bigint field in this format "20130314123743" - YYYYMMDDHHMMSS and i need to do a mysql query on it and get it back to the user in something like yyyy-mm-dd hh:mm:ss.
Is there a native mysql function that will take that date format and return it to something human readable?
One approach:
select cast(cast(bigintdateval as char(14)) as datetime)
SQLFiddle here.
To convert to a datetime you can use MySQL function FROM_UNIXTIME
FROM_UNIXTIME (unix_timestamp, [format ])
SELECT FROM_UNIXTIME(20130314123743, '%Y-%m-%d %h:%i:%s');
Exemple;
SELECT FROM_UNIXTIME(birthDay/100000, "%Y-%m-%d %H:%i:%s") AS date
FROM person;
source
FROM_UNIXTIME
Hope that help