Link to home
Start Free TrialLog in
Avatar of ayha1999
ayha1999

asked on

Max(date)

the following command returns even the default '1900-01-01 00:00:00.000'. how can I avoid returning the default date?

isnull(convert(datetime, max(compDate),103),'') as 'Exp. comp. date'

thanks
Avatar of kaminda
kaminda
Flag of Sri Lanka image

You can use a case statement to check wether the result is equal to default '1900-01-01 00:00:00.000' if so return null or empty string.
isnull(convert(datetime, max(CASE WHEN DATEPART(year,compDate) = 1900 THEN NULL ELSE compDate),103),'')

Open in new window

Try:

SELECT isnull(convert(datetime, max(compDate),103),'') as 'Exp. comp. date'
FROM table_name WHERE compDate <> '1900-01-01 00:00:00.000'
Avatar of ayha1999
ayha1999

ASKER

hi kaminda,
Incorrect syntax near the keyword 'convert'. with the statement.
ASKER CERTIFIED SOLUTION
Avatar of kaminda
kaminda
Flag of Sri Lanka image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
still same error
 Incorrect syntax near the keyword 'convert'.
try

convert(varchar, max(compDate),103) as 'Exp. comp. date'

or

isnull(convert(varchar, max(compDate),103),'') as 'Exp. comp. date'
hi ralmda,
same error returned.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial