문제

I have the following part of my query:

ISNULL(cast(fd.decision_date as varchar(20)), 'PENDING') as facility,
ISNULL(cast(cd.decision_date as varchar(20)), 'PENDING') as corporate,
ISNULL(cast(cb.creation_date as varchar(20)), 'PENDING') as billing

My values are originally of datetime datatype, but what I want to do is return the word 'PENDING' if the value is NULL.

With the code above, my results are casted to a varchar, thus returning something like:

Aug 20 2013 9:35AM 

instead of 2013-08-20 09:35:54

Suggestions would be greatly appreciated.

도움이 되었습니까?

해결책

Replace cast with convert and appropriate style: Convert syntax & styles, eg:

ISNULL(convert(varchar(20), fd.decision_date, 120), 'PENDING') as facility,

다른 팁

Instead of cast(... as varchar(20)), use convert with a style number, this is exactly what that's for.

SELECT CONVERT(CHAR(19), GETDATE(), 120);

So your query becomes:

COALESCE(CONVERT(CHAR(19), fd.decision_date, 120), 'PENDING') as facility,
COALESCE(CONVERT(CHAR(19), cd.decision_date, 120), 'PENDING') as corporate,
COALESCE(CONVERT(CHAR(19), cb.creation_date, 120), 'PENDING') as billing
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top