Format elements

GoogleSQL for BigQuery supports the following format elements.

Format elements for date and time parts

Many GoogleSQL parsing and formatting functions rely on a format stringto describe the format of parsed or formatted values. A format string representsthe textual form of date and time and contains separate format elements that areapplied left-to-right.

These functions use format strings:

Format strings generally support the following elements:

Format elementTypeDescriptionExample
%ADATE
DATETIME
TIMESTAMP
The full weekday name (English).Wednesday
%aDATE
DATETIME
TIMESTAMP
The abbreviated weekday name (English).Wed
%BDATE
DATETIME
TIMESTAMP
The full month name (English).January
%bDATE
DATETIME
TIMESTAMP
The abbreviated month name (English).Jan
%CDATE
DATETIME
TIMESTAMP
The century (a year divided by 100 and truncated to an integer) as a decimal number (00-99).20
%cDATETIME
TIMESTAMP
The date and time representation (English).Wed Jan 20 21:47:00 2021
%DDATE
DATETIME
TIMESTAMP
The date in the format %m/%d/%y.01/20/21
%dDATE
DATETIME
TIMESTAMP
The day of the month as a decimal number (01-31).20
%eDATE
DATETIME
TIMESTAMP
The day of month as a decimal number (1-31); single digits are preceded by a space.20
%FDATE
DATETIME
TIMESTAMP
The date in the format %Y-%m-%d.2021-01-20
%GDATE
DATETIME
TIMESTAMP
TheISO 8601 year with century as a decimal number. Each ISO year begins on the Monday before the first Thursday of the Gregorian calendar year. Note that %G and %Y may produce different results near Gregorian year boundaries, where the Gregorian year and ISO year can diverge.2021
%gDATE
DATETIME
TIMESTAMP
TheISO 8601 year without century as a decimal number (00-99). Each ISO year begins on the Monday before the first Thursday of the Gregorian calendar year. Note that %g and %y may produce different results near Gregorian year boundaries, where the Gregorian year and ISO year can diverge.21
%HTIME
DATETIME
TIMESTAMP
The hour (24-hour clock) as a decimal number (00-23).21
%hDATE
DATETIME
TIMESTAMP
The abbreviated month name (English).Jan
%ITIME
DATETIME
TIMESTAMP
The hour (12-hour clock) as a decimal number (01-12).09
%JDATE
DATETIME
TIMESTAMP
TheISO 8601 1-based day of the year (001-364 or 001-371 days). If the ISO year isn't set, this format element is ignored.364
%jDATE
DATETIME
TIMESTAMP
The day of the year as a decimal number (001-366).020
%kTIME
DATETIME
TIMESTAMP
The hour (24-hour clock) as a decimal number (0-23); single digits are preceded by a space.21
%lTIME
DATETIME
TIMESTAMP
The hour (12-hour clock) as a decimal number (1-12); single digits are preceded by a space. 9
%MTIME
DATETIME
TIMESTAMP
The minute as a decimal number (00-59).47
%mDATE
DATETIME
TIMESTAMP
The month as a decimal number (01-12).01
%nAllA newline character.
%PTIME
DATETIME
TIMESTAMP
When formatting, this is either am or pm.
This can't be used with parsing. Instead, use %p.
pm
%pTIME
DATETIME
TIMESTAMP
When formatting, this is either AM or PM.
When parsing, this can be used with am, pm, AM, or PM.
PM
%QDATE
DATETIME
TIMESTAMP
The quarter as a decimal number (1-4).1
%RTIME
DATETIME
TIMESTAMP
The time in the format %H:%M.21:47
%STIME
DATETIME
TIMESTAMP
The second as a decimal number (00-60).00
%sTIME
DATETIME
TIMESTAMP
The number of seconds since 1970-01-01 00:00:00. Always overrides all other format elements, independent of where %s appears in the string. If multiple %s elements appear, then the last one takes precedence.1611179220
%TTIME
DATETIME
TIMESTAMP
The time in the format %H:%M:%S.21:47:00
%tAllA tab character.
%UDATE
DATETIME
TIMESTAMP
The week number of the year (Sunday as the first day of the week) as a decimal number (00-53).03
%uDATE
DATETIME
TIMESTAMP
The weekday (Monday as the first day of the week) as a decimal number (1-7).3
%VDATE
DATETIME
TIMESTAMP
TheISO 8601 week number of the year (Monday as the first day of the week) as a decimal number (01-53). If the week containing January 1 has four or more days in the new year, then it's week 1; otherwise it's week 53 of the previous year, and the next week is week 1.03
%WDATE
DATETIME
TIMESTAMP
The week number of the year (Monday as the first day of the week) as a decimal number (00-53).03
%wDATE
DATETIME
TIMESTAMP
The weekday (Sunday as the first day of the week) as a decimal number (0-6).3
%XTIME
DATETIME
TIMESTAMP
The time representation in HH:MM:SS format.21:47:00
%xDATE
DATETIME
TIMESTAMP
The date representation in MM/DD/YY format.01/20/21
%YDATE
DATETIME
TIMESTAMP
The year with century as a decimal number.2021
%yDATE
DATETIME
TIMESTAMP
The year without century as a decimal number (00-99), with an optional leading zero. Can be mixed with %C. If %C isn't specified, years 00-68 are 2000s, while years 69-99 are 1900s.21
%ZTIMESTAMP
The time zone name.UTC-5
%zTIMESTAMP
The offset from the Prime Meridian in the format +HHMM or -HHMM as appropriate, with positive values representing locations east of Greenwich.-0500
%%AllA single % character.%
%EzTIMESTAMP
RFC 3339-compatible numeric time zone (+HH:MM or -HH:MM).-05:00
%E<number>STIME
DATETIME
TIMESTAMP
Seconds with <number> digits of fractional precision.00.000 for %E3S
%E*STIME
DATETIME
TIMESTAMP
Seconds with full fractional precision (a literal '*').00.123456
%E4YDATE
DATETIME
TIMESTAMP
Four-character years (0001 ... 9999). Note that %Y produces as many characters as it takes to fully render the year.2021

Examples:

SELECTFORMAT_DATE("%b-%d-%Y",DATE"2008-12-25")ASformatted;/*-------------* | formatted   | +-------------+ | Dec-25-2008 | *-------------*/
SELECTFORMAT_DATETIME("%c",DATETIME"2008-12-25 15:30:00")ASformatted;/*--------------------------* | formatted                | +--------------------------+ | Thu Dec 25 15:30:00 2008 | *--------------------------*/
SELECTFORMAT_TIME("%R",TIME"15:30:00")asformatted_time;/*----------------* | formatted_time | +----------------+ | 15:30          | *----------------*/
SELECTFORMAT_TIMESTAMP("%b %Y %Ez",TIMESTAMP"2008-12-25 15:30:00+00")ASformatted;/*-----------------* | formatted       | +-----------------+ | Dec 2008 +00:00 | *-----------------*/
SELECTPARSE_DATE("%Y%m%d","20081225")ASparsed;/*------------* | parsed     | +------------+ | 2008-12-25 | *------------*/
SELECTPARSE_DATETIME('%Y-%m-%d %H:%M:%S','1998-10-18 13:45:55')ASdatetime;/*---------------------* | datetime            | +---------------------+ | 1998-10-18T13:45:55 | *---------------------*/
SELECTPARSE_TIME('%I:%M:%S %p','2:23:38 pm')ASparsed_time/*-------------* | parsed_time | +-------------+ | 14:23:38    | *-------------*/
SELECTPARSE_TIMESTAMP("%c","Thu Dec 25 07:30:00 2008")ASparsed;-- Display of results may differ, depending upon the environment and-- time zone where this query was executed./*-------------------------* | parsed                  | +-------------------------+ | 2008-12-25 07:30:00 UTC | *-------------------------*/

Format clause for CAST

format_clause:FORMATformat_modelformat_model:format_string_expression

The format clause can be used in someCAST functions. Youuse a format clause to provide instructions for how to conduct acast. For example, you couldinstruct a cast to convert a sequence of bytes to a base64-encoded stringinstead of a UTF-8-encoded string.

The format clause includes a format model. The format model can containformat elements combined together as a format string.

Format bytes as string

CAST(bytes_expressionASSTRINGFORMATformat_string_expression)

You can cast a sequence of bytes to a string with a format element in theformat string. If the bytes can't be formatted with aformat element, an error is returned. If the sequence of bytes isNULL, theresult isNULL. Format elements are case-insensitive.

Format elementReturnsExample
HEX Converts a sequence of bytes into a hexadecimal string. Input: b'\x00\x01\xEF\xFF'
Output: 0001efff
BASEX Converts a sequence of bytes into aBASEX encoded string. X represents one of these numbers: 2, 8, 16, 32, 64. Input as BASE8: b'\x02\x11\x3B'
Output: 00410473
BASE64M Converts a sequence of bytes into abase64-encoded string based onrfc 2045 for MIME. Generates a newline character ("\n") every 76 characters. Input: b'\xde\xad\xbe\xef'
Output: 3q2+7w==
ASCII Converts a sequence of bytes that are ASCII values to a string. If the input contains bytes that aren't a valid ASCII encoding, an error is returned. Input: b'\x48\x65\x6c\x6c\x6f'
Output: Hello
UTF-8 Converts a sequence of bytes that are UTF-8 values to a string. If the input contains bytes that aren't a valid UTF-8 encoding, an error is returned. Input: b'\x24'
Output: $
UTF8 Same behavior as UTF-8.

Return type

STRING

Example

SELECTCAST(b'\x48\x65\x6c\x6c\x6f'ASSTRINGFORMAT'ASCII')ASbytes_to_string;/*-----------------* | bytes_to_string | +-----------------+ | Hello           | *-----------------*/

Format string as bytes

CAST(string_expressionASBYTESFORMATformat_string_expression)

You can cast a string to bytes with a format element in theformat string. If the string can't be formatted with theformat element, an error is returned. Format elements are case-insensitive.

In the string expression, whitespace characters, such as\n, are ignoredif theBASE64 orBASE64M format element is used.

Note: The bytes output value is displayed as a base64-encoded string. Forexample,b'\x00\x01\xEF\xFF' is displayed as0001efff when you use theHEX format element.
Format elementReturnsExample
HEX Converts a hexadecimal-encoded string to bytes. If the input contains characters that aren't part of the HEX encoding alphabet (0~9, case-insensitive a~f), an error is returned. Input: '0001efff'
Output: b'\x00\x01\xEF\xFF'
BASEX Converts aBASEX-encoded string to bytes. X represents one of these numbers: 2, 8, 16, 32, 64. An error is returned if the input contains characters that aren't part of the BASEX encoding alphabet, except whitespace characters if the format element isBASE64. Input as BASE8: '00410473'
Output: b'\x02\x11\x3B'
BASE64M Converts abase64-encoded string to bytes. If the input contains characters that aren't whitespace and not part of the base64 encoding alphabet defined atrfc 2045, an error is returned.BASE64M andBASE64 decoding have the same behavior. Input: '3q2+7w=='
Output: b'\xde\xad\xbe\xef'
ASCII Converts a string with only ASCII characters to bytes. If the input contains characters that aren't ASCII characters, an error is returned. Input: 'Hello'
Output: b'\x48\x65\x6c\x6c\x6f'
UTF-8 Converts a string to a sequence of UTF-8 bytes. Input: '$'
Output: b'\x24'
UTF8 Same behavior as UTF-8.

Return type

BYTES

Example

SELECTCAST('Hello'ASBYTESFORMAT'ASCII')ASstring_to_bytes-- Displays the bytes output value (b'\x48\x65\x6c\x6c\x6f')./*-------------------------* | string_to_bytes         | +-------------------------+ | b'\x48\x65\x6c\x6c\x6f' | *-------------------------*/

Format date and time as string

You can format these date and time parts as a string:

Case matching is supported when you format some date or time parts as a stringand the output contains letters. To learn more,seeCase matching.

Case matching

When the output of some format element contains letters, the letter cases ofthe output is matched with the letter cases of the format element,meaning the words in the output are capitalized according to how theformat element is capitalized. This is called case matching. The rules are:

  • If the first two letters of the element are both upper case, the words inthe output are capitalized. For exampleDAY =THURSDAY.
  • If the first letter of the element is upper case, and the second letter islowercase, the first letter of each word in the output is capitalized andother letters are lowercase. For exampleDay =Thursday.
  • If the first letter of the element is lowercase, then all letters in theoutput are lowercase. For example,day =thursday.

Format year part as string

CAST(expressionASSTRINGFORMATformat_string_expression)

Casts a data type that contains the year part to a string. Includesformat elements, which provide instructions for how to conduct the cast.

  • expression: This expression contains the data type with the yearthat you need to format.
  • format_string_expression: A string which contains format elements, includingthe year format element.

These data types include a year part:

  • DATE
  • DATETIME
  • TIMESTAMP

Ifexpression orformat_string_expression isNULL the return value isNULL. Ifformat_string_expression is an empty string, the output is anempty string. An error is generated if a value that isn't a supportedformat element appears informat_string_expression orexpression doesn'tcontain a value specified by a format element.

Format elementReturnsExample
YYYYYear, 4 or more digits. Input: DATE '2018-01-30'
Output: 2018
Input: DATE '76-01-30'
Output: 0076
Input: DATE '10000-01-30'
Output: 10000
YYYYear, last 3 digits only. Input: DATE '2018-01-30'
Output: 018
Input: DATE '98-01-30'
Output: 098
YYYear, last 2 digits only. Input: DATE '2018-01-30'
Output: 18
Input: DATE '8-01-30'
Output: 08
YYear, last digit only. Input: DATE '2018-01-30'
Output: 8
RRRRSame behavior as YYYY.
RRSame behavior as YY.

Return type

STRING

Example

SELECTCAST(DATE'2018-01-30'ASSTRINGFORMAT'YYYY')ASdate_time_to_string;/*---------------------* | date_time_to_string | +---------------------+ | 2018                | *---------------------*/

Format month part as string

CAST(expressionASSTRINGFORMATformat_string_expression)

Casts a data type that contains the month part to a string. Includesformat elements, which provide instructions for how to conduct the cast.

  • expression: This expression contains the data type with the monththat you need to format.
  • format_string_expression: A string which contains format elements, includingthe month format element.

These data types include a month part:

  • DATE
  • DATETIME
  • TIMESTAMP

Ifexpression orformat_string_expression isNULL the return value isNULL. Ifformat_string_expression is an empty string, the output is anempty string. An error is generated if a value that isn't a supportedformat element appears informat_string_expression orexpression doesn'tcontain a value specified by a format element.

Format elementReturnsExample
MMMonth, 2 digits. Input: DATE '2018-01-30'
Output: 01
MON Abbreviated, 3-character name of the month. The abbreviated month names for locale en-US are: JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC.Case matching is supported. Input: DATE '2018-01-30'
Output: JAN
MONTH Name of the month.Case matching is supported. Input: DATE '2018-01-30'
Output: JANUARY

Return type

STRING

Example

SELECTCAST(DATE'2018-01-30'ASSTRINGFORMAT'MONTH')ASdate_time_to_string;/*---------------------* | date_time_to_string | +---------------------+ | JANUARY             | *---------------------*/

Format day part as string

CAST(expressionASSTRINGFORMATformat_string_expression)

Casts a data type that contains the day part to a string. Includesformat elements, which provide instructions for how to conduct the cast.

  • expression: This expression contains the data type with the daythat you need to format.
  • format_string_expression: A string which contains format elements, includingthe day format element.

These data types include a day part:

  • DATE
  • DATETIME
  • TIMESTAMP

Ifexpression orformat_string_expression isNULL the return value isNULL. Ifformat_string_expression is an empty string, the output is anempty string. An error is generated if a value that isn't a supportedformat element appears informat_string_expression orexpression doesn'tcontain a value specified by a format element.

Format elementReturnsExample
DAY Name of the day of the week, localized. Spaces are padded on the right side to make the output size exactly 9.Case matching is supported. Input: DATE '2020-12-31'
Output: THURSDAY
DY Abbreviated, 3-character name of the weekday, localized. The abbreviated weekday names for locale en-US are: MON, TUE, WED, THU, FRI, SAT, SUN.Case matching is supported. Input: DATE '2020-12-31'
Output: THU
DDay of the week (1 to 7), starting with Sunday as 1. Input: DATE '2020-12-31'
Output: 4
DD2-digit day of the month. Input: DATE '2018-12-02'
Output: 02
DDD3-digit day of the year. Input: DATE '2018-02-03'
Output: 034

Return type

STRING

Example

SELECTCAST(DATE'2018-02-15'ASSTRINGFORMAT'DD')ASdate_time_to_string;/*---------------------* | date_time_to_string | +---------------------+ | 15                  | *---------------------*/

Format hour part as string

CAST(expressionASSTRINGFORMATformat_string_expression)

Casts a data type that contains the hour part to a string. Includesformat elements, which provide instructions for how to conduct the cast.

  • expression: This expression contains the data type with the hourthat you need to format.
  • format_string_expression: A string which contains format elements, includingthe hour format element.

These data types include a hour part:

  • TIME
  • DATETIME
  • TIMESTAMP

Ifexpression orformat_string_expression isNULL the return value isNULL. Ifformat_string_expression is an empty string, the output is anempty string. An error is generated if a value that isn't a supportedformat element appears informat_string_expression orexpression doesn'tcontain a value specified by a format element.

Format elementReturnsExample
HHHour of the day, 12-hour clock, 2 digits. Input: TIME '21:30:00'
Output: 09
HH12 Hour of the day, 12-hour clock. Input: TIME '21:30:00'
Output: 09
HH24 Hour of the day, 24-hour clock, 2 digits. Input: TIME '21:30:00'
Output: 21

Return type

STRING

Examples

SELECTCAST(TIME'21:30:00'ASSTRINGFORMAT'HH24')ASdate_time_to_string;/*---------------------* | date_time_to_string | +---------------------+ | 21                  | *---------------------*/
SELECTCAST(TIME'21:30:00'ASSTRINGFORMAT'HH12')ASdate_time_to_string;/*---------------------* | date_time_to_string | +---------------------+ | 09                  | *---------------------*/

Format minute part as string

CAST(expressionASSTRINGFORMATformat_string_expression)

Casts a data type that contains the minute part to a string. Includesformat elements, which provide instructions for how to conduct the cast.

  • expression: This expression contains the data type with the minutethat you need to format.
  • format_string_expression: A string which contains format elements, includingthe minute format element.

These data types include a minute part:

  • TIME
  • DATETIME
  • TIMESTAMP

Ifexpression orformat_string_expression isNULL the return value isNULL. Ifformat_string_expression is an empty string, the output is anempty string. An error is generated if a value that isn't a supportedformat element appears informat_string_expression orexpression doesn'tcontain a value specified by a format element.

Format elementReturnsExample
MIMinute, 2 digits. Input: TIME '01:02:03'
Output: 02

