0

Need to create new column from the existing column with string manipulation on the fly

Existing stored string in the column is nvarchar

Existing column string example value 0001134564444

Need, the first three positions '000' to become 'AB' (there is a good number of variations)

the following 11 shall be 1 (there are eight variations results (1 to 8))

the following 3456 shall take the last two characters 56

the last 4444 shall not change.

The new string should then be AB5614444

Now need this done in SQL server,...

have tried with, substring, stuff, replace, charindex Have reached the point...never too late to give up, this is my last chance.

Oracle version,

select column NewColumn, decode(substr(column, 1,3),'000','AB', '010', 'BC' ...)||substr(column,8,2)
||substr(column,5,1)||substr(column,10,5) NewStringColumn from table

getting absolutely nowhere with SQL server for this task, oracle remains no longer an option. any help is appreciated,

2
  • 1
    Please add sample input and output data. Commented Jun 7, 2020 at 8:02
  • 1
    Shouldn't the result be AB1564444? Commented Jun 7, 2020 at 8:12

1 Answer 1

1
select
  case substring(c, 1, 3) when '000' then 'AB' when '010' then 'BC' ... end
  +
  case substring(c, 4, 2) when '11' then '1' ... end
  +
  substring(c, 8, 6)
with first_mapping as (
  select k, v
  from (values
    ('000', 'AB'),
    ('010', 'BC'),
    ...
  ) v(k, v)
),
second_mapping as (
  select k, v
  from (values
    ('11', '1'),
    ...
  ) v(k, v)
)
select
  (select v from first_mapping where k = substring(c, 1, 3))
  +
  (select v from second_mapping where k = substring(c, 4, 2))
  +
  substring(c, 8, 6)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.