Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Its kinda complicated but I try to explain it ..I have a table in database somewhere looks like this. this is partno table
... |   Part No     | Convert | ...
... |    00000      |  ABC    | ...
... |    00001      |  DEF    | ...

All the input value must be start with 00000 or 00001, For example = 0000012345, 000014321.

Then, based on these first 5 value(00000 and 00001), it will be convert into like the Convert column.

Example:
====================
Input: 0000012345
Expected: ABC12345
====================
Input: 000014321
Expected: DEF4321
====================
Meaning that I must convert the input that I got based on the table above and I don't know how to do that.

What I have tried:

So, I cheated and make the query like this:

Insert into pencil (pencil_no,lastdate) 
values('ABC'|| SUBSTR('" + pen + "',1,2), sysdate)

//pen is the input value


and it works as I get my desired output. But unfortunately this is not the right way as the data in the table will be updated and change.

Can anyone guide me how to do this? Thanks in advance
Posted
Updated 21-Nov-19 22:11pm

1 solution

Create a computed column: Specify Computed Columns in a Table - SQL Server | Microsoft Docs[^]
That can use the other information in the row to generate the column value on the fly when you need to use it; it isn't stored so INSERT and UPDATE operations don't mess it up.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900