Return type

STRING

Example

SELECTCAST(TIME'21:30:00'ASSTRINGFORMAT'MI')ASdate_time_to_string;/*---------------------* | date_time_to_string | +---------------------+ | 30                  | *---------------------*/

Format second part as string

CAST(expressionASSTRINGFORMATformat_string_expression)

Casts a data type that contains the second part to a string. Includesformat elements, which provide instructions for how to conduct the cast.

  • expression: This expression contains the data type with the secondthat you need to format.
  • format_string_expression: A string which contains format elements, includingthe second format element.

These data types include a second part:

  • TIME
  • DATETIME
  • TIMESTAMP

Ifexpression orformat_string_expression isNULL the return value isNULL. Ifformat_string_expression is an empty string, the output is anempty string. An error is generated if a value that isn't a supportedformat element appears informat_string_expression orexpression doesn'tcontain a value specified by a format element.

Format elementReturnsExample
SSSeconds of the minute, 2 digits. Input: TIME '01:02:03'
Output: 03
SSSSSSeconds of the day, 5 digits. Input: TIME '01:02:03'
Output: 03723
FFn Fractional part of the second,n digits long. Replacen with a value from 1 to 9. For example, FF5. The fractional part of the second is rounded to fit the size of the output. Input for FF1: TIME '01:05:07.16'
Output: 1
Input for FF2: TIME '01:05:07.16'
Output: 16
Input for FF3: TIME '01:05:07.16'
Output: 016

Return type

STRING

Examples

SELECTCAST(TIME'21:30:25.16'ASSTRINGFORMAT'SS')ASdate_time_to_string;/*---------------------* | date_time_to_string | +---------------------+ | 25                  | *---------------------*/
SELECTCAST(TIME'21:30:25.16'ASSTRINGFORMAT'FF2')ASdate_time_to_string;/*---------------------* | date_time_to_string | +---------------------+ | 16                  | *---------------------*/

Format meridian indicator part as string

CAST(expressionASSTRINGFORMATformat_string_expression)

Casts a data type that contains the meridian indicator part to a string. Includesformat elements, which provide instructions for how to conduct the cast.

  • expression: This expression contains the data type with the meridian indicatorthat you need to format.
  • format_string_expression: A string which contains format elements, includingthe meridian indicator format element.

These data types include a meridian indicator part:

  • TIME
  • DATETIME
  • TIMESTAMP

Ifexpression orformat_string_expression isNULL the return value isNULL. Ifformat_string_expression is an empty string, the output is anempty string. An error is generated if a value that isn't a supportedformat element appears informat_string_expression orexpression doesn'tcontain a value specified by a format element.

Format elementReturnsExample
A.M. A.M. if the time is less than 12, otherwise P.M. The letter case of the output is determined by the first letter case of the format element. Input for A.M.: TIME '01:02:03'
Output: A.M.
Input for A.M.: TIME '16:02:03'
Output: P.M.
Input for a.m.: TIME '01:02:03'
Output: a.m.
Input for a.M.: TIME '01:02:03'
Output: a.m.
AM AM if the time is less than 12, otherwise PM. The letter case of the output is determined by the first letter case of the format element. Input for AM: TIME '01:02:03'
Output: AM
Input for AM: TIME '16:02:03'
Output: PM
Input for am: TIME '01:02:03'
Output: am
Input for aM: TIME '01:02:03'
Output: am
P.M.Output is the same as A.M. format element.
PMOutput is the same as AM format element.

Return type

STRING

Examples

SELECTCAST(TIME'21:30:00'ASSTRINGFORMAT'AM')ASdate_time_to_string;SELECTCAST(TIME'21:30:00'ASSTRINGFORMAT'PM')ASdate_time_to_string;/*---------------------* | date_time_to_string | +---------------------+ | PM                  | *---------------------*/
SELECTCAST(TIME'01:30:00'ASSTRINGFORMAT'AM')ASdate_time_to_string;SELECTCAST(TIME'01:30:00'ASSTRINGFORMAT'PM')ASdate_time_to_string;/*---------------------* | date_time_to_string | +---------------------+ | AM                  | *---------------------*/

Format time zone part as string

CAST(expressionASSTRINGFORMATformat_string_expression)

Casts a data type that contains the time zone part to a string. Includesformat elements, which provide instructions for how to conduct the cast.

  • expression: This expression contains the data type with the time zonethat you need to format.
  • format_string_expression: A string which contains format elements, includingthe time zone format element.

These data types include a time zone part:

  • DATE
  • TIME
  • DATETIME
  • TIMESTAMP

Ifexpression orformat_string_expression isNULL the return value isNULL. Ifformat_string_expression is an empty string, the output is anempty string. An error is generated if a value that isn't a supportedformat element appears informat_string_expression orexpression doesn'tcontain a value specified by a format element.

Format elementReturnsExample
TZH Hour offset for a time zone. This includes the+/- sign and 2-digit hour. Inputstamp: TIMESTAMP '2008-12-25 05:30:00+00' Output: −08
TZM Minute offset for a time zone. This includes only the 2-digit minute. Inputstamp: TIMESTAMP '2008-12-25 05:30:00+00' Output: 00

Return type

STRING

Examples

SELECTCAST(TIMESTAMP'2008-12-25 00:00:00+00:00'ASSTRINGFORMAT'TZH')ASdate_time_to_string;-- Results depend upon where this query was executed./*---------------------* | date_time_to_string | +---------------------+ | -08                 | *---------------------*/
SELECTCAST(TIMESTAMP'2008-12-25 00:00:00+00:00'ASSTRINGFORMAT'TZH'ATTIMEZONE'Asia/Kolkata')ASdate_time_to_string;-- Because the time zone is specified, the result is always the same./*---------------------* | date_time_to_string | +---------------------+ | +05                 | *---------------------*/
SELECTCAST(TIMESTAMP'2008-12-25 00:00:00+00:00'ASSTRINGFORMAT'TZM')ASdate_time_to_string;-- Results depend upon where this query was executed./*---------------------* | date_time_to_string | +---------------------+ | 00                  | *---------------------*/
SELECTCAST(TIMESTAMP'2008-12-25 00:00:00+00:00'ASSTRINGFORMAT'TZM'ATTIMEZONE'Asia/Kolkata')ASdate_time_to_string;-- Because the time zone is specified, the result is always the same./*---------------------* | date_time_to_string | +---------------------+ | 30                  | *---------------------*/

