如果 T-SQL 中记录为 NULL,如何替换字符串
我正在编写一个 T-SQL 报告,显示不同客户处于不同状态的帐户数量。报告结果如下:
Customer1 NoService 7
Customer1 IncompleteOrder 13
Customer1 NULL 9
Customer2 NoService 12
Customer2 Available 19
Customer2 NULL 3
...
“NULL”状态是有效数据,但我不想显示 NULL,而是显示“待处理”。到目前为止,这是我的 SQL:
USE cdwCSP;
SELECT
sr.sales_region_name AS SalesRegion
, micv.value
, COUNT(sr.sales_region_name)
FROM prospect p
LEFT JOIN sales_region sr
ON p.salesRegionId = sr.sales_region_number
LEFT JOIN prospectOrder po
ON po.prospectId = p.prospectId
LEFT JOIN wo
ON wo.prospectId = p.prospectId
LEFT JOIN woTray wot
ON wot.woId = wo.woId
LEFT JOIN miscInformationCustomerCategory micc
ON micc.prospectId = p.prospectId
LEFT JOIN miscInformationCustomerValues micv
ON micv.miscInformationCustomerCategoryId = micc.miscInformationCustomerCategoryId
LEFT JOIN miscInformationCategory mic
ON micc.miscInformationCategoryId = mic.miscInformationCategoryId
WHERE wot.dateOut IS NULL
AND mic.categoryName LIKE '%Serviceability%'
GROUP BY sr.sales_region_name, micv.value
ORDER BY sr.sales_region_name, micv.value;
任何帮助将不胜感激,我仍在学习 T-SQL,所以这可能是一个很容易回答的问题。
I'm writing a T-SQL report that shows the number of accounts that are in different statuses for different customers. The report results in something like:
Customer1 NoService 7
Customer1 IncompleteOrder 13
Customer1 NULL 9
Customer2 NoService 12
Customer2 Available 19
Customer2 NULL 3
...
The 'NULL' status is valid data, but instead of displaying NULL, I want to display "Pending". Here is my SQL so far:
USE cdwCSP;
SELECT
sr.sales_region_name AS SalesRegion
, micv.value
, COUNT(sr.sales_region_name)
FROM prospect p
LEFT JOIN sales_region sr
ON p.salesRegionId = sr.sales_region_number
LEFT JOIN prospectOrder po
ON po.prospectId = p.prospectId
LEFT JOIN wo
ON wo.prospectId = p.prospectId
LEFT JOIN woTray wot
ON wot.woId = wo.woId
LEFT JOIN miscInformationCustomerCategory micc
ON micc.prospectId = p.prospectId
LEFT JOIN miscInformationCustomerValues micv
ON micv.miscInformationCustomerCategoryId = micc.miscInformationCustomerCategoryId
LEFT JOIN miscInformationCategory mic
ON micc.miscInformationCategoryId = mic.miscInformationCategoryId
WHERE wot.dateOut IS NULL
AND mic.categoryName LIKE '%Serviceability%'
GROUP BY sr.sales_region_name, micv.value
ORDER BY sr.sales_region_name, micv.value;
Any help would be appreciated, I'm still learning T-SQL so this might be an easy question to answer.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
COALESCE
或ISNULL
。前者是标准的,返回第一个NOT NULL
参数(如果所有参数都是NULL
,则返回NULL
)ISNULL
是仅限于 2 个参数,但如果要测试的第一个值的计算成本较高(例如子查询),则在 SQL Server 中效率更高。需要注意的
ISNULL
的一个潜在“陷阱”是,它返回第一个参数的数据类型,因此如果要替换的字符串比列数据类型长,您将需要进行强制转换。例如,
将返回
Unk
但
ISNULL(CAST(C as VARCHAR(7)),'Unknown')
或COALESCE
都可以按需要工作。You can use
COALESCE
orISNULL
. The former is standard and returns the firstNOT NULL
argument (orNULL
if all arguments areNULL
)ISNULL
is restricted to only 2 arguments but is more efficient in SQL Server if the first value to be tested is expensive to evaluate (e.g. a subquery).One potential "gotcha" with
ISNULL
to be aware of is that it returns the datatype of the first parameter so if the string to be substituted is longer than the column datatype would allow you will need a cast.E.g.
Would return
Unk
But
ISNULL(CAST(C as VARCHAR(7)),'Unknown')
orCOALESCE
would both work as desired.您还可以使用 ISNULL('value', 'replacewithvalue')
you can also use
ISNULL('value', 'replacewithvalue')
请检查 ISNULL 了解更多信息。
Go check ISNULL for further info.