question

CEO-8149 avatar image
0 Votes"
CEO-8149 asked CEO-8149 published

Can I concatenate two database table fields having integer datatype??

Blockquote

Hello, I am creating a table and I would want two particular fields to concatenate to form a 3rd field. Take for instance:

class int not null,
studentNo int unique IDENTITY(100000,1) Primary Key,
StudentCode...


Class field will be 3-digits integer.

StudentNo will be automatically generated, starting from 10,000

StudentCode (I want it to contain the class 3-digits for each student and also the studentNo. It will start with the 3-digits of the class. i.e to concatenate the class value and the studentNo value)

I try as much to explain because I want explicit suggestion.

If you are proficient with database (MySql or Microsoft Sql), and you know about this, then kindly help.

Please help with a line or lines of codes.

sql-server-general
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Your suggestion works for me. I need to write it out in full in case someone else needs it or I might need it in future. Thanks. I had wanted to share screenshots and the result but your server isn't uploading the pictures. Anyway, it's resolved.


CreateTabel StudentsData(
studentNo int identity(10000,1) primary key,
classcode int not null,
surname varchar(30)
studentCode AS CONCAT(classcode, studentNo) )




Insert into StudentsData(surname, classcode)
Values('Jame',334)
('Othniel', 387)


After executing the insert statement, here is the output:

studentNo| surname | classcode | StudentCode

10000 | James | 334| 33410000
10001 | Othniel | 387 | 38710001

0 Votes 0 ·

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered CEO-8149 published

Did you already try concat(class, studentNo)? (Assuming that class does not start with zeroes).

The column can be defined like this:

  StudentCode as concat([class], studentNo)

It will be calculated automatically.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks for your response.

Do you mean my statement should be like this for an example:


CreateTabel StudentsData(
studentNo int identity(10000,1) primary key,
classcode int not null,
surname varchar(30)
studentCode AS CONCAT(classcode, studentNo)
)



I hope for a response from you please

0 Votes 0 ·