Trouble running three events in SELECT MySql - mysql

I am trying to run three events in a SELECT. Individually they seem to run fine but when I lump them together they do not work together resulting in error messages.
Trying to:
Convert TZ to local TZ
Change DATE FORMAT to Month Day Time
Select only records with an bor_id >166
Any help with the formatting for this would be GREAT. I can't seem to get it right.
select CONVERT_TZ(`borsignupdate`,'-08:00',##global.time_zone) DATE_FORMAT(MAX(borsignupdate),'%M %e %l:%i%p') AS maxdate
FROM borsurvey
WHERE bor_id>166

You need to put the CONVERT_TZ call in the argument to DATE_FORMAT.
SELECT DATE_FORMAT(CONVERT_TZ(MAX(borsignupdate), '-08:00', ##global.time_zone), '%M %e %l:%i%p') AS maxdate
FROM borsurvey
WHERE bor_id > 166
In your question in the comment, you're missing parentheses around the arguments to DATE_FORMAT, and you have some of the arguments to CONVERT_TZ outside its parentheses. This is basic syntax of 90% of programming languages, I'm not sure why you're having so much trouble with it.
SELECT *, DATE_FORMAT(CONVERT_TZ(borsignupdate, '-08:00', ##global.time_zone), '%M %e %l:%i%p') AS localtime
FROM borsurvey
WHERE bor_id > 166
It's basically the same as the previous query, except without the MAX() call around borsignupdate.

Related

MySQL timediff function does not give proper output

This function
SELECT HOUR(TIMEDIFF('2020-06-17 12:15:00am','2020-06-17 01:15:00am')) as 'diff'
gives me the difference as
11 hours
while actually it should be
1 hour
. How do I fix this? Please advice.
Thank you.
MySQL doesn't recognize am and pm by default, it parses times in 24-hour format. You need to use STR_TO_DATE() if you want to parse a custom datetime format.
Also, you need to put the later time first.
SELECT HOUR(TIMEDIFF(STR_TO_DATE('2020-06-17 01:15:00am', '%Y-%m-%d %r'),
STR_TO_DATE('2020-06-17 12:15:00am', '%Y-%m-%d %r'))) as 'diff'

What does the third parameter in CONVERT() do?

Query
Get the Joining year,Joining Month and Joining Date from employee table
This is my query which I have to perform. For this I write the following script:
select
SUBSTRING (convert(varchar,joining_date,103),7,4) ,
SUBSTRING (convert(varchar,joining_date,100),1,3) ,
SUBSTRING (convert(varchar,joining_date,100),5,2)
from
EMPLOYEE
The result is: http://d.pr/i/vObI
But when I changed convert(varchar,joining_date,100) to convert(varchar,joining_date,101)
Result is like this: http://d.pr/i/G5fZ
Can anyone please explain what this parameter means?
There are several different ways that you can format date using convert(varchar.... These are well documented on the MSDN site or different sites online.
Using convert(varchar..., date, 100) places the date in the format:
mon dd yyyy hh:mmAM (or PM)
May 10 2013 12:55PM
Using convert(varchar...date, 101) puts the date in the format:
mm/dd/yyyy
05/10/2013
See Demo
My suggestion would be whenever you implement these conversions, be sure to give a length on the varchar(10), etc.
Based on what it looks like you are returning, you can eliminate some of the convert/substring statements that you are using and implement some other functions to get the same result:
select year(joining_date) as [year] ,
convert(varchar(3),joining_date,100) as [month] ,
day(joining_date) as [day]
from EMPLOYEE
I think there is a better approach to split and get datetime parts using DATEPART:
select DATEPART(YEAR, [joining_date]),
DATEPART(MONTH, [joining_date]),
DATEPART(DAY, [joining_date]) from EMPLOYEE
or if you are interested for example in names use DATENAME:
select DATEPART(YEAR, [joining_date]),
DATENAME(MONTH, [joining_date]),
DATEPART(DAY, [joining_date]) from EMPLOYEE
However according to MSDN for DATENAME: "The return value depends on the language environment set by using SET LANGUAGE".
Regarding your initial question - these parameters are basically styles or I would call them Regional specific codes as described here, and you can just run the different queries against the DB and you will see the strings returned - and you will figure why you get unexpected results. For more infos refer to MSDN: CAST and CONVERT

MYSQL DATE_FORMAT Function not working

I am currently working on a gig guide and would like to format the date as: Day - Month - Date
I'm using the following mysql query -
$query_getEvents = "SELECT gig_guide.date, gig_guide.artist, gig_guide.venue,
gig_guide.tickets, DATE_FORMAT(gig_guide.date, '%M %e, %Y')
FROM gig_guide ORDER BY gig_guide.date ASC";
Unfortunately the DATE_FORMAT is not formatting and keeps returning the standard 0000-00-00 format instead of the way I want it.
Any ideas what may have gone wrong with this code? Or could it be a direct problem with the database itself and the date structure.
If the month is not setted in gig_guide.date, the mysql can't make representation 00.month as a string.
Try it with some date which have month setted.

How to format time to '2:34 hrs', '0:34 hrs' etc in MySQL

I am writing a query that is selecting two times and calculating the difference between these, where one is the start and the other is the finish to find out the duration. However both are set to TIME types in mysql. How can i format the time instead of showing HH:MM:SS to '[OPTIONAL H]H':MM hrs'
Do i use the extract function?
I presume i have to wrap the TIMEDIFF around something else?
my sql code is as follows:
SELECT operator_no, log_in_time, TIMEDIFF(log_in_time,log_out_time) AS Duration
many thanks
Yes, wrap it in TIME_FORMAT():
SELECT operator_no, log_in_time,
TIME_FORMAT(
TIMEDIFF(log_in_time, log_out_time),
'%H:%i hrs'
) AS Duration
would this not work too?
SELECT operator_no, log_in_time,
CONCAT(EXTRACT(HOUR_MINUTE FROM TIMEDIFF (log_in_time,end_time)) + 'hrs' )
AS Duration

mysql query not returning results

I am executing this query
SELECT *
FROM temp
WHERE DATE_FORMAT(startTime,'%m/%d/%Y') = '7/15/2012'
and startTime column has this value '2012-07-15 12:00:00'
But this is not returning any results. Can somebody please help?
Change here:
7/15/2012
to:
07/15/2012
According to the documentation for the DATE_FORMAT function, %m is "Month, numeric (00..12)". Note the zero-padding. So you need to write '07/15/2012' rather than '7/15/2012'.
(And in case you're wondering — I have no idea what month #0 is. So far as I'm aware, the months range from 01 to 12. Maybe some locales do have a month #0?)