Tracking the total sales by day based on a table of transactions is quite easy to write. The current code uses a BETWEEN and executes a query for each date. I don't really like this, especially when date ranges are in months.
Now, the date_created field for the transaction is of the type timestamp. And writing a query like works, except for one thing:
SELECT DATE(date_created), sum(sale_total)
FROM
transaction
WHERE DATE(date_created) BETWEEN ? and ?
GROUP BY DATE(date_created)
It works beautifully, except that the database is localized to GMT, and I'm here in CST. So any transactions that occur after 7:00 PM CST will be "pushed" to the next day because it is stored as the next day in GMT.
I guess my question is 2-fold
How would I proceed to GROUP BY the localized date?
Is there a way strictly in MySQL know that I want to use a different timezone in the query? Or will this have to be a manual adjustment?
TIMESTAMP fields are stored as UTC under the hood. If your data is in TIMESTAMP fields, you can set the MySQL time zone to use: SET time_zone = 'America/New_York';
If you've stored the data in DATETIME fields, they get stored in the MySQL system time zone, so conversion would be on you.
Related
I would like some confirmation on how NOW function works in MySQL
According to the docs and when runing the this query SELECT NOW() the mysql returns the current time (local time i guess?) in following format YYYY-MM-DD HH-MM-SS.
If this is the case, then how come this works when comparing NOW() to a column that includes a UTC ISO date and time?
For example this works fine:
SELECT * FROM table where deadline > NOW() # deadline column contains a utc ISO string
Is the query above reliable or did it just return the correct answer by luck?
in case this is NOT reliable, how would you do the comparison?
MySQL NOW()-function returns a datetime in the session timezone. MySQL has UTC_TIMESTAMP()-function which returns current UTC date and time, which will work better when you are compare it to an UTC date time.
Note that you should store datetimes as DATETIME, instead of char/varchar (assume this is what you meant by "UTC ISO date and time").
Is it possible in a select command to check if a datetime field is within the range of British Summer Time and, if so, add an hour onto the time?
If this isnt possible, can I do it in a stored procedure instead. So i would iterate over a temporary table where one of the columns is a date and if the date was BST then I could add an hour to the value of the date in the temporary table
thanks a lot
It isn't possible. DATETIME columns store just date and time, accurate to the second.
To include time zones, you need a TIMESTAMP column. TIMESTAMP values are stored as UTC so there's no conversion needed to handle BST or other time changes. There's more information here.
Is it posaible to write a query when you don't know any acutal dates? I need to compare transaction dates with funds available dates & update the funds available date by adding a day to the date.
I know MS SQL better than MySQL, but you still should be able to compare them using greater than and less than. If you want to modify the value, you'll want to use the DATE_ADD function (Date and Time functions reference). Comparing transaction date to the funds available date would involve something like WHERE TransTable.TransDate >= CustTable.FundsAvailDate, and then adding one day to the funds available date would be something like SET FundsAvailDate = DATE_ADD(FundsAvailDate, INTERVAL 1 DAY)
I would like to store in my table a full date: year, month, day, hour, minute.
Using the date type is limiting me to year month and day only.
What should I do? I have to mention that I will select records from the db order by the full date so storing the hour and minute seperatly as strings might be a problem right?
You may use DATETIME instead of DATE.
Took me a while to figure this out but to change it in MySQL you have to go to the column that you are looking to change in structure. Change the Type column from DATE to DATETIME.
I need to pull a variable amount of days of data from a MySQL database that stores the date as a UNIX timestamp in an int column.
For example, if I need the last 5 days of data, what would my query be?
(All queries would start from current date and would go back x amount of days).
Timestamp is considered one of the Date and Time types and therefore any of the Date Time Functions can be used on it.
SELECT * FROM your_table
WHERE Ftimestamp_column > UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 5 DAY));
I've never tried it but there's a MySQL function to convert unix timestamps into MySQL dates and then you can use DATE_SUB() or whatever. http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime