This article contains common examples of formatting dates in Oracle Database.
how to
How to Return the Number of Seconds Past Midnight in Oracle Database
Oracle Database provides the SSSSS format element that enables us to get the number of seconds past midnight from a given datetime value.
How to Get the Month Name from a Date in Oracle
In Oracle Database, the TO_CHAR(datetime) allows us to return a datetime value, formatted in a way that we specify.
One of the things we can do is return the month name from a date.
4 Ways to Format a Number Without Decimals in Oracle
When using Oracle Database, you can use functions like TO_CHAR(number) to return numbers as a string. This enables you to format the number to have a specific number of decimal places – including no decimal places, if that’s what you want.
You can also use functions like ROUND(number) and TRUNC(number) to round or truncate the number to no decimal places.
There’s also the CAST() function that allows you to cast a data type as an integer, which effectively removes any decimal places.
2 Ways to Format a Number with Leading Zeros in Oracle
When using Oracle Database to format a number to have leading zeros, we need to convert it to a string and format it accordingly.
You can use the TO_CHAR(number) function to format numbers with leading zeros.
And perhaps a lesser known fact, is that you can also use the LPAD() function to pad a number with leading zeros.
How to Format a Number with a Comma in Oracle
When you use the TO_CHAR() function to format a number in Oracle, you use a format model to determine how the number should be formatted.
For example, you could format a number like 12,345.00 or like 12.345,00, depending on your locale.
The format model can include the G or D format elements to add a comma to a number. Which one you use depends on whether you want the comma as a thousands separator, or as a decimal character.
Alternatively, you can use an actual comma character (,) if you prefer, although this method is not locale aware like the G and D format elements are.
How to Format a Number as Currency in Oracle
Oracle Database provides the TO_CHAR(number) function that enables you to format numbers based on a given format model. There are three format elements that you can use with the TO_CHAR() function in order to return a value as a currency.
3 Ways to Format a Number to 2 Decimal Places in Oracle
When using Oracle Database, you can use functions like TO_CHAR(number) to return numbers as a string, formatted to two decimal places (or however many decimal places you require).
Or you can use functions like ROUND(number) and TRUNC(number) to round or truncate the number to your required number of decimal places.
2 Ways to Get the Minutes from a Datetime Value in Oracle Database
Below are two functions that can be used to return the minutes portion from a datetime value in Oracle Database.
How to Get the Short Month Name from a Date in MariaDB
In MariaDB, you can use the DATE_FORMAT() function to return various date parts from a date. One of the things you can return is the short month name. For example Nov or Dec (instead of November or December).