a表有userid, nickname userid存了用户名也存了用户id,不按规范存档
b表USER_ID是数值 33
b表username是字母 如 lozn
a表userid既有 33也有lozn,同时进行转换为昵称 的办法
select b.NICK_NAME,
from BizTable a left join UserTable b on
(ISNULL(TRY_CAST(a.userid AS INT), -1)=-1 and b.username= a.userid)
or
( ISNULL(TRY_CAST(a.userid AS INT), -1)<>-1 and b.USER_ID = TRY_CAST(a.userid AS INT))
刚开始我是直接判断TRY_CAST(a.userid AS INT)=NULL
发现并不能解决问题
也试过
CASE WHEN ISNUMERIC(a.aa) = 1 AND TRY_CAST(a.bb AS INT) IS NOT NULL
THEN b.USER_ID
ELSE b.USER_NAME
END AS [xx],
发现并不能解决问题而且依然抛出抛出转换字符串成数据类型 int 时失败
网友评论