0

I have two columns in SQL Server Table, one is Id and other one is Number, for example if Ids are as follows: 1189, 758, 756, I want a User defined or system defined function, that can generate numbers as: T01189, T00758, T00756.

I want to keep this function either as Default field or add it to trigger - soon after the record inserted, you can suggest me which one is better also.

1 Answer 1

2

You could use a padding trick here:

SELECT
    Id,
    'T' + RIGHT('0000' + Id, 5) AS Number
FROM yourTable;

screen capture from demo link below

Demo

Sign up to request clarification or add additional context in comments.

2 Comments

But this is giving me the correct answer maybe because of different DB or version: select 'T' + RIGHT('00000'+CAST(InspectionItemId AS VARCHAR(5)),6), InspectionItemId, InspectionItemNumber from InspectionItems where CaseId=293 order by 1 desc
Oh...I am assuming that Id is text already. If not, then yes, you would need to cast it first, as you have pointed out.

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.