Reputation: 9
Please help me in getting date difference between two dates in SQL server. Below query giving error
DATEDIFF(DD,(convert(date,[Fdate])),(convert(date,[Ldate])))[DDiff]
Upvotes: 0
Views: 106
Reputation: 38023
Since you are on SQL Server 2012, use try_convert()
:
select datediff(day,try_convert(date,[fdate]),try_convert(date,[ldate])) as ddiff
In Sql Server 2012 and up: each of these will return null
when the conversion fails instead of an error.
The main issue may be that you are storing dates as a character data type. If so, that can cause additional issues depending on what format the date is stored in.
You may need to specify a style with try_convert()
, or set dateformat
to MDY
or DMY
, or use a specific culture with try_parse()
.
Upvotes: 3