我有3列SPRTELE_PHONE_AREA
,SPRTELE_PHONE_NUMBER
和SPRTELE_PHONE_EXT
-前缀,数字和后缀。
我创建了一种情况,如果SPRTELE_PHONE_NUMBER
和SPRTELE_PHONE_AREA
为空,我只想显示SPRTELE_PHONE_EXT
。否则,如果它们不为null,则输出将是带有括号和点的“(prefix)number.ext”。
这是我的代码:
SELECT
CASE
WHEN SPRTELE_PHONE_AREA IS NULL AND SPRTELE_PHONE_EXT IS NULL
THEN SPRTELE_PHONE_NUMBER
ELSE CONCAT('(', SPRTELE_PHONE_AREA, ')', SPRTELE_PHONE_NUMBER, '.', SPRTELE_PHONE_EXT)
END
FROM
vcc.sprtele;
问题是输出显示为每条记录的第二种情况:“ CONCAT ...”
问题出在哪里?
示例:前缀= 650,数字= 1234567,扩展名= 890
如果prefix为null而ext不为null-> 1234567.890,反之亦然
答案 0 :(得分:1)
您的查询缺少2种情况:
如果前缀为null而ext不为null 和
如果ext为空且前缀不为空。
[{"account":"1","cost":17.009999999999998},{"account":"2","cost":2.25}]
[{"anotheraccount":"a","cost":2}]
答案 1 :(得分:0)
如何?
COALESCE(CONCAT('(', SPRTELE_PHONE_AREA, ')', SPRTELE_PHONE_NUMBER, '.', SPRTELE_PHONE_EXT)
,CONCAT('(', SPRTELE_PHONE_AREA, ')', SPRTELE_PHONE_NUMBER
,CONCAT( SPRTELE_PHONE_NUMBER, '.', SPRTELE_PHONE_EXT)
,SPRTELE_PHONE_NUMBER,'')
COALESCE采用第一个非空值。
答案 2 :(得分:0)
你在这里
- url: /client-ssl
static_files: client-ssl
upload: client-ssl
application_readable: true
返回:
CREATE TABLE T(
Prefix VARCHAR(20),
Num VARCHAR(20),
Ext VARCHAR(20)
);
INSERT INTO T VALUES
('1', '11', '111'),
(NULL, '22', '222'),
('3', '33', NULL),
(NULL, '44', NULL);
SELECT CONCAT( IIF(Prefix IS NULL, '', CONCAT('(', Prefix, ')')),
Num,
IIF(Ext IS NULL, '', CONCAT('.', Ext))
) Results
FROM T
答案 3 :(得分:0)
此处的关键是将连接到字段值的格式字符(括号和句点)放入ISNULL()函数内:
+-----------+
| Results |
+-----------+
| (1)11.111 |
| 22.222 |
| (3)33 |
| 44 |
+-----------+
结果是:
;WITH Data AS (
SELECT 1 AS ExampleNumber,
'555' AS Prefix,
'1234567' AS PhoneNumber,
'999' AS Extension
UNION
SELECT 2 AS ExampleNumber,
NULL AS Prefix,
'1234567' AS PhoneNumber,
'999' AS Extension
UNION
SELECT 3 AS ExampleNumber,
'555' AS Prefix,
'1234567' AS PhoneNumber,
NULL AS Extension
)
SELECT ISNULL('(' + d.Prefix + ')', '')
+ ISNULL(d.PhoneNumber, '')
+ ISNULL('.' + d.Extension, '')
FROM Data d
ORDER BY d.ExampleNumber
如果您将回车符+换行符串联在一起,则此方法还可以用于连接地址1,地址2和地址3,而不会出现空格。