简体   繁体   English

使用t-sql从现有列创建新列

[英]creating new column from existing column using t-sql

i am trying to create new column from existing column(table) in a new table. 我正在尝试从新表中的现有column(table)创建新列。

this is my old table 这是我的旧桌子

projectnum       allw     budjet
648PE2075       152.00     230.00
648PE2075A      33.33      00.00
333AD0221B       125.11    1256.00
123CF0023        125.22     215.33

I need to create a new table that have a new column called Project_code created from projectnum column and all the old columns. 我需要创建一个新表,该表具有一个由projectnum列和所有旧列创建的名为Project_code的新列。 looks like this 看起来像这样

projectnum   Project_code      allw       budjet
648PE2075       648-075       152.00     230.00
648PE2075A      648-075-A      33.33      00.00
333AD0221B      333-221-B      125.11    1256.00
123CF0023        123-023       125.22     215.33

My challeng is when i try to write t_sql statement. 我的挑战是当我尝试编写t_sql语句时。 some records of projectnum are 10 character rest 9 character. projectnum的某些记录是10个字符,其余9个字符。 Help Please 请帮助

SELECT
    projectnum,
    CASE LEN(projectnum) 
        WHEN 9 THEN LEFT(projectnum,3) + '-' + SUBSTRING(projectnum,6,3)
        WHEN 10 THEN LEFT(projectnum,3) + '-' + SUBSTRING(projectnum,6,3) + '-' + RIGHT(projectnum,1)
    END AS Project_code,
    allw,
    budjet
INTO MyNewTable
FROM MyOldTable

Just obviously swap the table names! 显然只是交换表名! This will create the new table too, if you already have the table just change it so it reads 如果您已经有了表,也将创建新表,只需对其进行更改,使其读取

INSERT INTO MyNewTable(projectnum,Project_code, allw, budjet)
SELECT
    projectnum,
    CASE LEN(projectnum) 
        WHEN 9 THEN LEFT(projectnum,3) + '-' + SUBSTRING(projectnum,6,3)
        WHEN 10 THEN LEFT(projectnum,3) + '-' + SUBSTRING(projectnum,6,3) + '-' + RIGHT(projectnum,1)
    END AS Project_code,
    allw,
    budjet
FROM MyOldTable

There are much more elegant solutions that would allow for more options but hopefully this will work or give you an idea how to solve any other similar issues. 有许多更优雅的解决方案可以提供更多选择,但希望它能起作用,或者为您提供解决其他类似问题的思路。

Al. 人。

I suggest using select ... into ... from ... to create a new table from existing data in one step. 我建议一步一步地使用select ... into ... from ...从现有数据创建一个新表。 For your string operations substring() seems appropriate. 对于您的字符串操作, substring()似乎合适。 Please try the following query: 请尝试以下查询:

select
    projectnum,
    allw,
    budjet,
    substring(projectnum, 1, 3)
        + '-'
        + substring(projectnum, 7, 3)
        + case
            when len(projectnum) = 10
            then '-' + substring(projectnum, len(projectnum) - 1, 1) end
        as project_code

into
    new_table

from
    old_table

Read more on substring() at the Microsoft Docs . Microsoft Docs上了解有关substring()更多信息。

Make it reusable logic as a function 使它成为功能可重用的逻辑

create function code(@num varchar(20)) returns varchar(20) as
begin    
     return substring(@num, 1, 3) + '-' + substring(@num, 7, 3)
       + case when len(@num) = 10 then '-' + substring(@num, 10, 1) else '' end
end

the use as needed 根据需要使用

select dbo.code('648PE2075')
select dbo.code('648PE2075A')

You can inline it when needed as it will be faster than a UDF call. 您可以在需要时内联它,因为它比UDF调用更快。

ADDED 添加

If you have lots of rows, in-line is still the faster, but a table return udf is mostly fast and still resable 如果您有很多行,则内联仍然是更快的方法,但是表返回udf通常很快并且仍然可以重新设置

eg 例如

create function tblcode(@num varchar(20)) returns table as
   return select substring(@num, 1, 3) + '-' + substring(@num, 7, 3)
   + case when len(@num) = 10 then '-' + substring(@num, 10, 1) else '' end as code

and use it like 并像这样使用

select * 
from ( select D.* from T.ProjectNum
cross apply dbo.tblcode(T.ProjectNum)
) as xx
cross apply dbo.tblcode(xx.project)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM