Failed to set calculated column, not Null

I'm having trouble setting the computed column as not null .

I want to reach C001 , C002... etc. and at the same time set it as not null .

I read on the forum that this can be achieved using the default value of 0 for NULL values.
For example, ISNULL(Price + Taxes, 0)

I tried to apply this formula:

 ('C'+right('000'+CONVERT([varchar](3),[ID],(0)),(3))) 

But that did not work. Can someone tell me what I am missing?

 ALTER CreditCard accountNo AS ISNULL('C'+right('000'+CONVERT([varchar](3),[idCreditCard],(0)),(3)),0) 
+6
source share
2 answers

I finally found a solution to my problem!

The correct request should be:

 ALTER TABLE CreditCard ADD accountNo AS ISNULL('C'+right('000'+CONVERT([varchar](3),[idCreditCard],(0)),(3)),0) 

Thanks for the help guys!

+6
source

If this applies to SQL Server, you may be interested in this from MSDN.

'NOT NULL can be specified for computed columns only if PERSISTED is specified.' http://msdn.microsoft.com/en-us/library/ms190273.aspx

... but trying to reverse engineer the question from the answer, I think that "keenlearner only wanted to make sure that the column never had a null value without restriction.

+3
source

Source: https://habr.com/ru/post/918636/


All Articles