C
C
cicatrix2018-05-08 11:54:54
SQL Server
cicatrix, 2018-05-08 11:54:54

How to create a calculated field by the value of another field in MS-SQL?

There is a pallets table, it has a shipmentid field.
It is necessary to add a calculated field shipped with a dimension of bits, where the value would be 1 when shipmentid is not null, and 0 when, respectively, null.
How to register it in alter table?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin Tsvetkov, 2018-05-08
@cicatrix

CREATE TABLE [dbo].[pallets](
  [shipmentid] [int] NULL,
  [shipped]  AS (CONVERT([bit],case when [shipmentid] IS NULL then (0) else (1) end))
) ON [PRIMARY]

With correction from d-stream and SSMS.
Or another option:
[shipped] AS (CONVERT([bit],isnull([shipmentid],(0))))

K
kisaa, 2018-05-08
@kisaa

Trigger on insert/update?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question