Set randomly chosen string as default value in sql column (Postgress) - postgresql

I have this column on my user table hat_color in my PostgreSQL database.
I would like to set a randomly chosen color value as the default value if no value is provided when creating the column.
So the default value should be one of ['red', 'blue', 'green']
I was thinking something like
['red','blue','green'][floor(random() * (h-l+1) + l)::int]::text
but that does does not work. Is this possible to do and if so how?

The proper syntax:
(array['red','blue','green'])[floor(random()* 3+ 1)::int]
-- or
('{red,blue,green}'::text[])[floor(random()* 3+ 1)::int]
Test it in db<>fiddle.

Related

ERROR: data type tstzrange[] has no default operator class for access method "gist" in Postgres 10

I am trying to set an index to a tstzrange[] column in PostgreSQL 10. I created the column via the pgAdmin 4 GUI, set its name and data type as tstzrange[] and set it as not null, nothing more.
I then did a CREATE EXTENSION btree_gist; for the database and it worked.
Then I saw in the documentation that I should index the range and I do:
CREATE INDEX era_ac_range_idx ON era_ac USING GIST (era_ac_range);
...but then I get:
ERROR: data type tstzrange[] has no default operator class for
access method "gist"
which, frankly, I don't know what it actually means, or how to solve it. What should I do ?
PS, that column is currently empty, has no data yet.
Ps2, This table describes chronological eras, there is an id, the era name (eg the sixties) and the timezone range (eg 1960-1969).
A date is inserted by the user and I want to check in which era it belongs.
Well, you have an array of timestamp-ranges as a single column. You can index an array with a GIN index and a range with (iirc) GIN or GiST. However, I'm not sure how an index on a column that is both would operate. I guess you could model it as an N-dimensional r-tree or some such.
I'm assuming you want to check for overlapping ranges.Could you normalise the data and have a linked table with one range in each row?

How to store different types in a column?

I am a novice programmer currently working on how to store PostgreSQL column types in a table column. Specifically, I would like to create a table with a column named 'type' that stores one of the following types: boolean, integer, text and enum. Conceptually, however, I would like a user to store which values (s)he would like to use. An integer, a piece of text, or a list of options should all be possible. See the sample table below.
id | type | default value
---------------------------------------------------------------
1 | integer | 5
2 | boolean |
3 | enum("red", "blue", "yellow") | red
The last case confuses me. With my limited knowledge I know that creating a table with a column named type with type enum("boolean", "integer", "text", "enum") is possible, but I don't see how that allows for the last case in the sample table. It seems like this only allows for a type enum, but in that case I cannot specify a default value, such as red, because red is not listed in the options of the enum type.
I am left with two questions:
On creating the table, what type do I give the column named type?
How do I store the third record in the sample table, such that I can also store a default value and such that I can still check whether the default value is actually appropriate for the chosen type?
Any answer or help is greatly appreciated. Thanks!
I think you are confusing/mixing a lot of different things.
Regarding enum type
You can declare a column of a table to be of enumerated type. For this you need to define the type:
CREATE TYPE my_color_enum AS ENUM ( 'red', 'blue', 'yellow' );
Then use it in table just like any other type:
CREATE TABLE test ( column1 my_color_enum );
If you need to assign a default value to column1 it only lets you define a default value that exists in your ENUM type, so for example while this is valid:
CREATE TABLE test ( column1 my_color_enum default 'red');
This is not and will yield an error:
CREATE TABLE test ( column1 my_color_enum default 'green' );
Because "green" value is not present in your type. The error message will look something like this (this is a loose translation, not exact error message):
Invalid input value for enum my_color_enum: "green"
Storing different datatypes in one column
This seems like a bad design, but if you really need it, and would rather avoid text datatype and then casting back and forth, you could use json, jsonb and so forth...
It will come to bite you if you decide to have a lookup table which stores data types.

Pick another column value depending on column value (postgresql)

In my frontend application I have a function that is called pick(VALUE,'col1','col2','col3'). If the VALUE is 2 the value in col2 should be picked.
This is very handsome for replacing long code using "case when", "switch case" or "if else" calculations.
I have tried to find a similar function in Postgres, but no luck so far. Seen some function array() and values() mentioned, but cannot find the correct syntax.
The goal is to set an return on of three column values depending on first column value.
Pseudo code (not working):
Select status values(column1,column2,column3)from code
I know I can do this by using "case-when-then-else-end", but I am looking for a shorter way to achieve the same thing.
Jsfiddle showing the principe. But I only want to pick ONE value depending on type:
http://sqlfiddle.com/#!15/e0b41/10
You can create an array of values from pr_* columns, then pick one of them in this way:
(array[prl_1,prl_2,prl_3])[code_type]
Here is a simple demo: http://sqlfiddle.com/#!15/e0b41/23
select *,
(array[prl_1,prl_2,prl_3])[code_type]
from code
left join prl on prl_id =1

Functions are not appearing while using TFilterRow in Talend

I am using a tFilterRow to avoid empty rows. While trying to use it I am getting only one function value 'absolute value'.
I want to filter values with a length greater than 0.
Why I am not getting any other functions?
As mentioned in the comments, the length function is only available to schema columns that have the String data type.
To filter out any rows that have a null value in a column you can use a tFilterRow but configured so that the column being checked is not equal to null like so:
In the case you are dealing with the primitive int (rather than the Integer class) then the primitive can never be null and instead defaults to 0 so you'll want to set it as not equal to 0 instead.

Create Unique Field Value by Concatenation

How can you generate a unique value for a field that matches a concatenation of certain fields and a random number
i.e.
First Name: Jim
Last Name: Jones
Field Value: jimjones0345
obviously there's a need to ensure that this value was not populated before. How would one go about this?
Assuming your using SQL Server 2005 or later...
You might try something like
update myTable
set myNewColumn = FirstName+LastName+convert(varchar,(ABS(CAST(CAST(NEWID() AS VARBINARY(5)) AS Bigint))));