SQL从另一列的值创建列

时间:2022-12-25 22:50:43

I have the following table:

我有下表:

|  id  |         name            |
----------------------------------
|  1   | 236 SRTD - Something 1  |
----------------------------------
|  2   | 236 SRTD - Something 2  |
----------------------------------
|  3   | 236 SRTD - Something 3  |
----------------------------------
|  4   | 387 SRTD - Something 1  |

from that table I would like to construct another similar view but that looks like the following:

从该表我想构建另一个类似的视图,但看起来像如下:

|  id  |  SRTD  |         name            |
-------------------------------------------
|  1   |  236   | 236 SRTD - Something 1  |
-------------------------------------------
|  2   |  236   | 236 SRTD - Something 2  |
-------------------------------------------
|  3   |  236   | 236 SRTD - Something 3  |
-------------------------------------------
|  4   |  387   | 387 SRTD - Something 1  |

How can I modify the name column, obtain the SRTD number, and then create another column that contains that value.

如何修改名称列,获取SRTD编号,然后创建包含该值的另一​​列。

1 个解决方案

#1


3  

For SQL Server:

对于SQL Server:

SELECT id, SRTD = SUBSTRING(name, 1, CHARINDEX(' ', name)), name
  FROM dbo.table;

#1


3  

For SQL Server:

对于SQL Server:

SELECT id, SRTD = SUBSTRING(name, 1, CHARINDEX(' ', name)), name
  FROM dbo.table;