SQL Case语句查询

发布于 2024-12-17 12:40:18 字数 223 浏览 1 评论 0原文

参见图片。

我有一张表格,里面装满了与特定部门相关的记录。如果部门名称位于“StandardWork”字段下,但位于“Support -”一词之后,那么用于创建名为 Service 的新列的 SQL 命令是什么,它将忽略“support”一词、空格和破折号,而仅使用部门来对吗?

在此处输入图像描述

See image.

I have a table full of records relating to specific departments. If the department name is held under the field 'StandardWork' but after the word 'Support -' what is the SQL command to create a new column called Service which will disregard the word support, the space and the dash and just use the department to the right?

enter image description here

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

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

发布评论

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

评论(4

慵挽 2024-12-24 12:40:18

如果 StandardWork 需要与 Service 一起存在,计算列

ALTER TABLE Whatever
   ADD [Service] AS SUBSTRING(StandardWork, 11, 8000);

如果要替换 StandardWork,则:

ALTER TABLE Whatever
   ADD [Service] varchar(200) NULL;

UPDATE Whatever SET [Service] = SUBSTRING(StandardWork, 11, 8000);

ALTER TABLE Whatever
   ALTER COLUMN [Service] varchar(200) NOT NULL;

ALTER TABLE Whatever
   DROP COLUMN StandardWork;

If StandardWork need to exist alongside Service, computed column

ALTER TABLE Whatever
   ADD [Service] AS SUBSTRING(StandardWork, 11, 8000);

If to replace StandardWork, then:

ALTER TABLE Whatever
   ADD [Service] varchar(200) NULL;

UPDATE Whatever SET [Service] = SUBSTRING(StandardWork, 11, 8000);

ALTER TABLE Whatever
   ALTER COLUMN [Service] varchar(200) NOT NULL;

ALTER TABLE Whatever
   DROP COLUMN StandardWork;
呆头 2024-12-24 12:40:18

我更喜欢使用 STUFF 函数来解决这些问题。

    select stuff(StandardWork, 1, 10, '')

此函数将删除字符 1-10 并用空字符串替换它们。

I prefer to use the STUFF function to solve these issues.

    select stuff(StandardWork, 1, 10, '')

This function will delete the characters 1-10 and replace them with an empty string.

空心↖ 2024-12-24 12:40:18

您可以使用替换

select replace(StandardWork, 'Support - ', '')

内容(如果您的部门名称中包含 Support -)。

select stuff(StandardWork, 1, 10, '')

You can use replace

select replace(StandardWork, 'Support - ', '')

or stuff if you have departments that has Support - as a part of the name.

select stuff(StandardWork, 1, 10, '')
说好的呢 2024-12-24 12:40:18

在 select 语句中:

select ...
       case when StandardWork like 'Support - %' 
            then replace(StandardWork, 'Support - ', '')
       end SupportSubCategory
       ...

Inside a select statement:

select ...
       case when StandardWork like 'Support - %' 
            then replace(StandardWork, 'Support - ', '')
       end SupportSubCategory
       ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文