如果日期值为空,则将日期时间转换为字符串-保持原始日期时间格式
问题描述:
我的查询包含以下部分:
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
我的值最初是datetime数据类型,但我想做的是
My values are originally of datetime datatype, but what I want to do is return the word 'PENDING' if the value is NULL.
使用上面的代码,我的结果被 cast
ed了到 varchar
,因此返回以下内容:
With the code above, my results are cast
ed to a varchar
, thus returning something like:
Aug 20 2013 9:35AM
而不是 2013-08-20 09:35:54
建议将不胜感激。
答
用转换
和适当的 style 替换 cast
:转换语法&样式,例如:
Replace cast
with convert
and appropriate style: Convert syntax & styles, eg:
ISNULL(convert(varchar(20), fd.decision_date, 120), 'PENDING') as facility,