Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f3PUW-0001b1-PC for pgsql-general@arkaria.postgresql.org; Tue, 03 Apr 2018 17:13:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f3PUV-0002pV-4I for pgsql-general@arkaria.postgresql.org; Tue, 03 Apr 2018 17:13:03 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f3PUU-0002pM-KJ for pgsql-general@lists.postgresql.org; Tue, 03 Apr 2018 17:13:02 +0000 Received: from mail-pl0-x236.google.com ([2607:f8b0:400e:c01::236]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f3PUQ-0004l4-1V for pgsql-general@lists.postgresql.org; Tue, 03 Apr 2018 17:13:01 +0000 Received: by mail-pl0-x236.google.com with SMTP id x4-v6so9715885pln.7 for ; Tue, 03 Apr 2018 10:12:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=illuminatedcomputing-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=a7We0S7oUsByxykGPsqsYGUNCNUE+Ds+X8YKwywXME0=; b=j42FqQSmPRvKX+zOpitpPGMfwshDnDEfrelzNX6vZFcTc+0QWra9DAFykIJxnBwmaG gSDc0MbpeUqd/WmTWdSpNPDTqBJvRrgyudOSAOpczx0peWFEWCLv25L9aTSrWIPMWwxy fwLlU6Xs2Rp6fcIqGabLW3CTtAaF6mH+Xfa1ttMU05JPhrU86tM6eC7SZ53qRqxAfcmA UMpl2IPCuY7C6Iu6vs3iby41BPBS9lVayaZ/nO+xIpQ7B7VodJjU36zqBEUNlCp4B91P eK6bTET8hlr3eo9hy9uTWcx30D3HqiKRX6Lol+/gPCWJlfcWxAAxICNxzoI7wrHk3/rB lioA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=a7We0S7oUsByxykGPsqsYGUNCNUE+Ds+X8YKwywXME0=; b=FRmIK0r1SoAl/4bXZWgrmSasuf577VhsJ1ywLiZjMpFeMjQshEQhWvnTPYgsKM5W6M qVGiV8ET+yUAUnej6ksJn5lx+CEzd2UuuCM/bF9ZqCP42BmK6DJFbLpyyQYlsPQZP0fO 7qpLipNR75KxqPCzxLl1UbcKKPkuoSUpkXvmYVHm+QCXeAmGBjXclupvL/bJrjtkMjwA qndZuCo+opwH3DeL7TTJ0ylxHAw13xGQBsAgOsNkGRiM+KygfBrkidyTphQFaJZSPUB8 g+uYE1pmGYpW/djvX44p130MrfofwQ0SJ2w2bDaVMGM3POSpFIElBBlywiTcYuDDCKnt cVDA== X-Gm-Message-State: AElRT7Hr7NCky6pGv6Am5unXkj0/20bV8ChVcw6jToNK6+iWFWu26dcd 5GTrtqyFMceNb5FoV52U2w29bf1o X-Google-Smtp-Source: AIpwx49tL/S11aumice0H0r1DE2ljSVFFrqjyJzCXLWvNcKZNjTN19iNyXjWNDEWKONoAfuzr5UDmw== X-Received: by 10.99.121.134 with SMTP id u128mr9556340pgc.360.1522775575584; Tue, 03 Apr 2018 10:12:55 -0700 (PDT) Received: from [192.168.2.201] (static-50-53-109-254.bvtn.or.frontiernet.net. [50.53.109.254]) by smtp.googlemail.com with ESMTPSA id 86sm7686322pfh.93.2018.04.03.10.12.53 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 03 Apr 2018 10:12:54 -0700 (PDT) Subject: Re: How to get an inclusive interval when using daterange To: pgsql-general@lists.postgresql.org References: <918a4ac1-2d07-c273-c809-8f0a3925bfcd@aklaver.com> From: Paul Jungwirth Message-ID: Date: Tue, 3 Apr 2018 10:12:52 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 04/03/2018 09:40 AM, hmidi slim wrote: > I tried insert into availability values ('product x', > '[2018-02-02,2018-03-01]'::daterange); and I got the same result such > as insert into availability values ('product x', daterange('2018-02-02', > '2018-03-01', '[]'). Yes, those are equivalent ways of constructing the same daterange. If you really want a closed/closed daterange, you'll need to create your own type. I don't really recommend that, but you can do it. (Using close/open is the standard because it's so convenient for combining/comparing ranges.) It's easy to create a type without a canonical function, e.g.: CREATE FUNCTION date_minus(date1 date, date2 date) RETURNS float AS $$ SELECT cast(date1 - date2 as float); $$ LANGUAGE sql immutable; CREATE TYPE daterange2 AS range (subtype = date, subtype_diff = date_minus); Then you can say: SELECT daterange2('2018-01-01', '2018-03-01', '[]'); This is not great though, because without a canonical function Postgres doesn't know that [x,y] is equal to [x,y+1). If you prefer to have a canonical function, you'll need to write one in C. (It's easy but you won't be able to install it on a managed service like AWS RDS.) It might help to read these and the code they link to (The second one is by me.): https://stackoverflow.com/questions/29895077/how-to-create-a-new-date-range-type-with-included-upper-bound-in-postgres https://illuminatedcomputing.com/posts/2016/06/inet-range/ -- Paul ~{:-) pj@illuminatedcomputing.com