Spark SQL Date Function

  • Last update: February 17, 2025
  • Overview

    This document introduces the syntax of the date functions in Spark SQL.

    This document provides both New Calculation Column formulas and Spark SQL syntax, allowing you to choose freely.

    Timestamp

    You are advised to use DATETONUMBER in New Calculation Column of FineDataLink to convert the date into a 13-digit timestamp.

    You can also use the syntax in Spark SQL.

    Syntax

    Example

    unix_timestamp(): Returns the timestamp in the local time zone.

    select unix_timestamp()

    from_unixtime(Bigint-type unix timestamp, Format): Converts the number of seconds from unix epoch to a string representing the timestamp of that moment in the given format. For example, from_unixtime(1250111000,"yyyy-MM-dd") returns 2009-03-12.

    select from_unixtime(1557737000,"yyyy-MM-dd hh:mm:ss")

    unix_timestamp(Date): Converts the time string with the yyyy-MM-dd HH:mm:ss pattern to a timestamp. For example, unix_timestamp('2009-03-20 11:30:01') returns 1237573801.

    select unix_timestamp('2019-05-13   04:43:20')

    unix_timestamp(Date, Pattern): Converts a date string with a specified pattern to a Unix timestamp. Returns 0 if the format is incorrect. For example, unix_timestamp('2009-03-20', 'yyyy-MM-dd') returns 1237532400

    select unix_timestamp('2019/05/13   04:43:20',"yyyy/MM/dd hh:mm:ss"

    Date String

    Description

    Spark SQL Syntax

    Formula in New Calculation Column

    Recommendation

    Returns the year, month, and day parts of a datetime string.

    to_date(Timestamp)

     

    For example, to_date("1970-01-01   00:00:00") returns 1970-01-01.

    You can use the following formula in New Calculation Column.

    TODATE(LEFT(2023-06-13 00:01:27))

    The time field must be in the text format.

    TODATE(LEFT(2023-06-13 00:01:27))

    The time field must be in the date or timestamp format.

    Returned value: 2023-06-13 00:00:00

    You are advised to use the TODATELEFT, and FORMAT functions in New Calculation Column of FineDataLink.

    Returns the year part of a datetime string.

    year(Date)

     

    For example, year("1970-01-01") returns 1970.

    YEAR("2000/1/1")

    You are advised to use the YEAR function in New Calculation Column of FineDataLink.

    Returns the quarter of the given datetime as an integer.

    quarter(Date/Timestamp)

     

    For example, quarter('2015-04-08 ')   returns 2.

    Not supported currently.


    Returns the month of the date.

    month(Date)

     

    For example, month("1970-11-01") returns 1.

    MONTH("2000/1/1")

    You are advised to use the MONTH function in New Calculation Column of FineDataLink.

    Returns the day of the date.

    day(Date)

     

    For example, day("1970-11-01")   returns 1.

    DAY("2000/1/1")

    You are advised to use the DAYfunction in New Calculation Column of FineDataLink.

    Returns the hour of the specified time.

    hour(Date)

     

    For example, hour('2009-07-30 12:58: 59')   returns 12.

    HOUR("11:32:40")

    You are advised to use the HOUR function in New Calculation Column of FineDataLink.

    Returns the minute of the specified time.

    minute(Date)

     

    For example, minute('2009-07-30   12:58:59') returns 58.

    MINUTE("15:36:25")

    You are advised to use the MINUTE function in New Calculation Column of FineDataLink.

    Returns the second of the specified time.

    second(Date)

     

    For example, second('2009-07-30   12:58:59') returns 59.

    SECOND("15:36:25")

    You are advised to use the SECOND function in New Calculation Column of FineDataLink.

    Returns the week number of a given date within a year.

    weekofyear(Date)

     

    For example, weekofyear ("1970 - 11 - 01") returns 44.

    WEEK("2010/1/1")

    You are advised to use the WEEK function in New Calculation Column of FineDataLink.

    Returns the number of days, months, or years between two specified dates.

    datediff(End date, Start date)

     

    For example, datediff('2009-03-0 1', '2009-02-27') returns 2.

    DATEDIF("2001/2/28","2004/3/20","Y")

    You are advised to use the DATEDIF function in New Calculation Column of FineDataLink.

    Returns the date that is   years/months/days/hours/minutes/seconds after the provided date.

    Returns the date that is days after the start date: date_add(Start date, Integer number of days).

    Returns the date that is days before the start date: date_sub (Start date, Integer number of days).

    ADDTODATE("2023-01-01",   "y", 1)

    You are advised to use the ADDTODATE function in New Calculation Column of FineDataLink.

     

    Getting Current Time

    Description

    Spark SQL Syntax

    Formula in New Calculation Column

    Recommendation

    Returns the current time.

    current_timestamp

     

    Example: select current_timestamp

    NOW()

    You are advised to use the NOW function in New Calculation Column of FineDataLink.

    Returns the current date.

    current_date

     

    Example: select current_date

    TODATE(FORMAT(NOW(),"yyyy-MM-dd")) 

    You are advised to use the TODATE(FORMAT(NOW(),"yyyy-MM-dd")) formula in New Calculation Column of FineDataLink.

    Adding Months

    add_months(Start date, Integer number of months): Returns the date the specified number of months after the provided date.

    Example: 

    select add_months('2019-05-13',120)

    You are advised to use the following formula in New Calculation Column of FineDataLink: date(year(today()),MONTH(today())+num_months,day(today())). For details, see Common Date Formulas.

    Getting the Last Day of a Month

    last_day(Date): Returns the last day of the month to which the given date belongs without the hour, minute, and second parts (HH:mm:ss).

    Example: 

    select last_day('2019-02-01')

    You are advised to use the following formula in New Calculation Column of FineDataLink: DATE(YEAR(Date), MONTH(Date) + 1, 1-1). For details, see Common Date Formulas.

    Getting the Start of a Year or Month

    Use the TRUNC function of Spark SQL to get the start of the year or month for the time. The syntax is trunc(Date, Format).

    Example: 

    select trunc("2016-06-26","MM"),trunc("2016-06-26","YY")

    The start date of the year and the start date of the month for the date 2016-06-26 are returned, as shown in the following figure.

    You can also use the following formula in New Calculation Column of FineDataLink: DATE(YEAR(NOW()),01,01) and DATE(YEAR(NOW()),MONTH(NOW()),01).

    For more examples, see Common Date Formulas.

    Months Between Two Dates

    months_between (Date1, Date2): Returns the number of months between the first date and the second date. If the Date1 value is later than the Date2 value, the result is a positive number. If the Date1 value is earlier than the Date2 value, the result is a negative number. Otherwise, returns 0.0.

    Example:

    select months_between("2019-04-01", "2019-06-01")

    You are advised to use the DATEDIF function in New Calculation Column of FineDataLink.

    Getting the Date in a Specified Format

    date_format(Date/Timestamp/String, Format): Converts a date/timestamp/string to a value of string in the specified date format.

    Example: 

    select date_format('2019-05-13','MM/dd')

    You are advised to use the FORMAT function in New Calculation Column of FineDataLink to return date data in the specified format.

    Date of the Next Specified Day of Week

    next_day(Start date, Day of week): Returns the date of the first specified day of week (DOW) after the provided date.

    For example, select next_day('2019-05-13', 'TU') returns the date of the first Tuesday after May 13, 2019, as shown in the following figure. 

     


    附件列表


    主题: Data Development - Scheduled Task
    Previous
    Next
    • Helpful
    • Not helpful
    • Only read

    滑鼠選中內容,快速回饋問題

    滑鼠選中存在疑惑的內容,即可快速回饋問題,我們將會跟進處理。

    不再提示

    10s後關閉

    Get
    Help
    Online Support
    Professional technical support is provided to quickly help you solve problems.
    Online support is available from 9:00-12:00 and 13:30-17:30 on weekdays.
    Page Feedback
    You can provide suggestions and feedback for the current web page.
    Pre-Sales Consultation
    Business Consultation
    Business: international@fanruan.com
    Support: support@fanruan.com
    Page Feedback
    *Problem Type
    Cannot be empty
    Problem Description
    0/1000
    Cannot be empty

    Submitted successfully

    Network busy