0

What's this? The last char is a space (ascii 32), but SQL says the trimmed version is exactly the same as the not trimmed one. Moreover, it turns out that the length of the keyword corresponds to the trimmed length of the keyword and that the space comes after what SQL considers to be the last character.

Note that there are only these 143 results in a table of billions (>1,000,000,000)

select k.Keyword, len(k.Keyword) LenKeyword, trim(k.Keyword) TrimKeyword, 
    case when k.Keyword=trim(k.Keyword) then 1 else 0 end isSame,
    len(trim(k.Keyword)) LenTrimKeyword, SUBSTRING(k.Keyword,len(k.keyword)+1,1) LastCharPlus1
    , ASCII (SUBSTRING(k.Keyword,len(k.keyword)+1,1)) AsciiLastCharPlus1 
from #tk AdditionalSpaceAfterEndOfString
    inner join SE_Keywords k
    on AdditionalSpaceAfterEndOfString.Keyword=k.Keyword and AdditionalSpaceAfterEndOfString.Lang=k.Lang and AdditionalSpaceAfterEndOfString.Country=k.Country
order by k.DateAdded desc 

Keyword                                                                                                                                                LenKeyword  TrimKeyword                                                                                                                                            isSame      LenTrimKeyword LastCharPlus1 AsciiLastCharPlus1
------------------------------------------------------------------------------------------------------------------------------------------------------ ----------- ------------------------------------------------------------------------------------------------------------------------------------------------------ ----------- -------------- ------------- ------------------
bio arganöl                                                                                                                                            11          bio arganöl                                                                                                                                            1           11                           32
assurance retrait de permis                                                                                                                            27          assurance retrait de permis                                                                                                                            1           27                           32
call center, centre d'appel, algerie, alger, fran?ais, francophone, ogs, ogs, ogsolution,                                                              89          call center, centre d'appel, algerie, alger, fran?ais, francophone, ogs, ogs, ogsolution,                                                              1           89                           32
esta                                                                                                                                                   4           esta                                                                                                                                                   1           4                            32
google périsprit                                                                                                                                       16          google périsprit                                                                                                                                       1           16                           32
huizen aanbod den bosch                                                                                                                                23          huizen aanbod den bosch                                                                                                                                1           23                           32
recruiting pflege                                                                                                                                      17          recruiting pflege                                                                                                                                      1           17                           32
test by  keywords                                                                                                                                      17          test by  keywords                                                                                                                                      1           17                           32
employer branding pflege                                                                                                                               24          employer branding pflege                                                                                                                               1           24                           32
employer branding pflege                                                                                                                               24          employer branding pflege                                                                                                                               1           24                           32
lepelboom                                                                                                                                              9           lepelboom                                                                                                                                              1           9                            32
sun plaisance                                                                                                                                          13          sun plaisance                                                                                                                                          1           13                           32
keyboost 4                                                                                                                                             10          keyboost 4                                                                                                                                             1           10                           32
morocco desert tours                                                                                                                                   20          morocco desert tours                                                                                                                                   1           20                           32
vraag aanbod reclame                                                                                                                                   20          vraag aanbod reclame                                                                                                                                   1           20                           32
bedrijfskleding drukwerk                                                                                                                               24          bedrijfskleding drukwerk                                                                                                                               1           24                           32
bedrijfstuitje zeilen                                                                                                                                  21          bedrijfstuitje zeilen                                                                                                                                  1           21                           32
bmw occasion                                                                                                                                           12          bmw occasion                                                                                                                                           1           12                           32
marketing altenheim                                                                                                                                    19          marketing altenheim                                                                                                                                    1           19                           32
marketing pflegeeinrichtung                                                                                                                            27          marketing pflegeeinrichtung                                                                                                                            1           27                           32
marketing prijsbeker                                                                                                                                   20          marketing prijsbeker                                                                                                                                   1           20                           32
...                                                                                                            1           41                           32
abonnement culture paris                                                                                                                               24          abonnement culture paris                                                                                                                               1           24                           32
kommunikationsagentur pflege                                                                                                                           28          kommunikationsagentur pflege                                                                                                                           1           28                           32
personalmarketing pflege                                                                                                                               24          personalmarketing pflege                                                                                                                               1           24                           32
personalmarketing pflege                                                                                                                               24          personalmarketing pflege                                                                                                                               1           24                           32
salon de beauté pavillons-sous-bois                                                                                                                    35          salon de beauté pavillons-sous-bois                                                                                                                    1           35                           32
sophos safeguard                                                                                                                                       16          sophos safeguard                                                                                                                                       1           16                           32
bon achat spectacles fnac                                                                                                                              25          bon achat spectacles fnac                                                                                                                              1           25                           32
catering alphen-aan-den-rijn                                                                                                                           28          catering alphen-aan-den-rijn                                                                                                                           1           28                           32
drone electronics                                                                                                                                      17          drone electronics                                                                                                                                      1           17                           32
pflege marketing                                                                                                                                       16          pflege marketing                                                                                                                                       1           16                           32
pop! vinyl                                                                                                                                             10          pop! vinyl                                                                                                                                             1           10                           32
prêt sans banque                                                                                                                                       16          prêt sans banque                                                                                                                                       1           16                           32
sebastien izambard                                                                                                                                     18          sebastien izambard                                                                                                                                     1           18                           32
carte scenes et sorties                                                                                                                                23          carte scenes et sorties                                                                                                                                1           23                           32
dongen nieuws delen                                                                                                                                    19          dongen nieuws delen                                                                                                                                    1           19                           32
newerkkabels                                                                                                                                           12          newerkkabels                                                                                                                                           1           12                           32
quad and camel in marrakech                                                                                                                            27          quad and camel in marrakech                                                                                                                            1           27                           32
showroom marques de luxe                                                                                                                               24          showroom marques de luxe                                                                                                                               1           24                           32

