Spionred
Spionred

Reputation: 807

Append Field value with another field

I am trying to figure out if it's possible to have a MS SQL database field that has the value of another field in the same record appended to it.

To give it come context the dynamic field will contain an API call where I need to include the record ID. I would like the value the API field to be identical for each record and and use a reference to the ID field when when you query the table the API field contains the result of the API call.

Any ideas will be much appreciated.

Cheers, Kevin.

Upvotes: 0

Views: 159

Answers (2)

Pred
Pred

Reputation: 9042

You can create calculated columns:

ALTER TABLE YourTable ADD
    [columnName] AS ('YourStaticString' + CONVERT(VARCHAR(200), IdColumn))

Upvotes: 2

Kevin Hogg
Kevin Hogg

Reputation: 1781

Without modifying the table design, you can create a view of the table which has the additional field in it.

 CREATE VIEW dbo.MyView
 AS
      SELECT *, 'SomeText' + IdColumn AS DynamicColumn
        FROM MyTable

Note it is better to specify the columns explicitly, whereas I've used SELECT * for brevity.

Upvotes: 0

Related Questions