Query to Convert Varchar HH:MM to Integer Minutes in MySQL - mysql

I am having a table in MySQL Server where there is a column name StateStartTime and StateEndTime Datatype Varchar(100). It basically stores Time as HH:MM Format. As per the requirement now i it to gives me Minutes count i.e. 01:00 gives me 60, 01:30 -> 90. Please help me to write the query for MySQL, so that will convert Varchar (HH:MM) into Minutes(integer).
This Topic have the same problem but I need a solution for MySql
SELECT LTRIM(DATEDIFF(MINUTE, 0, StateStartTime))
FROM Time
Giving me error #1582 - Incorrect parameter count in the call to native function 'DATEDIFF'

You can try using the function TIME_TO_SEC() and divide the result by 60.
Example:
select TIME_TO_SEC(StateEndTime)/60 from table;

Related

MySql query to get minute winner

I have one table with always 1000 rows and a time column in the format hh:mm:ss. I need to count and get winner from minutes.
Something like:
|12:01:00|
|14:09:00|
|03:01:00|
|14:03:00|
|08:08:00|
So in this example minute 01 is the winner.
Appreciate any help.
Thanks
Get minute from the time using minute(date column) if it is MySQL or date_part(minute, date column) if it is in redshift DB or use mid() if it's a string.
Then do a group by with this extracted column and count it and find the winner.

Date and time conversion query in SQL

In my database table, there is a date column i.e. EXPECTED DATE which is in dd-mm-yyyy format, and the datatype of the column is text. Now, I want to convert the format to yyyy-mm-dd. But the date is not changing at all and also when I tried to get the timestamp for the expected date column . I am getting some errors. For date coming I have used this STR_TO_DATE. But the year is not coming like what I expect and the timestamp also.
For example:
select STR_TO_DATE ('30-11-2011', '%d,%m,%y') as date ;
returns a result as
2020-11-30
And for timestamp
select STR_TO_DATE ('2011,11,30 12,30,45', '%y,%m,%d, %H,%I,%S');
I am not getting errors.
Please help me get the correct answers for this problem.
For the first query you need to use the %Y. Remember that it is always better to use "Y" for the years when you are writing a query for year.
SELECT STR_TO_DATE("30,11,2011", "%d,%m,%Y");
For the second one also, you can use '%Y' in the place of '%y'. For minutes, use '%i' not '%I'. For hours and minutes, you can use whatever you like.
SELECT STR_TO_DATE("2011,11,30 12,30,45", "%Y,%m,%d %h,%i,%s");
Refer to the below documentation for more clarification on SQL commands.
You need %Y (capital Y) for the 4 digit year, when using MySQL's STR_TO_DATE. Also, minutes is represented by %i, not %I, the latter which is hours on a 0 to 12 scale. So use:
SELECT STR_TO_DATE('30-11-2011', '%d-%m-%Y');
SELECT STR_TO_DATE('2011,11,30 12,30,45', '%Y,%m,%d %H,%i,%S');
For the first query you need to use the %Y'.
SELECT STR_TO_DATE("30,11,2011", "%d,%m,%Y");
For minutes, use this one only '%i'.
SELECT STR_TO_DATE("2011,11,30 12,30,45", "%Y,%m,%d %h,%i,%s");

Difference between two dates in MySQL with DATE() function

I am using DATE() function to calculate the difference between two dates in MySQL
value of SYSDATE() function is following
select SYSDATE();
2020-07-15 12:16:07.0
When I am using date from same month, it is giving correct result
select DATE(SYSDATE())- DATE('2020-07-13');
2
But when I am using date from last month it is giving difference as 86 instead of 16;
select DATE(SYSDATE())- DATE('2020-06-29');
86
Edit:
I am aware that we can use DATEDIFF() but I want to verify why DATE() function is giving results like this since we are already using this in code
MySQL doesn't support subtracting one date from another. The code
SELECT DATE '2020-07-15' - DATE '2020-06-29';
should hence result in an error, but MySQL silently converts this to this instead:
SELECT 20200715 - 20200629;
Seeing that you want to subtract two values, it assumes that you want to work with numbers. Dates are not numbers, but their internal representation yyyymmdd can be represented numerically. So, while CAST(DATE '2020-07-15 ' AS int) fails with a syntax error, as it should, MySQL is not consistent, when it comes to subtraction. It generates the numbers 20200715 and 20200629 and works with these.
I consider this a bug. MySQL should either raise an exception or return an INTERVAL when subtracting one DATE from another.

Convert Bigint data to human readable date-time in Sql

I have created a MySql table and feed data therein with below code
CREATE TABLE IF NOT EXISTS DB (
INN VARCHAR(200) NOT NULL,
Time BIGINT not NULL
);
Now I want to get a table which will report the Maximum and Minimum values of Time when grouped by INN. Below is my code -
SELECT INN, from_unixtime(MIN(Time)), from_unixtime(MAX(Time)) FROM DB GROUP BY INN
I want to get the Time values reported as normal date-time instead of Epoch time. But with above code, I am getting <NA> values.
A typical Time value is like 1546380001264082944
Can someone please help me to get the correct code to achieve the same.
The problem here is to do with the precision of the unix timestamp you are using.
Consider this:
SELECT FROM_UNIXTIME(1546380001), FROM_UNIXTIME(1546380001264082944)
The output is:
2019-01-01T22:00:01Z (null)
The timestampt value you have, 1546380001264082944, contains a level of precision beyond that accepted by MySQL.
The definition of FROM_UNIXTIME is:
FROM_UNIXTIME(unix_timestamp[,format])
The doc states:
unix_timestamp is an internal timestamp value representing seconds
since '1970-01-01 00:00:00' UTC
The precision of your timestamp is significantly greater than seconds since the Unix Epoch.
The docs are available here.
The value 1546380001264082944 is too big to be epoch seconds or even milliseconds. This is easily verified by putting this value on https://currentmillis.com/.
You have stored precision upto a nanosecond. So, divide the column value by 1e9 before passing them to from_unixtime.
SELECT INN, from_unixtime(MIN(Time) / 1000000000), from_unixtime(MAX(Time) / 1000000000)
FROM DB
GROUP BY INN

Calculating minutes from two different format time

My DB values save in this format
"12:07 AM"
dataType varchar in Sql Server
i want minutes b/w 2 times 2nd time is
current time
23:50 AM
23:24:43.6100000
differnce 26 minutes
but output come in negative values by executing below query
here m trying to do but output come in negative values
select datediff(minute,cast(getdate() as time),OrderDeliverTime)
from tblOrder
If the OrderDeliverTime occurs in the past, then it should be the middle parameter in DateDiff:
select datediff(minute,OrderDeliverTime,cast(getdate() as time)) from tblOrder