Date Function in Teradata

天涯浪子 提交于 2019-12-08 20:02:37

In TD13.10+ you can create a simple SQL UDF:

-- (year, month, day) to date, "invalid date" error if no valid date
REPLACE FUNCTION ymd_to_date(y INTEGER, m INTEGER, d INTEGER)
RETURNS DATE
SPECIFIC ymd_to_date
RETURNS NULL ON NULL INPUT
CONTAINS SQL
DETERMINISTIC
COLLATION INVOKER
INLINE TYPE 1
RETURN 
   ((y - 1900) * 10000 + (m * 100) + d (DATE))

Dieter

As far i as i know there is no predefined date function where you pass the integers and it returns the date.. however you can write you know UDF..

One example in teradata:

 BTEQ -- Enter your SQL request or BTEQ command:
sel cast ('11'||'-'||'12'||'-'||'12' as date format 'dd-mm-yy');

sel cast ('11'||'-'||'12'||'-'||'12' as date format 'dd-mm-yy');

 *** Query completed. One row found. One column returned.
 *** Total elapsed time was 1 second.

(((('11'||'-')||'12')||'-')||'12')
----------------------------------
                          11-12-12

So you can simply achieve your desired result by following query:

sel cast (cast(p1_day as char(2))||'-'||cast(p2_month as char(2))||'-'||
cast(p3_year as char(2)) as date format 'dd-mm-yy');

P.S. : There doesn't seem any such built-in function in teradata.

To add to Deiter's function, look at the below queries. Teradata stores dates as Integers with an offset from 1900.

   SELECT CAST(CURRENT_DATE AS INTEGER);

    ---- 1150818

    SELECT CURRENT_DATE;

    --- 2015-08-18

---- the format of storage is (<year> - 1900)mmdd
    SELECT (2015 -1900)*100000 + (8 * 100)+ 18;

    ------ 1150818

115 years from 1900, month followed by date.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!