Format literal as string

CAST(expressionASSTRINGFORMATformat_string_expression)
Format elementReturnsExample
-Output is the same as the input.-
.Output is the same as the input..
/Output is the same as the input./
,Output is the same as the input.,
'Output is the same as the input.'
;Output is the same as the input.;
:Output is the same as the input.:
Whitespace Output is the same as the input. Whitespace means the space character, ASCII 32. It doesn't mean other types of space like tab or new line. Any whitespace character that isn't the ASCII 32 character in the format model generates an error.
"text" Output is the value within the double quotes. To preserve a double quote or backslash character, use the\" or\\ escape sequence. Other escape sequences aren't supported. Input: "abc"
Output: abc
Input: "a\"b\\c"
Output: a"b\c

Format string as date and time

You can format a string with these date and time parts:

When formatting a string with date and time parts, you must follow theformat model rules.

Format model rules

When casting a string to date and time parts, you must ensure theformat modelis valid. The format model represents the elements passed intoCAST(string_expression AS type FORMAT format_string_expression) as theformat_string_expression and is validated according to the followingrules:

  • It contains at most one of each of the following parts:meridian indicator, year, month, day, hour.
  • A non-literal, non-whitespace format element can't appear more than once.
  • If it contains the day of year format element,DDD, then it can't containthe month.
  • If it contains the 24-hour format element,HH24, then it can't contain the12-hour format element or a meridian indicator.
  • If it contains the 12-hour format element,HH12 orHH, then it must alsocontain a meridian indicator.
  • If it contains a meridian indicator, then it must also contain a 12-hourformat element.
  • If it contains the second of the day format element,SSSSS, then it can'tcontain any of the following: hour, minute, second, or meridian indicator.
  • It can't contain a format element such that the value it sets doesn't existin the target type. For example, an hour format element such asHH24 can'tappear in a string you are casting as aDATE.

Format string as year part

CAST(string_expressionAStypeFORMATformat_string_expression)

Casts a string-formatted year to a data type that containsthe year part. Includes format elements, which provide instructions for howto conduct the cast.

  • string_expression: This expression contains the string with the yearthat you need to format.
  • type: The data type to which you are casting. Must include the yearpart.
  • format_string_expression: A string which contains format elements, includingthe year format element. The formats elements in this string aredefined collectively as the format model, which must followthese rules.

These data types include a year part:

  • DATE
  • DATETIME
  • TIMESTAMP

If theYEAR part is missing fromstring_expression and the return typeincludes this part,YEAR is set to the current year.

An error is generated if a value that isn't a supported format element appearsinformat_string_expression orstring_expression doesn't contain a valuespecified by a format element.

Format elementReturnsExample
YYYY If it's delimited, matches 1 to 5 digits. If it isn't delimited, matches 4 digits. Sets the year part to the matched number. Input for MM-DD-YYYY: '03-12-2018'
Output as DATE: 2018-12-03
Input for YYYY-MMDD: '10000-1203'
Output as DATE: 10000-12-03
Input for YYYY: '18'
Output as DATE: 2018-03-01 (Assume current date is March 23, 2021)
YYY Matches 3 digits. Sets the last 3 digits of the year part to the matched number. Input for YYY-MM-DD: '018-12-03'
Output as DATE: 2018-12-03
Input for YYY-MM-DD: '038-12-03'
Output as DATE: 2038-12-03
YY Matches 2 digits. Sets the last 2 digits of the year part to the matched number. Input for YY-MM-DD: '18-12-03'
Output as DATE: 2018-12-03
Input for YY-MM-DD: '38-12-03'
Output as DATE: 2038-12-03
Y Matches 1 digit. Sets the last digit of the year part to the matched number. Input for Y-MM-DD: '8-12-03'
Output as DATE: 2008-12-03
Y,YYY Matches the pattern of 1 to 2 digits, comma, then exactly 3 digits. Sets the year part to the matched number. Input for Y,YYY-MM-DD: '2,018-12-03'
Output as DATE: 2008-12-03
RRRRSame behavior as YYYY.
RR

Matches 2 digits.

If the 2 digits entered are between 00 and 49 and the last 2 digits of the current year are between 00 and 49, the returned year has the same first 2 digits as the current year. If the last 2 digits of the current year are between 50 and 99, the first 2 digits of the returned year is 1 greater than the first 2 digits of the current year.

If the 2 digits entered are between 50 and 99 and the last 2 digits of the current year are between 00 and 49, the first 2 digits of the returned year are 1 less than the first 2 digits of the current year. If the last 2 digits of the current year are between 50 and 99, the returned year has the same first 2 digits as the current year.

Input for RR-MM-DD: '18-12-03'
Output as DATE: 2018-12-03 (executed in the year 2021) Output as DATE: 2118-12-03 (executed in the year 2050)
Input for RR-MM-DD: '50-12-03'
Output as DATE: 2050-12-03 (executed in the year 2021) Output as DATE: 2050-12-03 (executed in the year 2050)

Return type

The data type to which the string was cast. This can be:

  • DATE
  • DATETIME
  • TIMESTAMP

Examples

SELECTCAST('18-12-03'ASDATEFORMAT'YY-MM-DD')ASstring_to_date/*----------------* | string_to_date | +----------------+ | 2018-12-03     | *----------------*/

Format string as month part

CAST(string_expressionAStypeFORMATformat_string_expression)

Casts a string-formatted month to a data type that containsthe month part. Includes format elements, which provide instructions for howto conduct the cast.

  • string_expression: This expression contains the string with the monththat you need to format.
  • type: The data type to which you are casting. Must include the monthpart.
  • format_string_expression: A string which contains format elements, includingthe month format element. The formats elements in this string aredefined collectively as the format model, which must followthese rules.

These data types include a month part:

  • DATE
  • DATETIME
  • TIMESTAMP

If theMONTH part is missing fromstring_expression and the return typeincludes this part,MONTH is set to the current month.

An error is generated if a value that isn't a supported format element appearsinformat_string_expression orstring_expression doesn't contain a valuespecified by a format element.

Format elementReturnsExample
MM Matches 2 digits. Sets the month part to the matched number. Input for MM-DD-YYYY: '03-12-2018'
Output as DATE: 2018-12-03
MON Matches 3 letters. Sets the month part to the matched string interpreted as the abbreviated name of the month. Input for MON DD, YYYY: 'DEC 03, 2018'
Output as DATE: 2018-12-03
MONTH Matches 9 letters. Sets the month part to the matched string interpreted as the name of the month. Input for MONTH DD, YYYY: 'DECEMBER 03, 2018'
Output as DATE: 2018-12-03

Return type

The data type to which the string was cast. This can be:

  • DATE
  • DATETIME
  • TIMESTAMP

Examples

SELECTCAST('DEC 03, 2018'ASDATEFORMAT'MON DD, YYYY')ASstring_to_date/*----------------* | string_to_date | +----------------+ | 2018-12-03     | *----------------*/

Format string as day part

CAST(string_expressionAStypeFORMATformat_string_expression)

Casts a string-formatted day to a data type that containsthe day part. Includes format elements, which provide instructions for howto conduct the cast.

  • string_expression: This expression contains the string with the daythat you need to format.
  • type: The data type to which you are casting. Must include the daypart.
  • format_string_expression: A string which contains format elements, includingthe day format element. The formats elements in this string aredefined collectively as the format model, which must followthese rules.

These data types include a day part:

  • DATE
  • DATETIME
  • TIMESTAMP

If theDAY part is missing fromstring_expression and the return typeincludes this part,DAY is set to1.

An error is generated if a value that isn't a supported format element appearsinformat_string_expression orstring_expression doesn't contain a valuespecified by a format element.

Format elementReturnsExample
DDMatches 2 digits. Sets the day part to the matched number. Input for MONTH DD, YYYY: 'DECEMBER 03, 2018'
Output as DATE: 2018-12-03

Return type

The data type to which the string was cast. This can be:

  • DATE
  • DATETIME
  • TIMESTAMP

Examples

SELECTCAST('DECEMBER 03, 2018'ASDATEFORMAT'MONTH DD, YYYY')ASstring_to_date/*----------------* | string_to_date | +----------------+ | 2018-12-03     | *----------------*/

Format string as hour part

CAST(string_expressionAStypeFORMATformat_string_expression)

Casts a string-formatted hour to a data type that containsthe hour part. Includes format elements, which provide instructions for howto conduct the cast.

  • string_expression: This expression contains the string with the hourthat you need to format.
  • type: The data type to which you are casting. Must include the hourpart.
  • format_string_expression: A string which contains format elements, includingthe hour format element. The formats elements in this string aredefined collectively as the format model, which must followthese rules.

These data types include a hour part:

  • TIME
  • DATETIME
  • TIMESTAMP

If theHOUR part is missing fromstring_expression and the return typeincludes this part,HOUR is set to0.

An error is generated if a value that isn't a supported format element appearsinformat_string_expression orstring_expression doesn't contain a valuespecified by a format element.

Format elementReturnsExample
HH Matches 2 digits. If the matched numbern is12, setstemp = 0; otherwise, setstemp = n. If the matched value of the A.M./P.M. format element is P.M., setstemp = n + 12. Sets the hour part totemp. A meridian indicator must be present in the format model, when HH is present. Input for HH:MI P.M.: '03:30 P.M.'
Output as TIME: 15:30:00
HH12 Same behavior as HH.
HH24 Matches 2 digits. Sets the hour part to the matched number. Input for HH24:MI: '15:30'
Output as TIME: 15:30:00

Return type

The data type to which the string was cast. This can be:

  • TIME
  • DATETIME
  • TIMESTAMP

Examples

SELECTCAST('15:30'ASTIMEFORMAT'HH24:MI')ASstring_to_date_time/*---------------------* | string_to_date_time | +---------------------+ | 15:30:00            | *---------------------*/

Format string as minute part

CAST(string_expressionAStypeFORMATformat_string_expression)

Casts a string-formatted minute to a data type that containsthe minute part. Includes format elements, which provide instructions for howto conduct the cast.

  • string_expression: This expression contains the string with the minutethat you need to format.
  • type: The data type to which you are casting. Must include the minutepart.
  • format_string_expression: A string which contains format elements, includingthe minute format element. The formats elements in this string aredefined collectively as the format model, which must followthese rules.

These data types include a minute part:

  • TIME
  • DATETIME
  • TIMESTAMP

If theMINUTE part is missing fromstring_expression and the return typeincludes this part,MINUTE is set to0.

An error is generated if a value that isn't a supported format element appearsinformat_string_expression orstring_expression doesn't contain a valuespecified by a format element.

Format elementReturnsExample
MI Matches 2 digits. Sets the minute part to the matched number. Input for HH:MI P.M.: '03:30 P.M.'
Output as TIME: 15:30:00

Return type

The data type to which the string was cast. This can be:

  • TIME
  • DATETIME
  • TIMESTAMP

Examples

SELECTCAST('03:30 P.M.'ASTIMEFORMAT'HH:MI P.M.')ASstring_to_date_time/*---------------------* | string_to_date_time | +---------------------+ | 15:30:00            | *---------------------*/

Format string as second part

CAST(string_expressionAStypeFORMATformat_string_expression)

Casts a string-formatted second to a data type that containsthe second part. Includes format elements, which provide instructions for howto conduct the cast.

  • string_expression: This expression contains the string with the secondthat you need to format.
  • type: The data type to which you are casting. Must include the secondpart.
  • format_string_expression: A string which contains format elements, includingthe second format element. The formats elements in this string aredefined collectively as the format model, which must followthese rules.

These data types include a second part:

  • TIME
  • DATETIME
  • TIMESTAMP

If theSECOND part is missing fromstring_expression and the return typeincludes this part,SECOND is set to0.

An error is generated if a value that isn't a supported format element appearsinformat_string_expression orstring_expression doesn't contain a valuespecified by a format element.

Format elementReturnsExample
SS Matches 2 digits. Sets the second part to the matched number. Input for HH:MI:SS P.M.: '03:30:02 P.M.'
Output as TIME: 15:30:02
SSSSS Matches 5 digits. Sets the hour, minute and second parts by interpreting the matched number as the number of seconds past midnight. Input for SSSSS: '03723'
Output as TIME: 01:02:03
FFn Matchesn digits, wheren is the number following FF in the format element. Sets the fractional part of the second part to the matched number. Input for HH24:MI:SS.FF1: '01:05:07.16'
Output as TIME: 01:05:07.2
Input for HH24:MI:SS.FF2: '01:05:07.16'
Output as TIME: 01:05:07.16
Input for HH24:MI:SS.FF3: 'FF3: 01:05:07.16'
Output as TIME: 01:05:07.160

