null values in a view
null values in a view
От:
Lauri Kajan <lauri.kajan@gmail.com>
Дата:
Hi all, How could I create a view that returns null values among all other values. Here is a sample that i want to achieve: CREATE VIEW view1 AS SELECT attribute1 as a1, null as a2 FROM table; Now the problem is that I got an warning: column "a2" has type "unknown" I know that I should define a data type for a field a2. But how? This works with other values but not with nulls: CREATE VIEW view1 AS SELECT attribute1 as a1, text null as a2, text 'test' as a3 FROM table; Thanks -Lauri Kajan
Re: null values in a view
От:
Achilleas Mantzios <achill@matrix.gatewaynet.com>
Дата:
try: CREATE OR REPLACE VIEW view1 AS SELECT name as a1, null::text as a2, 'test'::text as a3 FROM some_table; Στις Wednesday 05 October 2011 17:22:21 ο/η Lauri Kajan έγραψε: > Hi all, > > How could I create a view that returns null values among all other values. > Here is a sample that i want to achieve: > > CREATE VIEW view1 AS > SELECT > attribute1 as a1, > null as a2 > FROM > table; > > Now the problem is that I got an warning: column "a2" has type "unknown" > I know that I should define a data type for a field a2. But how? > This works with other values but not with nulls: > > CREATE VIEW view1 AS > SELECT > attribute1 as a1, > text null as a2, > text 'test' as a3 > FROM > table; > > > > Thanks > > -Lauri Kajan > -- Achilleas Mantzios
Re: null values in a view
От:
hubert depesz lubaczewski <depesz@depesz.com>
Дата:
On Wed, Oct 05, 2011 at 05:22:21PM +0300, Lauri Kajan wrote:
> Hi all,
>
> How could I create a view that returns null values among all other values.
> Here is a sample that i want to achieve:
>
> CREATE VIEW view1 AS
> SELECT
> attribute1 as a1,
> null as a2
> FROM
> table;
>
> Now the problem is that I got an warning: column "a2" has type "unknown"
> I know that I should define a data type for a field a2. But how?
using normal cast:
null::text as a2
or cast(null as text);
Best regards,
depesz
--
The best thing about modern society is how easy it is to avoid contact with it.
http://depesz.com/
Re: null values in a view
От:
Tom Lane <tgl@sss.pgh.pa.us>
Дата:
Lauri Kajan writes: > This works with other values but not with nulls: > CREATE VIEW view1 AS > SELECT > attribute1 as a1, > text null as a2, > text 'test' as a3 > FROM > table; FYI, the syntax typename 'literal' works *only* with string literals, not anything else. For any other target you have to write CAST(value AS typename) or equivalently value::typename. CAST is SQL-standard, :: is a Postgres-ism. regards, tom lane
Re: null values in a view
От:
Lauri Kajan <lauri.kajan@gmail.com>
Дата:
This worked. Thank you all! I know the casting is quite basic operation but could this be added to the CREATE VIEW documentation? Now there is only an example: CREATE VIEW vista AS SELECT text 'Hello World' AS hello; The same syntax won't work with other type as Tom wrote. -Lauri On Wed, Oct 5, 2011 at 5:39 PM, Tom Lane wrote: > Lauri Kajan writes: >> This works with other values but not with nulls: > >> CREATE VIEW view1 AS >> SELECT >> attribute1 as a1, >> text null as a2, >> text 'test' as a3 >> FROM >> table; > > FYI, the syntax typename 'literal' works *only* with string literals, > not anything else. For any other target you have to write > CAST(value AS typename) or equivalently value::typename. > CAST is SQL-standard, :: is a Postgres-ism. > > regards, tom lane >