How to handle null values in date field in sql pgadmin

53 Views Asked by At

I need to find difference between two dates to calculate "total storage days" in pgadmin

date1-date2=total storage days

The date values is having null values, if i use isnull(date1,0)

ERROR: function isnull(date, unknown) does not exist
HINT: No function matches the given name and argument types. You might need to add explicit type casts.

If i use isnull(cast(date2 as integer),0) ERROR: cannot cast type date to integer

Please help me to calculate difference of 2 dates to find total storage date, if date field having null value also need to calculate total storage date

1

There are 1 best solutions below

2
Vivekanand Vishvkarma On

It calculate difference between date1 and date2 if not null both else 0

Select 
Case When date1 is not null And date2 is not null
Then DATEDIFF(day,date1,date2) Else 0 End total From TableName