Return type

The data type to which the string was cast. This can be:

  • TIME
  • DATETIME
  • TIMESTAMP

Examples

SELECTCAST('01:05:07.16'ASTIMEFORMAT'HH24:MI:SS.FF1')ASstring_to_date_time/*---------------------* | string_to_date_time | +---------------------+ | 01:05:07.2          | *---------------------*/

Format string as meridian indicator part

CAST(string_expressionAStypeFORMATformat_string_expression)

Casts a string-formatted meridian indicator to a data type that containsthe meridian indicator part. Includes format elements, which provide instructions for howto conduct the cast.

  • string_expression: This expression contains the string with the meridian indicatorthat you need to format.
  • type: The data type to which you are casting. Must include the meridian indicatorpart.
  • format_string_expression: A string which contains format elements, includingthe meridian indicator format element. The formats elements in this string aredefined collectively as the format model, which must followthese rules.

These data types include a meridian indicator part:

  • TIME
  • DATETIME
  • TIMESTAMP

An error is generated if a value that isn't a supported format element appearsinformat_string_expression orstring_expression doesn't contain a valuespecified by a format element.

Format elementReturnsExample
A.M. or P.M. Matches using the regular expression'(A|P)\.M\.'. Input for HH:MI A.M.: '03:30 A.M.'
Output as TIME: 03:30:00
Input for HH:MI P.M.: '03:30 P.M.'
Output as TIME: 15:30:00
Input for HH:MI P.M.: '03:30 A.M.'
Output as TIME: 03:30:00
Input for HH:MI A.M.: '03:30 P.M.'
Output as TIME: 15:30:00
Input for HH:MI a.m.: '03:30 a.m.'
Output as TIME: 03:30:00

Return type

The data type to which the string was cast. This can be:

  • TIME
  • DATETIME
  • TIMESTAMP

Examples

SELECTCAST('03:30 P.M.'ASTIMEFORMAT'HH:MI A.M.')ASstring_to_date_time/*---------------------* | string_to_date_time | +---------------------+ | 15:30:00            | *---------------------*/

Format string as time zone part

CAST(string_expressionAStypeFORMATformat_string_expression)

Casts a string-formatted time zone to a data type that containsthe time zone part. Includes format elements, which provide instructions for howto conduct the cast.

  • string_expression: This expression contains the string with the time zonethat you need to format.
  • type: The data type to which you are casting. Must include the time zonepart.
  • format_string_expression: A string which contains format elements, includingthe time zone format element. The formats elements in this string aredefined collectively as the format model, which must followthese rules.

These data types include a time zone part:

  • DATE
  • TIME
  • DATETIME
  • TIMESTAMP

An error is generated if a value that isn't a supported format element appearsinformat_string_expression orstring_expression doesn't contain a valuespecified by a format element.

Format elementReturnsExample
TZH Matches using the regular expression'(\+|\-| )[0-9]{2}'. Sets the time zone and hour parts to the matched sign and number. Sets the time zone sign to be the first letter of the matched string. The number 2 means matching up to 2 digits for non-exact matching, and exactly 2 digits for exact matching. Input for YYYY-MM-DD HH:MI:SSTZH: '2008-12-25 05:30:00-08'
Output as TIMESTAMP: 2008-12-25 05:30:00-08
TZM Matches 2 digits. Letn be the matched number. If the time zone sign is the minus sign, sets the time zone minute part to-n. Otherwise, sets the time zone minute part ton. Input for YYYY-MM-DD HH:MI:SSTZH: '2008-12-25 05:30:00+05.30'
Output as TIMESTAMP: 2008-12-25 05:30:00+05.30

Return type

The data type to which the string was cast. This can be:

  • DATE
  • TIME
  • DATETIME
  • TIMESTAMP

Examples

SELECTCAST('2020.06.03 00:00:53+00'ASTIMESTAMPFORMAT'YYYY.MM.DD HH:MI:SSTZH')ASstring_to_date_time/*-----------------------------* | as_timestamp                | +-----------------------------+ | 2020-06-03 00:00:53.110 UTC | *-----------------------------*/

Format string as literal

CAST(string_expressionASdata_typeFORMATformat_string_expression)
Format elementReturnsExample
-Output is the same as the input.
.Output is the same as the input..
/Output is the same as the input./
,Output is the same as the input.,
'Output is the same as the input.'
;Output is the same as the input.;
:Output is the same as the input.:
Whitespace A consecutive sequence of one or more spaces in the format model is matched with one or more consecutive Unicode whitespace characters in the input. Space means the ASCII 32 space character. It doesn't mean the general whitespace such as a tab or new line. Any whitespace character that isn't the ASCII 32 character in the format model generates an error.
"text" Output generated by the format element in formatting, using this regular expression, withs representing the string input:regex.escape(s). Input: "abc"
Output: abc
Input: "a\"b\\c"
Output: a"b\c

Format numeric type as string

CAST(numeric_expressionASSTRINGFORMATformat_string_expression)

You can cast anumeric type to a string by combining thefollowing format elements:

Except for the exponent format element (EEEE), all of the format elementsgenerate a fixed number of characters in the output, and the output is alignedby the decimal point. The first character outputs a- for negative numbers;otherwise a space. To suppress blank characters and trailing zeroes, use theFM flag.

Note: Some format elements are locale-specific.GoogleSQL always uses the'en-us' locale.

Return type

STRING

Example

SELECTinput,CAST(inputASSTRINGFORMAT'$999,999.999')ASoutputFROMUNNEST([1.2,12.3,123.456,1234.56,-12345.678,1234567.89])ASinput/*------------+---------------* |   input    |    output     | +------------+---------------+ |        1.2 |        $1.200 | |       12.3 |       $12.300 | |    123.456 |      $123.456 | |    1234.56 |    $1,234.560 | | -12345.678 |  -$12,345.678 | | 1234567.89 |  $###,###.### | *------------+---------------*/

Format digits as string

The following format elements output digits. If there aren't enoughdigit format elements to represent the input, all digit format elements arereplaced with# in the output.If there are no sign format elements, one extra space is reserved for the sign.For example, if the input is12 and the format string is'99', then the output is' 12', with a length of threecharacters.

