Db2

Db2

Connect with Db2, open source, and other data experts to gain value from your data, share insights, and solve problems.

 View Only
  • 1.  New SQL Functions

    Posted Mon March 30, 2020 11:17 AM

    Hi,

    I want to test some of the new functions and I need  more details for date_part - specially the list of allowed options and expected results (i.e. ISO results or not).

    Is this function added for Postgre compatibility?

     

    best regards

    Michael Tiefenbacher


    #Db2
    #Db2EarlyAccessProgram(EAP)Forum


  • 2.  RE: New SQL Functions

    Posted Wed March 31, 2021 12:59 PM

    Michael,

    Below is what's currently in our v11.1 development build of the Knowledge Center, for the date_part function. Let me know if that doesn't help.

    Erik

     

    DATE_PART scalar function

    The DATE_PART function returns a portion of a datetime based on its arguments. It extracts the subfield that is specified from the date, time, timestamp, and duration values.

    >>-DATE_PART--(--
    format-string
    --,--
    datetime-expression
    --)------><
    
    
    The schema is SYSIBM.
    format-string

    An expression that represents which unit of datetime is to be extracted from the date-expression. The expression that returns a built-in character string data type with an actual length that is not greater than 254 bytes. The format element in format-string specifies how the datetime-expression should be truncated. Leading and trailing blanks are removed from the string, and the resulting substring must be a valid format element for the type of datetime-expression (SQLSTATE 22007). Table 1 lists the allowable values for format-string.

    datetime-expression
    An expression that specifies the datetime value from which the unit specified by format-string is extracted. The expression must return a value that is a DATE, TIME, TIMESTAMP, date duration, time duration, or timestamp duration.
    Table 1. Allowable values for format-string
    format-string Description
    EPOCH The number of seconds since 1970-01-01 00:00:00.00. The value can be positive or negative.

    Valid for DATE, TIME, and TIMESTAMP.

    MILLENNIUM/MILLENNIUMS The millennium; for example, 2 indicates a date between 01 Jan 2000 and 31 Dec 2999.

    Valid for DATE, TIMESTAMP, date duration, and timestamp duration.

    CENTURY/CENTURIES The number of full 100-year periods represented by the year; for example, 20 indicates a date between 01 Jan 2000 and 31 Dec 2099.

    Valid for DATE, TIMESTAMP, date duration, and timestamp duration.

    DECADE/DECADES The number of full 10-year periods represented by the year; for example, 201 indicates a date between 01 Jan 2010 and 31 Dec 2019.

    Valid for DATE, TIMESTAMP, date duration, and timestamp duration.

    YEAR/YEARS The year; for example, 2015.

    Valid for DATE, TIMESTAMP, date duration , timestamp duration.

    QUARTER The quarter of the year (1 - 4) that the specified day is in.

    Valid for DATE, TIMESTAMP, date duration, and timestamp duration.

    MONTH/MONTHS

    The number of the month within the year (1 - 12).

    Valid for DATE, TIMESTAMP, date duration, and timestamp duration.

    WEEK

    The number of the week of the year (1 - 53) that the specified day is in. The value uses the ISO-8601 definition of a week, which begins on Monday; as a result, some years might have 53 weeks, and sometimes the first few days of January can be included as part of the 52nd or 53rd week of the previous year.

    Valid for DATE and TIMESTAMP.

    DAY/DAYS

    The day of the month (1 - 31).

    Valid for DATE, TIMESTAMP, date duration, and timestamp duration.

    DOW

    The day of the week, from 1 (Sunday) to 7 (Saturday).

    Valid for DATE and TIMESTAMP.

    DOY

    The day of the year (1 - 366).

    Valid for DATE and TIMESTAMP.

    HOUR/HOURS

    The hour of the day (0 - 23).

    Valid for TIME, TIMESTAMP, time duration, and timestamp duration.

    MINUTE/MINUTES

    The minute of the hour (0 - 59).

    Valid for TIME, TIMESTAMP, time duration, and timestamp duration.

    SECOND/SECONDS

    The second of the minute, not including fractional parts (0 - 59).

    Valid for TIME, TIMESTAMP, time duration, and timestamp duration.

    MILLISECOND/MILLISECONDS

    The second of the minute, including fractional parts to one thousandth of a second, multiplied by 1000 (0 - 59999).

    Valid for TIMESTAMP and timestamp duration.

    MICROSECOND/MICROSECONDS

    The second of the minute, including fractional parts to one millionth of a second, multiplied by 1000000 (0 - 59999999).

    Valid for TIMESTAMP and timestamp duration.

    The format-string values are case insensitive.

    The data type of the result of this function is BIGINT. If the argument can be null, the result can be null; if the argument is null, the result is the null value.

    Examples

    • Example 1: Extracting the day part from a date value. values DATE_PART('DAY', DATE('2007-02-18')); Result: 18
    • Example 2: Extracting the year part from a date duration. values DATE_PART('YEAR', cast(20130710 as decimal(8,0))); Result: 2013
    • Example 3: Extracting the hour part from a time duration. values DATE_PART('HOUR', cast(075559 as decimal(6,0))); Result: 7
      
      



  • 3.  RE: New SQL Functions

    Posted Wed March 31, 2021 12:59 PM

    Hi Erik

    thanks a lot very helpful - so why don't we get access to new Knowledge Center in general?

     

    More more question to the functionality

    select  date_part('dow', '01.03.2016') as date_part_dayofweek  

      fromsysibm.sysdummy1

    will return 3 (as documented).

    So this is not the ISO-standard which would be 2. This function would return 2 in Postgre - this is why I asked if it was implemented to gain Postgre compatibility.

     

    best regards

    Michael Tiefenbacher




  • 4.  RE: New SQL Functions

    Posted Mon March 30, 2020 11:18 AM

    Michael,

     

    This is a new function.  The function was specifically added for Netezza compatibility.  It does move us closer to postgreSQL compatible as well, but results are aligned with Netezza behavior.

    Hopefully the KC we've uploaded is of help, as well.

     

    Erik




  • 5.  RE: New SQL Functions

    Posted Mon March 30, 2020 11:20 AM

    Hi Erik,

     

    yes the Uploaded KC is really EXTREMELY helpful - thanks.

     

    Regarding the function:

    1. Netezza documentation says it will return ISO Standard
      https://www.ibm.com/support/knowledgecenter/SSULQD_7.2.0/com.ibm.nz.dbu.doc/r_dbuser_functions_expressions_extract_datetime.html?lang=de
      So this function is buggy in my eyes - could you please confirm.
    2. I would like to see in the DB2 documentation that it should return weeks in ISO format.

     

    best regards

    Michael Tiefenbacher