我在表中表示firstname和lastname的两个字段,并且必须放在视图中的单个字段中。在这样做的同时,我必须全部放入小型大写。
SELECT
/* NAME*/ COALESCE(UPPER(SUBSTRING(LastName, 1, 1)), '') + COALESCE(LOWER(SUBSTRING(LastName, 2, LEN(LastName)-1)), '') + ' ' +
COALESCE(UPPER(SUBSTRING(FirstName, 1, 1)), '') + COALESCE(LOWER(SUBSTRING(FirstName, 2, LEN(FirstName)-1)), '') AS Name
这样可以正常工作,但是一些记录已插入,一个字段为空,因此子字符串会生成错误并停止所有查询。
我该如何管理?
答案 0 :(得分:0)
DECLARE @LastName NVARCHAR(30)= '';
DECLARE @FirstName NVARCHAR(30)= 'jhon';
SELECT CASE WHEN (LEN(@LastName) > 0 AND @LastName IS NOT NULL)
THEN
ISNULL(UPPER(LEFT(@LastName, 1)) + LOWER(RIGHT(@LastName, LEN(@LastName)-1)) + ' ', '')
ELSE '' END
+ CASE WHEN (LEN(@FirstName) > 0 AND @FirstName IS NOT NULL)
THEN
ISNULL(UPPER(LEFT(@FirstName, 1)) + LOWER(RIGHT(@FirstName, LEN(@FirstName)-1)), '')
ELSE '' END AS Name
答案 1 :(得分:0)
试试这个:
select
replace(lower(lastname),SUBSTRING(lastName , 1, 1),UPPER(SUBSTRING(lastName , 1, 1))) + ' ' +
replace(lower(firstname),SUBSTRING(firstName , 1, 1),UPPER(SUBSTRING(firstName , 1, 1)))