Add a Time Zone Offset to a datetime2 Value in SQL Server (T-SQL)

In SQL Server, the TODATETIMEOFFSET() function was specifically designed to return a datetimeoffset value from a datetime2 value.

Given the fact that the datetime2 data type doesn’t actually support time zone offsets, and datetimeoffset must contain the offset, the TODATETIMEOFFSET() function allows you to specify a time zone offset to use.

This article provides some examples to demonstrate.

Continue reading

4 Ways to Separate Hours, Minutes, and Seconds from a Time Value in MariaDB

MariaDB has several functions that enable you to extract various date and time parts from date/time values. You can use these to separate each date/time component into its own column if required.

Below are four ways to extract the hours, minutes, and seconds from a time value in MariaDB. As a bonus, we’ll also include microseconds.

Continue reading

MariaDB Composite Date & Time Units Explained

MariaDB includes a bunch of date and time units that you can use when working with date and time values. For example, MONTH is a unit, and HOUR is another unit.

Some units are composite units. Composite units are when two units get combined into one. The naming convention is that each unit name is separated by an underscore. For example, MINUTE_SECOND is for minutes and seconds.

Below are some examples that demonstrate how composite units work in MariaDB.

Continue reading

How to Add AM/PM to a Time or Datetime Value in MariaDB

MariaDB includes many functions that allow you to return time and datetime values in a variety of formats.

Two functions allow you to format the time portion using a 12 hour clock, with the AM/PM designator are DATE_FORMAT() and TIME_FORMAT().

There are many format specifiers that can be used with these functions, but only a couple that return the AM/PM designator.

Continue reading

How HOUR() Works in MariaDB

In MariaDB, HOUR() is a built-in date and time function that returns the hour from a given time expression.

It accepts one argument, which is the time you want to extract the hour from.

For time-of-day values, it returns the hour as a number in the range 0 to 23. However, the range of TIME values can be much larger, and therefore, the returned value can be much higher than 23.

The return value is always positive, even if a negative time is provided.

Continue reading