Received: from dc-mx01.cluster1.charter.net (dc-mx01.cluster0.hsacorp.net [209.225.8.11]) by postgresql.org (8.11.3/8.11.4) with ESMTP id fBL5L0m94030 for ; Fri, 21 Dec 2001 00:21:01 -0500 (EST) (envelope-from billc@ballydev.com) Received: from [24.205.197.19] (HELO ballydev.com) by dc-mx01.cluster1.charter.net (CommuniGate Pro SMTP 3.4.6) with ESMTP id 57443366; Fri, 21 Dec 2001 00:28:44 -0500 Message-ID: <3C22C6B8.3020806@ballydev.com> Date: Thu, 20 Dec 2001 21:20:56 -0800 From: Bill Cunningham User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:0.9.4) Gecko/20011128 Netscape6/6.2.1 X-Accept-Language: en-us MIME-Version: 1.0 To: "Bob Smith, Hammett & Edison, Inc." CC: pgsql-admin@postgresql.org Subject: Re: Odd behavior with NULL value References: <000001411913091751151@h-e.com> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Archive-Number: 200112/151 X-Sequence-Number: 2874 I apologize if this is sent twice. The simplest solution would be to use a view over the data hiding the mess of the case statement so that ad-hoc users' queries would still work. - Bill Bob Smith, Hammett & Edison, Inc. wrote: >On Thu, Dec 20, 2001, 18:55:18 Tom Lane wrote: > >>bsmith@h-e.com (Bob Smith, Hammett & Edison, Inc.) writes: >> >>>On a related note, does anyone know if 'current' works with DATE? >>> >>DATE does not have an internal representation of 'current', so the DATE >>input parser treats it the same as 'now'. AFAIR only timestamp (nee >>datetime) has that concept. >> >>FYI, the concept of 'current' has been deemed broken and removed >>altogether for 7.2. See discussions in the pgsql-hackers archives >>if you want to know why. >> >> regards, tom lane >> > >Here is the problem I'm trying to solve, perhaps someone can help. For an >invoicing system database, I have a table that defines employees. Each has a >begin and end date defining the employment period. For current employees, the >end date is "open". How do I express that to keep queries as simple as >possible? The three choices I came up with are: > > (1) Use end_date = 'current' > > (2) Store end_date as TEXT and cast it to DATE in expressions, so it can > contain the text constant 'today' for current employees > > (3) Use end_date = NULL and have an extra expression in the queries > >Because 'current' doesn't work for DATE types, (1) is a bust. (2) and (3) >both work, but I'm not sure which is better from a performance point of view. > For example, if I want all the employees that are current as of a particular >date, for (2) it would be: > > SELECT * FROM employee WHERE (foo >= employee.start_date) AND > (foo <= employee.end_date::DATE) > >and for (3) it would be: > > SELECT... WHERE (foo >= employee.start_date) AND > ((foo <= employee.end_date) OR (employee.end_date IS NULL)) > >(Thanks to all who posted with explanations of why (x IS NULL) should be used >instead of (x = NULL)). The cast has a performance penalty, but then so does >using OR, especially in a join. Which would be worse? > >I just noticed that (3) does have one advantage over (2); if the system allows >end_date to be set into the future, (3) works for dates in the future, but (2) >does not. But that isn't one of my requirements so it isn't a deciding >factor. > >Any opinions on which approach is better, or does anyone see a fourth >alternative? > >Thanks! > > > |\ _,,,---,,_ Bob Smith > /,`.-'`' -. ;-;;,_ Hammett & Edison, Inc. > |,4- ) )-,_. ,\ ( `'-' bsmith@h-e.com > '---''(_/--' `-'\_) > >---------------------------(end of broadcast)--------------------------- >TIP 2: you can get off all lists at once with the unregister command > (send "unregister YourEmailAddressHere" to majordomo@postgresql.org) >