Home > Back-end >  create a calculated column in sql combining text and a substring of another column
create a calculated column in sql combining text and a substring of another column

Time:01-31

I have a column called TAG_ that can be any of the following

XV-123451
YV-123452
STV-123453

I want to create a calculated column that puts ZSC- plus all the characters after the hyphen.

From a previous question I have asked on here, the way to get all the characters after the hyphen is:

SUBSTRING(TAG_ ,CHARINDEX('-',TAG_ ,0) 1,LEN(TAG_ ))

How do I add to the formula above to put ZSC- in front of it?

The answers I'm looking for are as follows:

ZSC-123451
ZSC-123452
ZSC-123453

This should be easy but I don't have the time to spend in SQL like I should with my job duties.

Thanks in advance for any help.

CodePudding user response:

On SQL Server we can try:

ALTER TABLE yourTable
ADD NEW_TAG AS ('ZSC'   SUBSTRING(TAG_, CHARINDEX('-', TAG_)   1, LEN(TAG_)));

CodePudding user response:

On mysql you can do it as follows :

select Tag, CONCAT('ZSC-', SUBSTRING_INDEX(Tag,'-',-1)) AS new_tag
from table1;

demo here

  • Related