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

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -