MySQL String to Date (Generic) -
can suggest function converts strings date?
> function takes string , give: > 0000-00-00 if string cannot converted (string in <> date out) > null if string null or blank(string in = date out) > yyyy-mm-dd if string can converted (string in = date out)
> function takes string , give: > 0000-00-00 if string cannot converted (string in <> date out) > null if string null or blank(string in = date out) > yyyy-mm-dd if string can converted (string in = date out) drop function if exists stringtodate; delimiter $$ create function `stringtodate`(v text) returns date begin declare result date; if (v null or v = '') set result = null; elseif (str_to_date(v,'%d-%m-%y') not null , length(clean(v)) > 8) set result = str_to_date(v,'%d-%m-%y'); elseif (str_to_date(v,'%d,%m,%y') not null , length(clean(v)) > 8) set result = str_to_date(v,'%d,%m,%y'); elseif (str_to_date(v,'%d/%m/%y') not null , length(clean(v)) > 8) set result = str_to_date(v,'%d/%m/%y'); elseif (str_to_date(v,'%y-%m-%d') not null , length(clean(v)) > 8) set result = str_to_date(v,'%y-%m-%d'); elseif (str_to_date(v,'%y,%m,%d') not null , length(clean(v)) > 8) set result = str_to_date(v,'%y,%m,%d'); elseif (str_to_date(v,'%y/%m/%d') not null , length(clean(v)) > 8) set result = str_to_date(v,'%y/%m/%d'); elseif (str_to_date(v,'%d-%m-%y') not null , length(clean(v)) < 10) set result = str_to_date(v,'%d-%m-%y'); elseif (str_to_date(v,'%d,%m,%y') not null , length(clean(v)) < 10) set result = str_to_date(v,'%d,%m,%y'); elseif (str_to_date(v,'%d/%m/%y') not null , length(clean(v)) < 10) set result = str_to_date(v,'%d/%m/%y'); else set result = cast("0000-00-00" date); end if; return result; end
Comments
Post a Comment