Cloud Spanner FORMAT_DATE Function and Example

  • Post author:
  • Post last modified:July 13, 2020
  • Post category:General
  • Reading time:6 mins read

You can use the FORMAT_DATE function to format the string containing a date expression. It is one of the highly used functions to format date. This function is equivalent to to_char and to_date function present in other relational databases. In this article, we will check cloud Spanner FORMAT_DATE function, syntax and some example

Cloud Spanner FORMAT_DATE Function

The format_date function is used to date expression to required format.

Cloud Spanner FORMAT_DATE function formats the date expression containing date according to the specified date format string.

Cloud Spanner FORMAT_DATE Function Syntax

Following is the FORMAT-DATE syntax.

FORMAT_DATE(format_string, date_expr)

Where, date_expr is date expression and format_string is the date format string.

Format Elements for DATE

The FORMAT_DATE functions that use format strings support the following elements

Format ElementDescription
%AThe full weekday name.
%aThe abbreviated weekday name.
%BThe full month name.
%b or %hThe abbreviated month name.
%CIt is the century as a decimal number (00-99). A year divided by 100 and truncated to an integer .
%DThe date in the format %m/%d/%y.
%dThe day of the month as a decimal number (01-31).
%eThe day of month as a decimal number (1-31); single digits are preceded by a space.
%FThe date in the format %Y-%m-%d.
%GThe ISO 8601 year with century as a decimal number.
%gThe ISO 8601 year without century as a decimal number (00-99).
%jThe day of the year as a decimal number (001-366).
%mThe month as a decimal number (01-12).
%nA newline character.
%tA tab character.
%UThe week number of the year as a decimal number (00-53). . Sunday as the first day of the week.
%uThe weekday (Monday as the first day of the week) as a decimal number (1-7).
%VThe ISO 8601 week number of the year (Monday as the first day of the week) as a decimal number (01-53).
%WThe week number of the year as a decimal number (00-53). Monday as the first day of the week
%wThe weekday as a decimal number (0-6). Sunday as the first day of the week.
%xThe date representation in MM/DD/YY format.
%YThe year with century as a decimal number.
%yThe year without century as a decimal number (00-99), with an optional leading zero.
%E4YFour-character years (0001 … 9999).

Cloud Spanner FORMAT_DATE Function Examples

Following are the some of the common example FORMAT_DATE functions

Cloud Spanner Convert Date to DD-MM-YYYY Format
select FORMAT_DATE('%d-%m-%Y', current_date) as date;
date
13-07-2020
Google Cloud Spanner Convert Date to DD/MM/YYYY Format
select FORMAT_DATE('%d/%m/%Y', current_date) as date;
date
13/07/2020
Cloud Spanner Convert Date to DD/MM/YY Format
select  FORMAT_DATE('%x', current_date) as date;
date
07/13/20
Google Cloud Spanner Convert Date to DD-MM-YY Format
select FORMAT_DATE('%d-%m-%y', current_date) as date;
date
13-07-20
Cloud Spanner Convert Date to MON-YYYY Format
SELECT FORMAT_DATE("%b-%Y", current_date) AS formatted;
formatted
Jul-2020
Google Cloud Spanner Convert Date to DD-MON-YYYY Format
SELECT FORMAT_DATE("%d-%b-%Y", current_date) AS formatted;
formatted
13-Jul-2020
Cloud Spanner Convert Date to YYYY-MM-DD Format
SELECT FORMAT_DATE("%F", current_date) AS formatted;
formatted
2020-07-13

Related Articles

Hope this helps 🙂