DB2实现部分mysql函数

-------------------------------------------------------------------------- -- -- DB2 UDF(User-Defined Function) CURRENT_DATE -- -- 2010/02/03 -- -- Name of UDF: CURRENT_DATE ( ) -- -- Used UDF: None -- -- Description: Returns current date. -- -- Author: Woody -- -------------------------------------------------------------------------- CREATE FUNCTION "CURRENT_DATE" ( ) RETURNS DATE NO EXTERNAL ACTION ------------------------------------------------------------------------ -- SQL UDF (Scalar) ------------------------------------------------------------------------ BEGIN ATOMIC RETURN SELECT CURRENT_DATE FROM SYSIBM.DUAL; END @ -------------------------------------------------------------------------- -- -- DB2 UDF(User-Defined Function) CURDATE -- -- 2010/02/03 -- -- Name of UDF: CURDATE ( ) -- -- Used UDF: None -- -- Description: Returns current date. -- -- Author: Woody -- -------------------------------------------------------------------------- CREATE FUNCTION CURDATE ( ) RETURNS DATE NO EXTERNAL ACTION ------------------------------------------------------------------------ -- SQL UDF (Scalar) ------------------------------------------------------------------------ BEGIN ATOMIC RETURN SELECT CURRENT DATE FROM SYSIBM.DUAL; END @ -------------------------------------------------------------------------- -- -- DB2 UDF(User-Defined Function) DATE_FORMAT -- -- 2010/02/03 -- -- Name of UDF: DATE_FORMAT ( P_TS, P_FMT ) -- -- Used UDF: None -- -- Description: Format a timestamp value per the given format text. -- -- Author: Paul Yip ([email protected]) -- -- Note: This function was based on the function of site -- "http://www.ibm.com/developerworks/data/library/techarticle/0211yip/0211yip3.html" -------------------------------------------------------------------------- CREATE FUNCTION DATE_FORMAT ( P_TS TIMESTAMP, P_FMT VARCHAR(50) ) RETURNS VARCHAR(50) NO EXTERNAL ACTION DETERMINISTIC LANGUAGE SQL ------------------------------------------------------------------------ -- SQL UDF (Scalar) -- P_TS -- P_FMT ------------------------------------------------------------------------ BEGIN ATOMIC RETURN WITH TMP (DD,MM,YYYY,HH,MI,SS,NNNNNN) AS ( SELECT SUBSTR( DIGITS (DAY(P_TS)),9), SUBSTR( DIGITS (MONTH(P_TS)),9) , RTRIM(CHAR(YEAR(P_TS))) , SUBSTR( DIGITS (HOUR(P_TS)),9), SUBSTR( DIGITS (MINUTE(P_TS)),9), SUBSTR( DIGITS (SECOND(P_TS)),9), RTRIM(CHAR(MICROSECOND(P_TS))) FROM SYSIBM.SYSDUMMY1 ) SELECT CASE P_FMT WHEN '%Y-%m-%d %H:%i:%s' THEN YYYY || '-' || MM || '-' || DD || ' ' || HH || ':' || MI || ':' || SS WHEN '%Y-%c-%e' THEN YYYY || '-' || CASE WHEN MM < 10 THEN REPLACE(MM, '0', '') ELSE MM END || '-' || CASE WHEN DD < 10 THEN REPLACE(DD, '0', '') ELSE DD END WHEN '%Y-%m-%d' THEN YYYY || '-' || MM || '-' || DD ELSE RAISE_ERROR(99999001, P_FMT) END FROM TMP; END @ -------------------------------------------------------------------------- -- -- DB2 UDF(User-Defined Function) DATEDIFF -- -- 2010/02/04 -- -- Name of UDF: DATEDIFF ( ) -- -- Used UDF: None -- -- Description: Returns day difference between the given two dates. -- -- Author: Woody -- -------------------------------------------------------------------------- CREATE FUNCTION DATEDIFF ( P_DATETIME1 TIMESTAMP, P_DATETIME2 TIMESTAMP ) RETURNS INTEGER NO EXTERNAL ACTION ------------------------------------------------------------------------ -- SQL UDF (Scalar) -- P_DATETIME1 -- P_DATETIME2 ------------------------------------------------------------------------ BEGIN ATOMIC RETURN SELECT DATE(P_DATETIME1) - DATE(P_DATETIME2) FROM SYSIBM.DUAL; END @ -------------------------------------------------------------------------- -- -- DB2 UDF(User-Defined Function) Samples for conversion -- -- 2010/03/10 -- -- Name of UDF: FIND_IN_SET ( P_TS, P_FMT ) -- -- Used UDF: None -- -- Description: Find the target string postion -- from a string splitted by comma -- -- Author: Woody -- -------------------------------------------------------------------------- CREATE FUNCTION FIND_IN_SET ( TARGET VARCHAR(100), STR_SET VARCHAR(2000) ) RETURNS INTEGER NO EXTERNAL ACTION ------------------------------------------------------------------------ -- SQL UDF (Scalar) -- TARGET -- STR_SET ------------------------------------------------------------------------ BEGIN ATOMIC DECLARE POS INTEGER; DECLARE COMMA INTEGER; DECLARE VAL VARCHAR(100); SET POS = 1; SET COMMA = 0; SET VAL = ''; IF TARGET IS NULL OR STR_SET IS NULL THEN RETURN NULL; ELSEIF LENGTH(TARGET) = 0 OR LENGTH(STR_SET) = 0 THEN RETURN 0; END IF; WHILE (POS <= LENGTH(STR_SET)) DO IF SUBSTR(STR_SET, POS, 1) = ',' THEN SET COMMA = COMMA + 1; IF UPPER(VAL) = UPPER(TARGET) THEN RETURN COMMA; END IF; SET VAL = ''; ELSE SET VAL = VAL || SUBSTR(STR_SET, POS, 1); END IF; SET POS = POS + 1; END WHILE; IF UPPER(VAL) = UPPER(TARGET) THEN RETURN COMMA + 1; ELSE RETURN 0; END IF; END @ -------------------------------------------------------------------------- -- -- DB2 UDF(User-Defined Function) FROM_UNIXTIME -- -- 2010/01/25 -- -- Name of UDF: FROM_UNIXTIME ( ) -- -- Used UDF: TS_FMT -- -- Description: Implement specific from_unixtime function of mysql. -- -- Author: Woody -- -- Note: This function was based on the function in page 414 of book -- "MySQL to DB2 Conversion Guide" -------------------------------------------------------------------------- CREATE FUNCTION FROM_UNIXTIME ( SECONDS_SINCE_1970 INTEGER, FORMAT_TEXT VARCHAR(100) ) RETURNS TIMESTAMP ------------------------------------------------------------------------ -- SQL UDF (Scalar) -- SECONDS_SINCE_1970 -- FORMAT_TEXT ------------------------------------------------------------------------ BEGIN ATOMIC RETURN SELECT TS_FMT((TIMESTAMP(DATE('1970-01-01')) + SECONDS_SINCE_1970 SECOND) + CURRENT TIMEZONE, FORMAT_TEXT) FROM SYSIBM.DUAL; END @ -------------------------------------------------------------------------- -- -- DB2 UDF(User-Defined Function) Samples for conversion -- -- 2010/02/03 -- -- Name of UDF: NOW ( ) -- -- Used UDF: None -- -- Description: Alias name of current timestamp. -- -- Author: Woody -- -------------------------------------------------------------------------- CREATE FUNCTION NOW ( ) RETURNS TIMESTAMP NO EXTERNAL ACTION ------------------------------------------------------------------------ -- SQL UDF (Scalar) ------------------------------------------------------------------------ BEGIN ATOMIC RETURN SELECT CURRENT TIMESTAMP FROM SYSIBM.DUAL; END @ -------------------------------------------------------------------------- -- -- DB2 UDF(User-Defined Function) Samples for conversion -- -- 2010/02/22 -- -- Name of UDF: PERIOD_DIFF ( YM1, YM2 ) -- -- Used UDF: None -- -- Description: Returns months number between YM1 and YM2. -- -- Author: Woody -- -- Note: The format of both YM1 and YM2 should be like 'YYYYMM' -------------------------------------------------------------------------- CREATE FUNCTION PERIOD_DIFF ( YM1 VARCHAR(6), YM2 VARCHAR(6) ) RETURNS INTEGER NO EXTERNAL ACTION DETERMINISTIC ------------------------------------------------------------------------ -- SQL UDF (Scalar) -- YM1 -- YM2 ------------------------------------------------------------------------ BEGIN ATOMIC RETURN SELECT MONTHS_BETWEEN(TO_DATE(YM1, 'YYYYMM'),TO_DATE(YM2, 'YYYYMM')) FROM SYSIBM.DUAL; END @ -------------------------------------------------------------------------- -- -- DB2 UDF(User-Defined Function) TO_DAYS -- -- 2010/01/25 -- -- Name of UDF: TO_DAYS ( P_TIME ) -- -- Used UDF: None -- -- Description: work the same as mysql version -- -- Author: Woody -- -------------------------------------------------------------------------- CREATE FUNCTION TO_DAYS (P_TIME TIMESTAMP) RETURNS INTEGER NO EXTERNAL ACTION ------------------------------------------------------------------------ -- SQL UDF (Scalar) -- P_TIME ------------------------------------------------------------------------ BEGIN ATOMIC RETURN SELECT DAYS(P_TIME) + 365 FROM SYSIBM.DUAL; END @ -------------------------------------------------------------------------- -- -- DB2 UDF(User-Defined Function) Samples for conversion -- -- 2010/01/25 -- -- Name of UDF: UNIX_TIMESTAMP ( ) -- -- Used UDF: None -- -- Description: Implement specific unix_timestamp function of mysql. -- -- Author: Woody -- -- Note: This function was based on the function in page 414 of book -- "MySQL to DB2 Conversion Guide" -------------------------------------------------------------------------- CREATE FUNCTION UNIX_TIMESTAMP (TS VARCHAR(255)) RETURNS INTEGER LANGUAGE SQL ------------------------------------------------------------------------ -- SQL UDF (Scalar) -- TS ------------------------------------------------------------------------ BEGIN ATOMIC -- The implementation below is found at http://www.dbforums.com/db2/1004639-db2-unix-timestamp.html -- But as to the former part current time zone should be considered. DECLARE TTS TIMESTAMP; IF TIMESTAMP(TS) <= TIMESTAMP('1970-01-01') THEN RETURN 0; END IF; SET TTS = TIMESTAMP(TS) - CURRENT TIMEZONE; RETURN SELECT CAST (DAYS(TTS) - DAYS('1970-01-01') AS INTEGER) * 86400 + MIDNIGHT_SECONDS(TTS) FROM SYSIBM.DUAL; END @ -------------------------------------------------------------------------- -- -- DB2 UDF(User-Defined Function) WEEKDAY -- -- 2010/03/01 -- -- Name of UDF: WEEKDAY ( ) -- -- Used UDF: None -- -- Description: Returns current week day. -- -- Author: Woody -- -------------------------------------------------------------------------- CREATE FUNCTION WEEKDAY ( P_DATE DATE ) RETURNS INTEGER NO EXTERNAL ACTION DETERMINISTIC ------------------------------------------------------------------------ -- SQL UDF (Scalar) -- P_DATE ------------------------------------------------------------------------ BEGIN ATOMIC DECLARE OUTCOMING INT; SET OUTCOMING = DAYOFWEEK(P_DATE) - 2; IF OUTCOMING < 0 THEN SET OUTCOMING = OUTCOMING + 7; END IF; RETURN OUTCOMING; END @ -------------------------------------------------------------------------- -- -- DB2 UDF(User-Defined Function) YEARWEEK -- -- 2010/03/31 -- -- Name of UDF: YEARWEEK ( ) -- -- Used UDF: None -- -- Description: Returns current year and week. -- -- Author: Woody -- -------------------------------------------------------------------------- CREATE FUNCTION YEARWEEK ( DATETIME TIMESTAMP ) RETURNS VARCHAR(6) NO EXTERNAL ACTION DETERMINISTIC ------------------------------------------------------------------------ -- SQL UDF (Scalar) -- DATETIME ------------------------------------------------------------------------ BEGIN ATOMIC DECLARE WEEK_NO INTEGER; DECLARE YEAR_NO INTEGER; SET WEEK_NO = WEEK_ISO(DATETIME); SET YEAR_NO = YEAR(DATETIME); IF MONTH(DATETIME) = 1 AND DAY(DATETIME) < 10 AND WEEK_NO > 10 THEN SET YEAR_NO = YEAR_NO - 1; END IF; RETURN SELECT YEAR_NO || CASE WHEN WEEK_NO < 10 THEN '0' || WEEK_NO ELSE TO_CHAR(WEEK_NO) END FROM SYSIBM.DUAL; END @ -- CONNECT RESET -- @

你可能感兴趣的:(sql,Date,mysql,function,db2,tts)