select ISDATE('20170213') from dummy;
select ISDATE('JUNK') from dummy;
如何操作是HANA SQL
。
好,这似乎行得通。
select DATS_IS_VALID('20170213') from dummy; -- 1 -- expects YYYYMMDD only
select DATS_IS_VALID('2017-0213') from dummy; -- 0
select DATS_IS_VALID('2017-02-13') from dummy; -- 0
select DATS_IS_VALID('20170233') from dummy; -- 0
select DATS_IS_VALID('21-02-2017') from dummy; -- 0
select DATS_IS_VALID('02212017') from dummy; -- 0
另请参见tstmp_is_valid()了解时间戳。另请参阅here
或者,使用UDF-仅采用YYYYMMDD和YYYY-MM-DD
--DROP FUNCTION ISDATE;
CREATE FUNCTION ISDATE (inputval NVARCHAR(100))
-- expects format YYYYMMDD
RETURNS RETURNVAL BOOLEAN
LANGUAGE SQLSCRIPT
AS
BEGIN
DECLARE DT date;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
RETURNVAL := false;
END;
select CAST(:inputval AS DATE) into DT from dummy;
RETURNVAL := true;
END;
select ISDATE('20170213') from dummy; --true
select ISDATE('2017-02-13') from dummy; --true
select ISDATE('2017-FEB-13') from dummy; --false
select ISDATE('13-12-2017') from dummy; --false
select ISDATE('abc') from dummy; --false
select ISDATE('13-Dec-2017') from dummy; --false
select ISDATE('20170232') from dummy; --false
这里是处理错误数据的“截止日期”,如果无效,则返回NULL,请确保您所有的日期都与格式字符串匹配,否则它将转换为null。
CREATE FUNCTION TO_DATE_V2 (inputval NVARCHAR(100), format_str NVARCHAR(20))
RETURNS RETURNVAL DATE
LANGUAGE SQLSCRIPT
AS
BEGIN
DECLARE DT date;
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
RETURNVAL := NULL;
END;
select TO_DATE(:inputval, :format_str) into DT from dummy;
RETURNVAL := :DT;
END;
select TO_DATE_V2('20-04/2017', 'DD/MM/YYYY') from dummy; -- null
select TO_DATE_V2('20/04/2017', 'DD/MM/YYYY') from dummy; -- 2017-04-20
select TO_DATE_V2('JUNK', 'DD/MM/YYYY') from dummy; -- null