mysql query not returning results - mysql

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?)

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'

Unable to get Results of a query

I am newbie to SQL.
I have this query
I am not sure where i am doing it wrong.
Can you guys help me out?
thanks.
Three mistakes:
You need single quotes
MySql expects two-digit months
The 2015-09-30 end date includes an implicit midnight for the time component, which excludes most of the day
Put it all together you get this:
SELECT * FROM results WHERE played_on BETWEEN '2015-09-01' AND '2015-10-01';
While I'm here, I prefer to avoid BETWEEN in favor of explicit bounds. There's always that chance someone codes a game for exactly midnight October 1:
SELECT * FROM results WHERE played_on >= '2015-09-01' AND played_on < '2015-10-01';
And you would have had this answer faster if I could have copy/pasted the query text from your post instead of having to re-type. Posting images of sample data or code instead of the text is considered very rude here.
i think you can use MySql Date Function like MONTH() and YEAR() for this query :
SELECT * FROM results WHERE MONTH(played_on) = 9 AND YEAR(played_on) = 2015
I hope this answer can help you.

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

Hive's hour() function returns 12 hour clock value

According to the documentation Hives standard function hour() should return a value between 0 and 24 but for some reason I always get a twelve hour clock value, between 0 and 12. I'm using a MySQLDateTime field as a Timestamp field in my Hive table. Anyone know what the problem might be?
I think I found it. I looked at the source code and apparently UDFHour.java does have two evaluate() functions. One that does accept a Text object as parameter and one that uses a TimeStampWritable object as parameter. Both work with a Calendar instance but for some reason the first function returns the value of Calendar.HOUR_OF_DAY and the second one Calendar.HOUR.
I've looked in the Hives documentation but I couldn't find anything about that second function, but it's there. I'm using Hive 0.9.0.16, which came with Hortonworks' HDP.
Edit:
I've reported this a while back. A patch is now available: https://issues.apache.org/jira/browse/HIVE-3850.
Regardless of what hive has done, you could format the date to be returned as 24 hour format.
select FROM_UNIXTIME(mydate)
from mytable
;
Or you may update all datetimes stamps if it makes sense.
Reference
The function hour() returns a 24h formatted result if it works with string format. You may use
hour(cast (column_name as string))
for lower version of hive,I got a workaround
hour(from_unixtime(
unix_timestamp(
from_utc_timestamp(
from_unixtime(round(created_at/1000)),'Etc/GMT-8')
)))
Since I am using EMR, I can not choose to use the latest version of hive, so I got this workaround.
Just to show an example for what has already been told above
HOUR(cast (from_utc_timestamp(my_date_timestamp ,'GMT') as string)) -- returns 24 hr format
HOUR( from_utc_timestamp(my_date_timestamp ,'GMT') ) --returns 12 hr format

mysql calculation

This is my initial question....well I got a response but I'm still stuck.
Can anyone please explain how can I achieve this in mysql:
I have two fields in mysql, 'cap_commdate' with DATE TYPE and 'cap_policyterm' with INT TYPE. I want to have another field called 'cap_maturityDate' which will automatically compute the policy term pereod in years (in layman's term ie: cap_commdate*cap_policyterm). What is the right SQL query to use; or what is the best approach; and I want to use it in my recordset to prepare a confirmation page... please a simple explanation...
I have tried the following:
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) ...
I ran the query and got errors; so i edited it and used:
SELECT
DATE_ADD("cap_commdate", INTERVAL "cap_policyterm" YEAR) AS cap_maturity FROM capital
All I got was empty fields. Please help out.
It sounds like you want this:
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR)
...
This DATE_ADD function adds the given number of years to the starting date, thus producing the maturity date — just as you've described.
Please show the errors you get. The query
SELECT DATE_ADD(cap_commdate, INTERVAL cap_policyterm YEAR) FROM capital
should work.