I read some questions where questioners ask "Sometimes client gives data where dates are expressed as float or integer values.How do I find maximum date?". Ex March 02, 2006 can be expressed as 02032006.0 020306 2032006 20306 020306.0000 2032006Assuming the values are expressed in dmy format
The possible way is convert that value into proper date so that all types of date related calculations can be doneCreate function proper_date (@date_val varchar(25))returns datetime asBegin Select @date_val= case when @date_val like '%.0%' then substring(@date_val,1,charindex('.',@date_val)-1) else @date_val end return cast( case when @date_val like '%[a-zA-Z-/]%' then case when ISDATE(@date_val)=1 then @date_val else NULL end when len(@date_val)=8 then right(@date_val,4)+'-'+substring(@date_val,3,2)+'-'+left(@date_val,2) when len(@date_val)=7 then right(@date_val,4)+'-'+substring(@date_val,2,2)+'-0'+left(@date_val,1) when len(@date_val)=6 then case when right(@date_val,2)<50 then '20' else '19' end +right(@date_val,2)+'-'+substring(@date_val,3,2)+'-'+left(@date_val,2) when len(@date_val)=5 then case when right(@date_val,2)<50 then '20' else '19' end +right(@date_val,2)+'-'+substring(@date_val,2,2)+'-0'+left(@date_val,1) else case when ISDATE(@date_val)=1 then @date_val else NULL end end as datetime )End
This function will convert them into proper dateselect dbo.proper_date('02032006.0') as proper_date, dbo.proper_date('020306.000') as proper_date, dbo.proper_date('02032006') as proper_date, dbo.proper_date('020306') as proper_date, dbo.proper_date('20306') as proper_date, dbo.proper_date('020306') as proper_date
Apart from converting integer or float values to date, it will also convert date strings to dateSelect dbo.proper_date('March 2, 2006') as proper_date, dbo.proper_date('2 Mar, 2006') as proper_date, dbo.proper_date('2006 Mar 2') as proper_date, dbo.proper_date('2-Mar-2006') as proper_date, dbo.proper_date('3/02/2006') as proper_date, dbo.proper_date('02-03-2006') as proper_date, dbo.proper_date('2006/03/02') as proper_date, dbo.proper_date('March 2006') as proper_date, dbo.proper_date('2 Mar 2006') as proper_date
MadhivananFailing to plan is Planning to fail