Re: The same result for with SPACE and without SPACE - Mailing list pgsql-admin

From Paul Smith*
Subject Re: The same result for with SPACE and without SPACE
Date
Msg-id fbae23da-7761-161d-b484-6c38e2522127@pscs.co.uk
Whole thread Raw
In response to The same result for with SPACE and without SPACE  ("Wetmore, Matthew (CTR)" <Matthew.Wetmore@express-scripts.com>)
Responses Re: The same result for with SPACE and without SPACE
List pgsql-admin
On 15/06/2023 16:21, Wetmore, Matthew (CTR) wrote:
@font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4;}@font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4;}@font-face {font-family:"Helvetica Neue";}p.MsoNormal, li.MsoNormal, div.MsoNormal {margin:0in; margin-bottom:.0001pt; font-size:12.0pt; font-family:"Times New Roman",serif;}a:link, span.MsoHyperlink {mso-style-priority:99; color:blue; text-decoration:underline;}a:visited, span.MsoHyperlinkFollowed {mso-style-priority:99; color:purple; text-decoration:underline;}p.msonormal0, li.msonormal0, div.msonormal0 {mso-style-name:msonormal; mso-margin-top-alt:auto; margin-right:0in; mso-margin-bottom-alt:auto; margin-left:0in; font-size:12.0pt; font-family:"Times New Roman",serif;}span.EmailStyle18 {mso-style-type:personal-reply; font-family:"Calibri",sans-serif; color:#1F497D;}.MsoChpDefault {mso-style-type:export-only; font-size:10.0pt;}div.WordSection1 {page:WordSection1;}

Before you kick me out of the group, can you please explain.

 

I thought the orig issue was that purposefully spaces/whitespace are being ignored (or not ignored.) in the select.  Maybe there was an email in the middle that I missed

 

create table matt_test (c1 int)

 

insert into matt_test  values ('123')

 

insert into matt_test  values (' 123')

 

insert into matt_test values ('123 ')

 

select c1 from matt_test where c1 = '123'

-- all 3 rows returned.

 

Is it expected behavior that all 3 rows would be returned (because the space isn’t an INT?)

 

Yes, that's totally expected behaviour. The "problem" is that it's pretty much obvious behaviour as well.

Your table is defined to store numbers not text.

So, when you do

     insert into matt_test  values ('123'); -- with any combination of leading/trailing spaces

Postgresql converts it to

    insert into matt_test values(123)

So, all three inserts you did are actually the same, and all store the *NUMBER* 123 in the table. Spaces are not part of the number, so are not stored

When you make the table store 'TEXT' or VARCHAR fields, then spaces ARE relevant for that type, so the data stored is different. For CHAR fields, they are space-padded or truncated as necessary to be the defined field size.

This is all pretty much basic SQL behaviour. Any correctly implemented SQL database server will behave exactly the same.


Paul

pgsql-admin by date:

Previous
From: "Wetmore, Matthew (CTR)"
Date:
Subject: The same result for with SPACE and without SPACE
Next
From: "David G. Johnston"
Date:
Subject: Re: The same result for with SPACE and without SPACE