Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine values from two columns into the third column

Tags:

c#

sql-server

AUTOID --- BRANCHID ---- QUTNO
1           10           "10#1"
2           11           "11#2"

AUTOID is a primary, autogenerated column. I need to fill the QUTNO column with combination of values from columns branchid and autoid. How can i do these in sql server insert statement stored procedure?

like image 254
dfgv Avatar asked Jan 20 '26 18:01

dfgv


2 Answers

How about using a computed column?

Something like

DECLARE @TABLE TABLE(
        AUTOID INT IDENTITY(1,1),
        BRANCHID INT,
        QUTNO AS CAST(BRANCHID AS VARCHAR(25)) + '#' + CAST(AUTOID AS VARCHAR(25))
)

INSERT INTO @TABLE (BRANCHID) VALUES (10),(11)

SELECT * FROM @TABLE

SQL Fiddle DEMO

like image 110
Adriaan Stander Avatar answered Jan 23 '26 08:01

Adriaan Stander


Make the column a computed column and set the computational expression so the two columns are concatenated accordingly.

like image 35
Thorsten Dittmar Avatar answered Jan 23 '26 07:01

Thorsten Dittmar