check if datetime field is British Summer Time in MySQL - mysql

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.

Related

Subtract 5 hours from a datetime value that is type timestamp with timezone

I am querying a table that has a datetime column and the value is in the format time stamp with time zone. I've tried doing a select hour(timestamp,-5) as NTime and different variants of that, but the furthest I get is an error stating the timestamp is not a valid name/type. I'm officially going off the deep end on this....
Basically, I just need the new alias column values to be a time stamp that is 5 hours behind the timestamp that is in the original table. Thank you all in advance!!
MariaDB / MySQL doesn't have a "timestamp with timezone" data type.
DATETIMEs are simple wall time, and TIMESTAMPs are UNIX time_t timestamps (the number of seconds since 1970-01-01T00:00:00UTC).
You can convert DATETIME values from one time zone to another by with tz_convert().
SELECT tz_convert('2022-04-08 21:53', 'America/Chicago', 'UTC')
for example.
Or, just to do date arithmetic you can do stuff like this.
SELECT '2022-04-08 21:53' - INTERVAL 5 HOUR

Why I can't store a timestamp in mysql / phpmyadmin?

I have a table created in phpMyAdmin and it contains two fields: start_time and text_modified. It looks like this
so the start_time might be null.
When I'm filling the data in phpmyadmin I can choose the date and time that should be represented as this timestamp:
After doing so I expect to store a timestamp value in this field instead of date time. But when I do a query SELECT start_time from table I see there this:
So I assumed that it is just the php my admin that shows me automatically all dates as a date time value instead of timestamps. But now when I do a query: SELECT FROM_UNIXTIME(start_time) FROM table I'm getting those results:
and instead I want normal dates here. What is going wrong here?
In a timestamp you can insert datetime values, that are internally stored as integers (the seconds since 1970-01-01 as you probably know). When you select them, they are displayed as date and time.
So far so good.
When you have values like 0000-00-00 00:00:00 you probably inserted NULL values or invalid dates or dates out of range for the integer value. Using FROM_UNIXTIME() doesn't make sense here, since this function calculates a date and time value from an integer value. This integer value of the timestamp column is like I said only used internally. Therefore you get NULL values for valid dates and 1970-01-01 for invalid dates since those were presumably treated as 0 and 0 seconds since 1970-01-01 00:00:00 is, surprise, 1970-01-01 00:00:00.

What difference between the DATE, TIME, DATETIME, and TIMESTAMP Types

I need to store both time and date in the mysql. So I used of NOW() function for that. But I don't know what should I use for type column im phpmyadmin. It should be noted that NOW() returns both time and date like this:
2014-11-11 12:45:34
Here is a solution, I can use of a separator for separating date and time (2014-11-11 and 12:45:34) and then store them in the DATE type and TIME type individually. Or I can use of VARCHAR type for storing both of them in one column. But I think these ways are not standard. what is standard type for storing both date and time ?
Here is my query: (also I don't know why NOW() function does not works)
INSERT INTO table (timedate) VALUES (NOW())
DATE: It is used for values with a date part but no time part. MySQL retrieves and displays DATE values in YYYY-MM-DD format. The supported range is 1000-01-01 to 9999-12-31.
DATETIME: It is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in YYYY-MM-DD HH:MM:SS format. The supported range is 1000-01-01 00:00:00 to 9999-12-31 23:59:59.
TIMESTAMP: It is also used for values that contain both date and time parts, and includes the time zone. TIMESTAMP has a range of 1970-01-01 00:00:01 UTC to 2038-01-19 03:14:07 UTC.
TIME: Its values are in HH:MM:SS format (or HHH:MM:SS format for large hours values). TIME values may range from -838:59:59 to 838:59:59. The hours part may be so large because the TIME type can be used not only to represent a time of day (which must be less than 24 hours), but also elapsed time or a time interval between two events (which may be much greater than 24 hours, or even negative).
I have a slightly different perspective on the difference between a DATETIME and a TIMESTAMP. A DATETIME stores a literal value of a date and time with no reference to any particular timezone. So, I can set a DATETIME column to a value such as '2019-01-16 12:15:00' to indicate precisely when my last birthday occurred. Was this Eastern Standard Time? Pacific Standard Time? Who knows? Where the current session time zone of the server comes into play occurs when you set a DATETIME column to some value such as NOW(). The value stored will be the current date and time using the current session time zone in effect. But once a DATETIME column has been set, it will display the same regardless of what the current session time zone is.
A TIMESTAMP column on the other hand takes the '2019-01-16 12:15:00' value you are setting into it and interprets it in the current session time zone to compute an internal representation relative to 1/1/1970 00:00:00 UTC. When the column is displayed, it will be converted back for display based on whatever the current session time zone is. It's a useful fiction to think of a TIMESTAMP as taking the value you are setting and converting it from the current session time zone to UTC for storing and then converting it back to the current session time zone for displaying.
If my server is in San Francisco but I am running an event in New York that starts on 9/1/1029 at 20:00, I would use a TIMESTAMP column for holding the start time, set the session time zone to 'America/New York' and set the start time to '2009-09-01 20:00:00'. If I want to know whether the event has occurred or not, regardless of the current session time zone setting I can compare the start time with NOW(). Of course, for displaying in a meaningful way to a perspective customer, I would need to set the correct session time zone. If I did not need to do time comparisons, then I would probably be better off just using a DATETIME column, which will display correctly (with an implied EST time zone) regardless of what the current session time zone is.
TIMESTAMP LIMITATION
The TIMESTAMP type has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC and so it may not usable for your particular application. In that case you will have to use a DATETIME type. You will, of course, always have to be concerned that the current session time zone is set properly whenever you are using this type with date functions such as NOW().
Saty described the differences between them. For your practice, you can use datetime in order to keep the output of NOW().
For example:
CREATE TABLE Orders
(
OrderId int NOT NULL,
ProductName varchar(50) NOT NULL,
OrderDate datetime NOT NULL DEFAULT NOW(),
PRIMARY KEY (OrderId)
)
You can read more at w3schools.
In shorter explanation
DATE: The DATE stores a date value in the form YYYY-MM-DD (year-month-day). It does not store time.
TIME: The TIME stores a time value in the form HH:MM:SS (hours-minutes-seconds). It does not store the date.
DATETIME: The DATETIME stores a date and time value in the form YYYY-MM-DD HH:MM:SS. It stores both the date and time.
TIMESTAMP: The TIMESTAMP is similar to the DATETIME, but includes a timezone. (Example of values YYYY-MM-DD HH:MM:SS +HH:MM, YYYY-MM-DD HH:MM:SS -HH:MM. +HH:MM and -HH:MM indicate the time zone from UTC (Coordinated Universal Time).

Group By Localized Date

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.

MySQL only allowing Date to be stored: how to store Time as well?

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.