SQL中的串联列与定界符。如果第一列为null,则不希望定界符在其之后出现。我如何摆脱它?

发布于 2025-02-06 16:48:02 字数 785 浏览 2 评论 0原文

SELECT TOP 1000 places.*
    , home_Desc
    , CONCAT_WS(' - ', COALESCE(brand, ' '), home_Desc, location_Desc)  AS homeSite

示例:

brandhome_desclocation_desc
蓝色木质
森林

所以我现在得到:

1.' blue - large - woody '
2. ' - small - forest '

但是第二盘我想要的是:

small - forest
SELECT TOP 1000 places.*
    , home_Desc
    , CONCAT_WS(' - ', COALESCE(brand, ' '), home_Desc, location_Desc)  AS homeSite

Example:

brandhome_Desclocation_Desc
bluelargewoody
NULLsmallforest

So right now I am getting:

1.' blue - large - woody '
2. ' - small - forest '

But what I want for the second set is:

small - forest

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

一曲琵琶半遮面シ 2025-02-13 16:48:02
CREATE TABLE TEST(brand VARCHAR(100), home_Desc VARCHAR(100), location_Desc VARCHAR(100))

INSERT INTO TEST VALUES ('blue','large','woody'),
(NULL,'small','forest')

SELECT CONCAT_WS(' - ',brand, home_Desc, location_Desc) FROM TEST

正如松鼠所说,您必须将 colesce(品牌,'')替换为品牌

CREATE TABLE TEST(brand VARCHAR(100), home_Desc VARCHAR(100), location_Desc VARCHAR(100))

INSERT INTO TEST VALUES ('blue','large','woody'),
(NULL,'small','forest')

SELECT CONCAT_WS(' - ',brand, home_Desc, location_Desc) FROM TEST

As Squirrel say, you must replace COALESCE(brand, '') to brand

樱花坊 2025-02-13 16:48:02

利用该规则,与null值(在本例中为分支)连接的非null值(在这种情况下的仪表符)将产生null(并假设其他两个永远不能为null/blank:

, COALESCE(brand + '-', ' ') + home_Desc + ' - ' + location_Desc AS homeSite

Taking advantage of the rule a non-NULL value (the dash character in this instance) concatenated with a NULL value (branch, in this instance) would yield NULL (and assuming that the other two can never be null/blank:

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