Having trouble setting a computed column as not Null - sql

I'm having problems setting a computed column as not null.
What I want to achieve is C001,C002..., etc. and at the same time set it as not null.
I have read on a forum that this can be achieved by using the default value 0 for NULL values.
E.g., ISNULL(Price + Taxes, 0)
I have tried to apply to this formula:
('C'+right('000'+CONVERT([varchar](3),[ID],(0)),(3)))
But it didn't seem to work. Can anyone tell me what am I missing?
ALTER CreditCard accountNo AS ISNULL('C'+right('000'+CONVERT([varchar](3),[idCreditCard],(0)),(3)),0)

I've finally found the solution to my problem!
The correct query should be:
ALTER TABLE CreditCard ADD accountNo AS ISNULL('C'+right('000'+CONVERT([varchar](3),[idCreditCard],(0)),(3)),0)
Thanks for the help guys!

If this relates to SQL Server, you might be interested in this from MSDN.
'NOT NULL can be specified for computed columns only if PERSISTED is also specified.'
http://msdn.microsoft.com/en-us/library/ms190273.aspx
... but after trying to reverse-engineering the question from the answer, I think that 'keenlearner wanted only to ensure that there was never a null value in the column without having the constraint.

Related

How can I use regexp (or equivalent) in MS SQL Server to solve this problem?

I have searched online and in SO to try and find a solution to my problem but I can't find anyone that has solved this.
When creating a table in MS SQL Server, I have a column called composite_name. I want to add a constraint to this column so that it only accepts values of the form:
COMPOSITE1, COMPOSITE2, ..., COMPOSITE[x], ...
where [x] is any integer.
I have tried to do this by adding the below constraint to my table creation statement:
CONSTRAINT [check_composite_name] CHECK (composite_name LIKE 'COMPOSITE[0-9]+')
the table creates, but then when I insert data that has the form mentioned above I get an error. Clearly something is wrong with the string pattern but I'm not sure how to fix this.
I stress that I am using Microsoft SQL Server so there are no functions like REGEXP_LIKE. I thought I had followed the documentation correctly on this given here.
Any help would be greatly appreciated. Thanks in advance
You can use like and not like:
check (composite like 'COMPOSITE[0-9]%' and
composite not like 'COMPOSITE%[^0-9]%'
)
Note: This may also match lower case, depending on the collation of the column.

PostgreSQL extract keys from jsonb, exception "cannot call jsonb_object_keys on a scalar"

I am trying to get my head around with jsonb in Postgres. There are quite a few issues here, What I wanted to do was something like:
SELECT table.column->>'key_1' as a FROM "table"
I tried with -> and also some combinations of brackets as well, but I was always getting nil in a.
So I tried to get all keys first to see if it is even recognizing jsonb or not.
SELECT jsonb_object_keys(table.column) as a FROM "table"
This threw an error:
cannot call jsonb_object_keys on a scalar
So, to check the column type(which I created, so I know it IS jsonb, but anyway)
SELECT pg_typeof(column) as a FROM "table" ORDER BY "table"."id" ASC LIMIT 1
This correctly gave me "jsonb" in the result.
values in the column are similar to {"key_1":"New York","key_2":"Value of key","key_3":"United States"}
So, I am really confused on what actually is going on here and why is it calling my json data to be scalar? What does it actually means and how to solve this problem?
Any help in this regard will be greatly helpful.
PS: I am using rails, posted this as a general question for the problem. Any rails specific solution would also work.
So the issue turned out to be OTHER than only SQL.
As I mentioned I am using rails(5.1), I had used default value '{}' for the jsonb column. And I was using a two-way serializer for the column by defining it in my model for the table.
Removing this serializer and adjusting the default value to {} actually solved the problem.
I think my serializer was doing something to the values, but still, in the database, it had correct value like i mentioned in the question.
It is still not 100% clear to me what was the problem. But it is solved anyway. If anyone can shed some light on what exactly the problem was, that will be great.
Hope this might help someone.
In my case the ORM layer somehow managed to wrote a null string into the JSON column and Postgres was happy with it. Trying to execute json_object_keys on such value resulted in the OP error.
I have managed to track down the place that allow such null strings and after fixing the code, I have also fixed the data with the following query:
UPDATE tbl SET col = '{}'::jsonb WHERE jsonb_typeof(col) <> 'object';
If you intentionally mix the types stored in the column (e.g. sometimes it is an object, sometimes array etc), you might want to filter out all rows that don't contain objects with a simple WHERE:
SELECT jsonb_object_keys(tbl.col) as a FROM tbl WHERE jsonb_typeof(col) = 'object';

add column in ORACLE with DEFAULT value and NULLABLE column type

We recently had our deployments crash because of a very strange reason.
alter table eugen add eugencolumn number default 99;
Is not mentioning the column as 'NOT NULL' making such a big difference? I want to understand why this query is creating problems, adding a 'NOT NULL' constraint solves it but is there any other work around? if you do not want the column to not be null?
Thanks,
Much Appreciated.
Actually your Question is not clear .If you want to set 99 as default value after modifying table structure execute the query.
update eugen set eugencolumn = 99;

Change data greater than a given date to NULL in one column. Getting key error

I have a column filled with dates, some of which are duplicates, and I want to change all the ones greater than 2012-05-28 to NULL. Here is the statement I'm using:
UPDATE my_data SET date_firstnewtumor=NULL
WHERE date_firstnewtumor>2012-05-28;
However, MySQL Workbench is giving me this error message: "...you tried to update a table without a WHERE that uses a key column."
I then tried the above code in MySQL Command Line Client and it changed the entire column date_firstnewtumor to NULL.
I've looked at examples of code online and I could swear their WHERE statement was similar to mine. What am I doing wrong?
Thank you for your advice!
I think you'll just want to specify the date literal correctly:
WHERE date_firstnewtumor > '2012-05-28';
Or simply:
WHERE date_firstnewtumor > 20120528;

How you get a list of updated columns in SQL server trigger?

I want to know what columns where updated during update operation on a triger on first scaaning books online it looks like COLUMNS_UPDATED is the perfect solution but this function actualy don't check if values has changed , it check only what columns where selected in update clause, any one has other suggestions ?
The only way you can check if the values have changed is to compare the values in the DELETED and INSERTED virtual tables within the trigger. SQL doesn't check the existing value before updating to the new one, it will happily write a new identical value over the top - in other words, it takes your word for the update and tracks the update rather than actual changes.
We can use Update function to find if a particular column is updated:
IF UPDATE(ColumnName)
Refer to this link for details: http://msdn.microsoft.com/en-us/library/ms187326.aspx
As the others have posted, you'll need to interrogate INSERTED and DELETED. The only other useful bit of advice might be that you can get only the rows that have changed values (and discard the rows that didn't change) by using the EXCEPT operator - like this:
SELECT * FROM Inserted
EXCEPT
SELECT * FROM Deleted
The only way I can think of is that you can compare the values in DELETED and INSERTED to see which columns have changed.
Doesn't seem a particularly elegant solution though.
I asked this same question!
The previous posters are correct -- without directly comparing the values, you can't tell for sure whether the data has actually changed or not. However, there are several ways to do this type of checking, depending on what else you're trying to do in the trigger. My question has some good advice in the answers about those different mechanisms and their tradeoffs.