Created time greater than or equal to - mysql

I am trying to write a query which will show all records greater than or equal to a 2013-08-01.
I have tried the following:
SELECT *
FROM v_itdept_allcases
WHERE `Created Time` >= '2013-08-01';
The query returns dates between 30-06-2006 and 23-09-2013.
Not quite sure what else to try as I have tried several variations with no luck.
Gary

As has been mentioned, you're probably hitting a format or string comparison issue. I would suggest converting to an easily comparable monotonic format, such as UNIX_TIMESTAMP(date), when then can be arithmetically modified. You are correct that you shouldn't need to do that, but it's a reasonably bulletproof solution.

Related

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.

Working in SQL, trying to create a query using date ranges, but some values are 0000-00-00 and need to replace them

I'm trying to create a query in which the UserRecordID's displayed are the ones in which
(1) the EventCreatedDate is greater than UpgradeCreatedDate
and
(2) the EventCreatedDate is less than the UpgradeExpirationDate
So something like:
WHERE Event.CreatedDate > UpgradeCreatedDate
AND Event.CreatedDate < UpgradeExpirationDate
However, the problem is that some UserRecordID's do not have an expiration date and are represented by "0000-00-00" and SQL seems to see this as a really small date, when I want the query to see it as a really large date (essentially infinity, since the upgrade never expires). So whenever the UpgradeExpirationDate is 0000-00-00 my current query always sees the Event.CreatedDate as greater than the UpgradeExpirationDate when I want it to be less than. Is there a way to account for this? I tried using a CASE statement, but still couldn't get it to work the way I wanted.
one thing. you can restate all the 0000-00-00 to 9999-12-31 in the database.. then everything you do will work just fine. I have typically used 9999-12-31 to represent "forever" vs 0000-00-00. Then your SQL will work as expected in all cases.
if you dont do that .. you will have to tac onto the where clause
WHERE
Event.CreatedDate > UpgradeCreatedDate
AND (Event.CreatedDate < UpgradeExpirationDate OR
UpgradeExpirationDate = '0000-00-00')

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 Convert 48 Hours into 48:00:00

I have a regular DATETIME row, eg: 2012-06-19 13:56:56
I am running the following to see how much the time difference is:
DATEDIFF(end_time, NOW()) * 24
It returns 48
Edit: How do I get the Minutes/Seconds? I have tried UNIXTIME(field) - UNIXTIME(NOW()) but i cant get beyond it.
Im trying to convert this into 48:00:00 (Or however that timestamp works)
I keep looking up time functions but they have to do with EXTRACT, and Im not sure thats the way to go about it.
Since datediff only ever returns a difference in days, you might as well just use a string operation to do your formating:
SELECT CONCAT(DATEDIFF(end_time, NOW()) * 24), ':00:00')

Trying to use BETWEEN condition to return rows based on date but getting no results

I have a database containing a list of events, it is formatted something like this:
Datef Event Location Discipline
10/01/2012 MG Training Brooklands MG
I am running this query in order to get the results between certain dates:
SELECT * FROM events WHERE Discipline IN ('MG') AND Datef BETWEEN 01/01/2012 AND 31/01/2012
The query runs correctly and I know that there are relevant results in the database, but I receive no results when running the query in phpmyadmin (I just get told "Your SQL query has been executed successfully").
I was wondering if anyone had any idea why this wouldn't be returning results?
Update:
Putting dates in quotes (e.g. SELECT * FROM events WHERE Discipline IN ('MG') AND Datef BETWEEN 01/01/2012 AND 31/01/2012) kinda works but there's a bug.
I've certain dates doesn't work. e.g. SELECT * FROM events WHERE Discipline IN ('COMM') AND Datef BETWEEN '2012-02-01' AND '2012-02-29' shows no results, even though there is an event on 2010-02-01. Is this a peculiarity in the BETWEEN algorithm, or am I still getting my query wrong?
Without quotes or anything designating those values as dates, it will simply do the math on the integers you wrote.
In other words, you ask for
BETWEEN 01/01/2012 AND 31/01/2012
So you get 1/1=1, then 1/2012 which is almost 0.
Then you do 31/1=31, then 31/2012 which is also almost 0.
So it becomes
BETWEEN 0 and 0
Try wrapping your code strtotime(). MySQL and PHP use different formatting.
For some additional info, here are a couple of links:
http://www.richardlord.net/blog/dates-in-php-and-mysql
http://www.binarytides.com/blog/parse-and-format-dates-between-php-and-mysql/
Your column Datef is of the wrong type and should be datatime
SELECT
*
FROM
`events`
WHERE
`Discipline` IN ('MG')
AND
`Datef` BETWEEN '2012-01-01' AND '2012-01-31'