Set default value for column programmatically - postgresql

I need help to exchange the default value of column in postgres according to the value of other column.
I mean: if column B has value 1, the default value of column A has to be seq_one. If column B has value 2, the default value of column A has to be seq_two.

Related

how to generate auto increment column which will be have some pattern in postgresql?

I have a table named dept_registration it has a column dept_code so I have to make this field auto-generate but in a specific pattern.
example:- test0001
test0002
test0003
test0004
the "test" should be appended before number automatically after insertion
The column definition could be
dept_code text DEFAULT 'test' || lpad(nextval('myseq')::text, 4, '0')
where myseq is a sequence.
You will get in trouble once the counter reaches 10000...

Change the column type to 'Date' from 'Character varying' in PostgreSQL 11.0

I have a postgreSQL 11.0 table with dates in below format, but the column type 'character varying'.
id primary_completion_date
0 December2019
1 April2020
2 September2021
3 September2022
4 December2021
Is it possible to convert the column type to 'date'. When I try to convert it to date, It changes the column content to below:
id primary_completion_date
0 12-2019-01
1 04-2020-01
2 09-2021-01
3 09-2022-01
4 12-2021-01
I am using following statement in my script.
alter table tbl alter primary_completion_date type date using to_date(primary_completion_date, 'MonthYYYY')
How can I retain the column content as the input but change the column type to 'Date'.
How can I retain the column content as the input but change the column type to 'Date'*"
You can't because December2019 is not a valid value for a date column.
But you can always format the date value when you retrieve it:
select id, to_char(primary_completion_date, 'MonthYYYY') as formatted_completion_date
from the_table

How to coalesce two column with default value 0 in PostgreSQL?

I have a case when select data from PostgreSQL the table have more than 3 columns; 2 columns are a numeric type with a default value of 0 for both of them. While inserting data into Column A, it is filled with some numeric value.
So now I wanna select with the COALESCE function such that if Column B is still 0, it will return the value from Column A.
How can I do that?
Option 1: use nullif
coalesce(nullif("B", 0), "A")
Option 2: use CASE
CASE WHEN "B" = 0 THEN "A" ELSE "B" END
I'd go for option 2, even though it doesn't use coalesce as you requested.

PostgreSQL : Update column with same vaue

Following is sample table and data
create table chk_vals (vals text);
insert into chk_vals values ('1|2|4|3|9|8|34|35|38|1|37|1508|1534');
So,How to update column vals by appending integer in 4th position of the existing value(ie. 3 | is used as a seperator) into the last position along with symbol |
as you can see the existsing value if 1|2|4|3|9|8|34|35|38|1|37|1508|1534 and the output should be 1|2|4|3|9|8|34|35|38|1|37|1508|1534|3
Use PostgreSQL's split_part() to splits the field and find the value at position 4
select split_part(vals,'|',4) val from chk_vals
this will return value 3
update chk_vals
set vals=vals||format('|%s',(select split_part(vals,'|',4) val from chk_vals))
Format()

How to insert DB Default values under EF?

when adding a new record like this
ContentContacts c2 = new ContentContacts();
c2.updated_user = c2.created_user = loggedUserId;
c2.created_date = c2.updated_date = DateTime.UtcNow;
db.ContentContacts.AddObject(c2);
I'm getting
Cannot insert the value NULL into column 'main_email_support', table 'SQL2008R2.dbo.ContentContacts'; column does not allow nulls. INSERT fails. The statement has been terminated.
but the default value in the database is an empty string like:
why am I getting such error? shouldn't the EF says something like:
"ohh, it's a nullvalue, so let's add the column default value instead"
I did a small test, created a table with a column that had a default value but did not allow nulls.
Then this SQL Statement:
INSERT INTO [Test].[dbo].[Table_1]
([TestText])
VALUES
(null)
Gives this error:
Msg 515, Level 16, State 2, Line 1
Cannot insert the value NULL into column 'TestText', table
'Test.dbo.Table_1'; column does not allow nulls. INSERT fails.
The problem here is that the insert specifies all the columns, also those with default values. The the Insert tries to update the columns with null values.
You have 2 options:
Update the table through a view, which does not contain the default columns
Set the default values in your C# code
A default value is business logic, so there is a case for it being set in the business layer of your application.