Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lX3gr-00017f-4i for pgsql-admin@arkaria.postgresql.org; Thu, 15 Apr 2021 15:13:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lX3gp-0001Ay-S2 for pgsql-admin@arkaria.postgresql.org; Thu, 15 Apr 2021 15:13:55 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lX3dY-0002zN-Jk for pgsql-admin@lists.postgresql.org; Thu, 15 Apr 2021 15:10:32 +0000 Received: from mail.pscs.co.uk ([195.224.19.137]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lX3dU-0004Z6-Cz for pgsql-admin@lists.postgresql.org; Thu, 15 Apr 2021 15:10:31 +0000 Authentication-Results: mail.pscs.co.uk; spf=none; auth=pass (cram-md5) smtp.auth=pscs Received: from lmail.pscs.co.uk ([192.168.66.70]) by mail.pscs.co.uk ([10.224.19.137] running VPOP3) with ESMTPSA (TLSv1.3 TLS_AES_256_GCM_SHA384) for ; Thu, 15 Apr 2021 16:10:24 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=pscs.co.uk; q=dns/txt; s=lmail; h=Subject:To:References:From:Message-ID:Date:MIME-Version:In-Reply-To :Content-Type:Cc:Content-Transfer-Encoding:Reply-to:Sender; t=1618499095; x=1619103895; bh=uuLJk0C0hikQHvnSf4d/lsw0JhNx00PYtWVrQOSa6GY=; b=ORA54MEkmff86n9OtaUz55HYOwu18ZyETYRfmq9qZN9SFeOpe5drW2vyInvixUvg1zMPfFAu mDqNtEtEflbLHHzPlDFh9Qm6lMZMouobihtJIbr8otFtq4Xv9tTIyX4lnv9b4Uwx68kW7vLKJS vPvHzu/qW6zRsP0mqXnBEqut0= Authentication-Results: lmail.pscs.co.uk; spf=none; auth=pass (cram-md5) smtp.auth=paul Received: from [192.168.57.136] ([82.68.48.206] (82-68-48-206.dsl.in-addr.zen.co.uk)) by lmail.pscs.co.uk ([192.168.66.70] running VPOP3) with ESMTPSA (TLSv1.3 TLS_AES_256_GCM_SHA384); Thu, 15 Apr 2021 16:04:54 +0100 Subject: Re: Data Type to store Leading Zero(0) To: pgsql-admin@lists.postgresql.org References: <7bb92c536d544e818562bfe713eac363@kpn.com> From: Paul Smith Message-ID: <546d0cf6-1927-4ec9-b757-734fb58f1a0f@pscs.co.uk> Date: Thu, 15 Apr 2021 16:04:53 +0100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: <7bb92c536d544e818562bfe713eac363@kpn.com> Content-Type: multipart/alternative; boundary="------------EAC786C9AD89620B56884D7D" Content-Language: en-GB X-Authenticated-Sender: paul X-Server: VPOP3 Enterprise V8.1 - Registered to: PSCS X-Organisation: Paul Smith Computer Services X-VPOP3Tester: 12 345 X-Authenticated-Sender: pscs List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------EAC786C9AD89620B56884D7D Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 15/04/2021 14:47, soumik.bhattacharjee@kpn.com wrote: > > Thanks for your mail. > > * Boolean is not possible as this is phone number. > Phone numbers aren't numbers. They're text. Does it make sense to add two phone numbers together? No? Then they're not numbers :-) So, either store them as varchar or do something fancy. For instance, you could store them as an bigint (an integer is probably not big enough), with a smallint indicating how many preceding zeros there are - but I'd expect a varchar to be quicker, and it'd certainly be simpler. You say that 'varchar' gives a performance hit - can you explain how? Have you done some performance profiling, or have you just assumed? If the performance hit is with searching, then you could index on the phone number cast to an integer, and then do a string comparison to verify? Eg create table people (name varchar, phone varchar); create index people_phone on people(cast(phone as bigint)); select * from people where cast('07000123456' as bigint)= cast(phone as bigint) and '07000123456' = phone; this will use the 'people_phone' index, so searches will be quick. If 'bigint' indexes are too slow, then you could just index the last 9 digits and verify with a string compare: create index people_phone on people(cast(substring(phone from length(phone) - 8) as integer)); select * from people where cast('000123456' as integer) = cast(substring(phone from length(phone) - 8) as integer) and '07000123456' = phone; -- Paul Paul Smith Computer Services support@pscs.co.uk - 01484 855800 -- Paul Smith Computer Services Tel: 01484 855800 Vat No: GB 685 6987 53 Sign up for news & updates at http://www.pscs.co.uk/go/subscribe --------------EAC786C9AD89620B56884D7D Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit
On 15/04/2021 14:47, soumik.bhattacharjee@kpn.com wrote:

Thanks for your mail.

  • Boolean is not possible as this is phone number.
Phone numbers aren't numbers. They're text. Does it make sense to add two phone numbers together? No? Then they're not numbers :-)

So, either store them as varchar or do something fancy.

For instance, you could store them as an bigint (an integer is probably not big enough), with a smallint indicating how many preceding zeros there are - but I'd expect a varchar to be quicker, and it'd certainly be simpler.

You say that 'varchar' gives a performance hit - can you explain how? Have you done some performance profiling, or have you just assumed?



If the performance hit is with searching, then you could index on the phone number cast to an integer, and then do a string comparison to verify?

Eg

create table people (name varchar, phone varchar);

create index people_phone on people(cast(phone as bigint));

select * from people where cast('07000123456' as bigint)= cast(phone as bigint) and '07000123456' = phone;

this will use the 'people_phone' index, so searches will be quick.


If 'bigint' indexes are too slow, then you could just index the last 9 digits and verify with a string compare:

create index people_phone on people(cast(substring(phone from length(phone) - 8) as integer));

select * from people where cast('000123456' as integer) = cast(substring(phone from length(phone) - 8) as integer) and '07000123456' = phone;

-- 
Paul
Paul Smith Computer Services
support@pscs.co.uk - 01484 855800

--

Paul Smith Computer Services
Tel: 01484 855800
Vat No: GB 685 6987 53

--------------EAC786C9AD89620B56884D7D--