Timestamp functions Stay organized with collections Save and categorize content based on your preferences.
Spanner supports the following MySQL timestamp functions.You need to implement the MySQL functions in yourSpanner database before you can use them. For more information oninstalling the functions, seeInstall MySQL functions.
Function list
| Name | Summary |
|---|---|
mysql.DATEDIFF | Subtracts two dates, returns the number of days between them. |
mysql.LOCALTIME | Alias formysql.NOW. |
mysql.LOCALTIMESTAMP | Alias formysql.NOW. |
mysql.NOW | Returns the TIMESTAMP at which the query statement that contains this function started to run. |
mysql.DATEDIFF
mysql.DATEDIFF(timestamp_expression1,timestamp_expression2)Description
Calculates the number of days between twoTIMESTAMP values(timestamp_expression1 -timestamp_expression2).
This function supports the following arguments:
timestamp_expression1: The firstTIMESTAMPvalue (minuend).timestamp_expression2: The secondTIMESTAMPvalue (subtrahend).
Return data type
INT64
Differences from MySQL
This function only acceptsTIMESTAMP values. The MySQL version also acceptsDATE orDATETIME values.
Example
The following example calculates the difference in days between two timestamps:
SELECTmysql.DATEDIFF(TIMESTAMP'2025-01-10 10:00:00',TIMESTAMP'2025-01-01 05:00:00')asdays_difference;/*+-----------------+| days_difference |+-----------------+| 9 |+-----------------+*/mysql.LOCALTIME
mysql.LOCALTIME()Description
Returns theTIMESTAMP when the current query statement started to run. Thisfunction is an alias formysql.NOW() andmysql.LOCALTIMESTAMP().
This function doesn't support any arguments.
Return data type
TIMESTAMP
Example
The following example returns the start time of the current query:
SELECTmysql.LOCALTIME()ascurrent_query_time;/*+-------------------------------+| current_query_time |+-------------------------------+| 2025-06-03 12:28:32.123456+00 |+-------------------------------+*/mysql.LOCALTIMESTAMP
mysql.LOCALTIMESTAMP()Description
Alias forNOW.
mysql.NOW
mysql.NOW()Description
Returns theTIMESTAMP at which the current query statement started to run. Thisfunction is an alias formysql.LOCALTIME() andmysql.LOCALTIMESTAMP().
This function doesn't support any arguments.
Return data type
TIMESTAMP
Example
The following example returns the start time of the current query:
SELECTmysql.NOW()ascurrent_query_time;/*+-------------------------------+| current_query_time |+-------------------------------+| 2025-06-03 12:28:32.123456+00 |+-------------------------------+*/mysql.UTC_TIMESTAMP
mysql.UTC_TIMESTAMP()Description
Returns the current Coordinated Universal Time (UTC)TIMESTAMP at which the querystatement started to run. In this implementation, it behaves likemysql.NOW().
This function doesn't support any arguments.
Return data type
TIMESTAMP
Differences from MySQL
While MySQL'sUTC_TIMESTAMP() always returns a UTC timestamp regardless of thesession timezone, this function, as implemented, returns the query start time,which is inherently UTC in GoogleSQL.
Example
The following example returns the current UTC timestamp at the start of the query:
SELECTmysql.UTC_TIMESTAMP()ascurrent_utc_ts;/*+-------------------------------+| current_utc_ts |+-------------------------------+| 2025-06-03 12:28:32.123456+00 |+-------------------------------+*/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-12-15 UTC.