如果 T-SQL 中记录为 NULL,如何替换字符串

发布于 2024-10-30 08:49:37 字数 1281 浏览 2 评论 0原文

我正在编写一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

静若繁花 2024-11-06 08:49:37

您可以使用 COALESCEISNULL。前者是标准的,返回第一个 NOT NULL 参数(如果所有参数都是 NULL,则返回 NULL

SELECT COALESCE(micv.value,'Pending') as value

ISNULL 是仅限于 2 个参数,但如果要测试的第一个值的计算成本较高(例如子查询),则在 SQL Server 中效率更高。

需要注意的 ISNULL 的一个潜在“陷阱”是,它返回第一个参数的数据类型,因此如果要替换的字符串比列数据类型长,您将需要进行强制转换。

例如,

CREATE TABLE T(C VARCHAR(3) NULL);

INSERT T VALUES (NULL);

SELECT ISNULL(C,'Unknown')
FROM T

将返回 Unk

ISNULL(CAST(C as VARCHAR(7)),'Unknown')COALESCE 都可以按需要工作。

You can use COALESCE or ISNULL. The former is standard and returns the first NOT NULL argument (or NULL if all arguments are NULL)

SELECT COALESCE(micv.value,'Pending') as value

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.

CREATE TABLE T(C VARCHAR(3) NULL);

INSERT T VALUES (NULL);

SELECT ISNULL(C,'Unknown')
FROM T

Would return Unk

But ISNULL(CAST(C as VARCHAR(7)),'Unknown') or COALESCE would both work as desired.

没有伤那来痛 2024-11-06 08:49:37

您还可以使用 ISNULL('value', 'replacewithvalue')

you can also use ISNULL('value', 'replacewithvalue')

临走之时 2024-11-06 08:49:37
SELECT
   sr.sales_region_name   AS SalesRegion
   , ISNULL(micv.value,'Pending')
   , COUNT(sr.sales_region_name)
FROM prospect p
--(...)

请检查 ISNULL 了解更多信息。

SELECT
   sr.sales_region_name   AS SalesRegion
   , ISNULL(micv.value,'Pending')
   , COUNT(sr.sales_region_name)
FROM prospect p
--(...)

Go check ISNULL for further info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文