473,378 Members | 1,489 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Problem with select and null

Hi all

'SELECT (null * null)' returns null

'SELECT (null - 0)' returns null

'SELECT ((null * null) - 0)' gives the following error -
ERROR: operator does not exist: "char" - integer

Why does this statement give an error? I would expect it to return null.

Under some circumstances my program generates the above select statement, so this is not an academic question.

Platform is PostgreSQL 7.4.1, running on Redhat 9.

Is this a bug? Is there any workaround? My workaround for now is to 'coalesce' to zero all the values which could return null. The result is zero instead of null, which is not what I want, but I can live with it in the short term.

TIA for any suggestions.

Frank Millman

Nov 23 '05 #1
2 2622
Frank Millman wrote:
Hi all

'SELECT (null * null)' returns null

'SELECT (null - 0)' returns null

'SELECT ((null * null) - 0)' gives the following error -
ERROR: operator does not exist: "char" - integer

Why does this statement give an error? I would expect it to return null.


This does not seem to be a bug from my point of view. Postgres just doesn't
know what datatype these nulls should be. You can cast the null values to
integer (or float if you need):

SELECT ((null::integer * null::integer) - 0);
-- returns null

Best Regards,
Michael Paesold
---------------------------(end of broadcast)---------------------------
TIP 5: Have you checked our extensive FAQ?

http://www.postgresql.org/docs/faqs/FAQ.html

Nov 23 '05 #2
"Frank Millman" <fr***@chagford.com> writes:
'SELECT (null * null)' returns null
IMHO you really ought to get an error from that. It's just as type-free
as

select ('FOO' * 'bar');

which currently gives a rather silly result. The reason this happens
is that the type "char" (not to be confused with char) has arithmetic
operators, and we have an implicit cast from text to "char" so these
operators are able to suck in constructs that are probably user errors.
I have previously proposed removing these operators, which seem quite
useless anyway, but didn't get around to doing it for 8.0.
Under some circumstances my program generates the above select statement, s=
o this is not an academic question.


Figure out what type your program is expecting the null to be, and
explicitly cast it to that type. For instance

SELECT ((null::int * null::int) - 0);

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: jannordgreen | last post by:
The form 'Client Records' has this record source: SELECT ., .LastName, .FirstName, .Company, .Address, .City, ., .State, .Country, .WorkPhone, .FaxNumber, .EmailAddress, .Business, .Origin, .,...
2
by: Jonas Hansson | last post by:
Hi, I have a problem regarding NULL values in a database when I retrieve the data with the MySqlDataAdapter fill a DataSet with the returned value. Whenever the SQL statement returns data that has...
4
by: luna | last post by:
could someone explain the meaning behind :- System.IndexOutOfRangeException: firstname1 when the code is :- If objDR("firstname1") Is System.DBNull.Value Then firstname1.Text = "" Else :...
17
by: John | last post by:
Hi I have a datadapter with the following SQL; SELECT ID, Company, Status, CompanyType FROM Companies WHERE (@Status IS NULL or @Status = Status) When I try to fill like...
2
by: dansalmada | last post by:
Hi I'm using DB2 and I'm trying to pull information from a table. I've tried every function possible (IFNULL,COALESCE,CASE) to get a zero every time the column is NULL. The thing is I don't...
5
by: corepaul | last post by:
I am using Acess 2000 and Windows 2000. I have a form with a text box txLName. tStrGlobal As String is dimensioned globally. The first time I enter the text box, the Enter event with the code ...
1
by: Twobridge | last post by:
I have created a stored procedure and when i execute it in sql server 2005 it runs fine...the user can put in a parameter or none at all and the corresponding results are returned. @name...
1
by: Manikandan | last post by:
Hi, I have a table with following data Tablename:details No(varchar) Name(varchar) Updated(Datetime) 1 mm 10/10/2006 2 nn 02/12/2005 3 kk NULL I'm using executescalar to get the...
5
by: Rahul B | last post by:
Hi, Why is it that this command doesn't work in DB2. I have to create a view in which one of the columns has to be null(Don't ask me why that should always be null, because the view in Oracle...
2
AmberJain
by: AmberJain | last post by:
Hello everyone, Right now I am learning C and I'm using a BORLAND TURBO C++ 3.0 compiler to compile my programs. Well I am facing a problem with one of my program which I got as my assignment. I...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.