Question

i am trying to create new column from existing column(table) in a new 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. 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. some records of projectnum are 10 character rest 9 character. Help Please

Was it helpful?

Solution

I suggest using select ... into ... from ... to create a new table from existing data in one step. For your string operations substring() seems appropriate. 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.

OTHER TIPS

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.

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.

ADDED

If you have lots of rows, in-line is still the faster, but a table return udf is mostly fast and still resable

e.g.

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)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top