Trimming does not help, the only thing is looking if the char after the total of the string is a space:

select Keyword, Lang, Country into #tk from se_keywords where ascii (SUBSTRING(Keyword,len(keyword)+1,1)) is not null

Gregory Liénard
  • 1,071
  • 3
  • 7
  • I would suggest that it isn't a space then. Perhaps something else, like a non-breaking space, or a line break (and carriage return) and you don't have. What is the `binary` value of the value you say isn't being trimmed? – Thom A Sep 09 '22 at 08:11
  • When I do cast (SUBSTRING(k.Keyword,len(k.keyword)+1,1) as binary) LastCharPlus1B, I get: 0x200000000000000000000000000000000000000000000000000000000000 – Gregory Liénard Sep 09 '22 at 08:17
  • Note that it is Latin1_General_BIN2 collation – Gregory Liénard Sep 09 '22 at 08:18
  • I can't replicate your problem: [db<>fiddle](https://dbfiddle.uk/2-qUa2gG) `TRIM` works fine. Can you provide a [mre]? – Thom A Sep 09 '22 at 08:26
  • I can copy it to a new table with the same results. I can move the table to a new db and create a backup of it. Would that do? – Gregory Liénard Sep 09 '22 at 08:35
  • Use DDL and DML to replicate the issue and [edit] your question with those statements. – Thom A Sep 09 '22 at 08:36
  • 2
    *"The last char is a space (ascii 32), but SQL says the trimmed version is exactly the same as the not trimmed one."* wait, i just realised, you're talking about the comparison and expecting them to be different... (lack of coffee in the morning). Even in a binary collation, trailing spaces are ignored for comparison. [db<>fiddle](https://dbfiddle.uk/Tf_FlVN7). If you want to check if the trimmed value differs, check the `DATALENGTH`. – Thom A Sep 09 '22 at 08:49
  • So it seems indeed. It is reproduceable by just adding a space to a keyword. So the only way to detect if a string has a trailing space is this query?? ascii (SUBSTRING(Keyword,len(keyword)+1,1)) is not null – Gregory Liénard Sep 09 '22 at 08:54
  • No, as I said, use `DATALENGTH`: `CASE LEN('abc ') WHEN DATALENGTH('abc ') THEN 'No Spaces' ELSE 'Trailing Spaces' END` Or, alternatively `CASE DATALENGTH('abc ') WHEN DATALENGTH(TRIM('abc ')) THEN 'No Spaces' ELSE 'Trailing Spaces' END`. – Thom A Sep 09 '22 at 08:56
  • No worries, sorry that took a "hot minute"; Coffee is starting to at least sink in now. :) – Thom A Sep 09 '22 at 08:58
  • 1
    Note that you need to multiply with 2 when using nvarchar so: CASE LEN('abc ')*2 WHEN DATALENGTH('abc ') THEN 'No Spaces' ELSE 'Trailing Spaces' END – Gregory Liénard Sep 09 '22 at 09:12

0 Answers0