Informix: Select null problem

寵の児 提交于 2019-11-29 15:13:40

This page says the reason you can't do that is because "NULL" doesn't have a type. So, the workaround is to create a sproc that simply returns NULL in the type you want.

That sounds like a pretty bad solution to me though. Maybe you could create a variable in your script, set it to null, then select that variable instead? Something like this:

DEFINE dummy INT;
LET dummy = NULL;

SELECT group_ser, item_ser, dummy
FROM sometable

You don't have to write a stored procedure; you simply have to tell IDS what type the NULL is. Assuming you are not using IDS 7.31 (which does not support any cast notation), you can write:

SELECT NULL::INTEGER FROM dual;

SELECT CAST(NULL AS INTEGER) FROM dual;

And, if you don't have dual as a table (you probably don't), you can do one of a few things:

CREATE SYNONYM dual FOR sysmaster:"informix".sysdual;

The 'sysdual' table was added relatively recently (IDS 11.10, IIRC), so if you are using an older version, it won't exist. The following works with any version of IDS - it's what I use.

-- @(#)$Id: dual.sql,v 2.1 2004/11/01 18:16:32 jleffler Exp $
-- Create table DUAL - structurally equivalent to Oracle's similarly named table.
-- It contains one row of data.

CREATE TABLE dual
(
    dummy CHAR(1) DEFAULT 'x' NOT NULL CHECK (dummy = 'x') PRIMARY KEY
) EXTENT SIZE 8 NEXT SIZE 8;
INSERT INTO dual VALUES('x');

REVOKE ALL ON dual FROM PUBLIC;
GRANT SELECT ON dual TO PUBLIC;

Idiomatically, if you are going to SELECT from Systables to get a single row, you should include 'WHERE tabid = 1'; this is the entry for Systables itself, and if it is missing, the fact that your SELECT statement does return any data is the least of your troubles. (I've never seen that as an error, though.)

unrulylogic
SELECT group_ser, item_ser, replace(null,null) as my_null_column
FROM sometable

or you can use nvl(null,null) to return a null for your select statement.

Is there any reason to go for an actual table? I have been using

select blah from table(set{1})
hairysnaek
select blah from table(set{1})

is nice when you are using 10.x database. This statement doesn't touch database. The amount of read/write operations is equal to 0,

but

when you're using 11.x it will cost you at least 4500 buffer reads because this version of Informix creates this table in memory and executes query against it.

pbl
select to_date(null) from table;

This works when I want to get a date with null value

You can use this expression (''+1) on the SELECT list, instead of null keyword. It evaluates to NULL value of type DECIMAL(2,0).

This (''+1.0001) evaluates to DECIMAL(16,4). And so on.

If you want DATE type use DATE(''+1) to get null value of type DATE.

(''+1)||' ' evaluates to an empty string of type VARCHAR(1).

To obtain NULL value of type VARCHAR(1) use this expression: DATE(''+1)||' '

Works in 9.x and 11.x.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!