Format elementReturnsExample
0A decimal digit. Leading and trailing zeros are included. Input:12
Format:'000'
Output:' 012'
Input:12
Format:'000.000'
Output:' 012.000'
Input:-12
Format:'000.000'
Output:'-012.000'
9A decimal digit. Leading zeros are replaced with spaces. Trailing zeros are included. Input:12
Format:'999'
Output:'  12'
Input:12
Format:'999.999'
Output:'  12.000'
X or x

A hexadecimal digit. Can't appear with other format elements except 0, FM, and the sign format elements. The maximum number of hexadecimal digits in the format string is 16.

X generates uppercase letters and x generates lowercase letters.

When 0 is combined with the hexadecimal format element, the letter generated by 0 matches the case of the next X or x element. If there is no subsequent X or x, then 0 generates an uppercase letter.

Input:43981
Format:'XXXX'
Output:' ABCD'
Input:43981
Format:'xxxx'
Output:' abcd'
Input:43981
Format:'0X0x'
Output:' ABcd'
Input:43981
Format:'0000000X'
Output:' 0000ABCD'

Return type

STRING

Example

SELECTCAST(12ASSTRINGFORMAT'999')asa,CAST(-12ASSTRINGFORMAT'999')asb;/*------+------* | a    | b    | +------+------+ |   12 |  -12 | *------+------*/

Format decimal point as string

The following format elements output a decimal point. These format elements aremutually exclusive. At most one can appear in the format string.

Format elementReturnsExample
. (period)Decimal point. Input:123.58
Format:'999.999'
Output:' 123.580'
DThe decimal point of the current locale. Input:123.58
Format:'999D999'
Output:' 123.580'

Return type

STRING

Example

SELECTCAST(12.5ASSTRINGFORMAT'99.99')asa;/*--------* | a      | +--------+ |  12.50 | *--------*/

Format sign as string

The following format elements output the sign (+/-). These format elements aremutually exclusive. At most one can appear in the format string.

If there are no sign format elements, one extra space is reserved for the sign.For example, if the input is12 and the format string is'99', then the output is' 12', with a length of threecharacters.

The sign appears before the number. If the format model includes a currencysymbol element, then the sign appears before the currency symbol.

Format elementReturnsExample
SExplicit sign. Outputs+ for positive numbers and- for negative numbers. The position in the output is anchored to the number.NaN and0 will not be signed. Input:-12
Format:'S9999'
Output:'  -12'
Input:-12
Format:'9999S'
Output:'  12-'
MIExplicit sign. Outputs a space for positive numbers and- for negative numbers. This element can only appear in the last position. Input:12
Format:'9999MI'
Output:'  12 '
Input:-12
Format:'9999MI'
Output:'  12-'
PRFor negative numbers, the value is enclosed in angle brackets. For positive numbers, the value is returned with a leading and trailing space. This element can only appear in the last position. Input:12
Format:'9999PR'
Output:'   12 '
Input:-12
Format:'9999PR'
Output:'  <12>'

Return type

STRING

Example

SELECTCAST(12ASSTRINGFORMAT'S99')asa,CAST(-12ASSTRINGFORMAT'S99')asb;/*-----+-----* | a   | b   | +-----+-----+ | +12 | -12 | *-----+-----*/

Format currency symbol as string

The following format elements output a currency symbol. These format elementsare mutually exclusive. At most one can appear in the format string. In theoutput, the currency symbol appears before the first digit or decimal point.

Format elementReturnsExample
$Dollar sign ($). Input:-12
Format:'$999'
Output:' -$12'
C or cThe ISO-4217 currency code of the current locale. Input:-12
Format:'C999'
Output:' -USD12'
Input:-12
Format:'c999'
Output:' -usd12'
LThe currency symbol of the current locale. Input:-12
Format:'L999'
Output:' -$12'

Return type

STRING

Example

SELECTCAST(12ASSTRINGFORMAT'$99')asa,CAST(-12ASSTRINGFORMAT'$99')asb;/*------+------* | a    | b    | +------+------+ |  $12 | -$12 | *------+------*/

Format group separator as string

The following format elements output a group separator.

Format elementReturnsExample
, (comma)Group separator. Input:12345
Format:'999,999'
Output:'  12,345'
GThe group separator point of the current locale. Input:12345
Format:'999G999'
Output:'  12,345'

Return type

STRING

Example

SELECTCAST(1234ASSTRINGFORMAT'999,999')asa;/*----------* | a        | +----------+ |    1,234 | *----------*/

Other numeric format elements

Format elementReturnsExample
BOutputs spaces when the integer part is zero. If the integer part of the number is 0, then the following format elements generate spaces in the output: digits (9, X, 0), decimal point, group separator, currency, sign, and exponent. Input:0.23
Format:'B99.999S'
Output:'       '
Input:1.23
Format:'B99.999S'
Output:' 1.230+'
EEEEOutputs the exponent part of the value in scientific notation. If the exponent value is between -99 and 99, the output is four characters. Otherwise, the minimum number of digits is used in the output. Input:20
Format:'9.99EEEE'
Output:' 2.0E+01'
Input:299792458
Format:'S9.999EEEE'
Output:'+2.998E+08'
FMRemoves all spaces and trailing zeroes from the output. You can use this element to suppress spaces and trailing zeroes that are generated by other format elements. Input:12.5
Format:'999999.000FM'
Output:'12.5'
RNReturns the value as Roman numerals, rounded to the nearest integer. The input must be between 1 and 3999. The output is padded with spaces to the left to a length of 15. This element can't be used with other format elements exceptFM. Input:2021
Format:'RN'
Output:'          MMXXI'
VThe input value is multiplied by 10^n, where n is the number of 9s after theV. This element can't be used with a decimal point or exponent format element. Input:23.5
Format:'S000V00'
Output:'+02350'

Return type

STRING

Example

SELECTCAST(-123456ASSTRINGFORMAT'9.999EEEE')asa;"/*------------* | a          | +------------+ | -1.235E+05 | *------------*/

About BASE encoding

BASE encoding translates binary data in string format into a radix-Xrepresentation.

If X is 2, 8, or 16, Arabic numerals 0–9 and the Latin lettersa–z are used in the encoded string. So for example, BASE16/Hexadecimal encodingresults contain 0~9 and a~f.

If X is 32 or 64, the default character tables are defined inrfc 4648. When you decode a BASE string where X is 2, 8, or 16,the Latin letters in the input string are case-insensitive. For example, both"3a" and "3A" are valid input strings for BASE16/Hexadecimal decoding, andwill output the same result.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-10-02 UTC.