0

I want to select all texts having twice or more the string "JUSTIFICATIF DE VOYAGE" in my column raw_text of my SQL database :

I used this command : WHERE raw_text LIKE '%JUSTIFICATIF DE VOYAGE%JUSTIFICATIF DE VOYAGE%'

But this is one of the raw_text returned :

JUSTIFICATIF DE VOYAGE
Paris , le 11/07/2022
Bonjour,
Vous voudrez bien trouver ci - dessous le justificatif de voyage concernant votre commande e - billet du
11/06/2022 .
Nous attirons votre attention sur le fait que ce justificatif ne constitue en aucun cas un titre de transport .
Départ / Arrivée TGV INQUI 6687 Date / Heure
PARIS GARE DE LYON 15/06 à 16h38
Montant du voyage 56,00 EUR
Dossier voyage QORXSD
LYON PART DIEU 15/06 à 18h56
TARIF LIBERTE- Carte Libené valide à présenter . Echange et remboursement sans frais jusqu'à 30 min apres
départ . Des 30 min avant départ , billet échangeable 2 fois max ( méme jour , même trajet ) et non remboursable
après 1 échange . Siemprunt d'un autre train , échange obligatoire
Ne - bile :
Référence client 
Nº FCE
Point de vente FRXMB
SNCF vous remercie de votre confiance .
2- CLASSE
1 A sur 1 0 Page 1/1
SNCF
SNCF Voyageurs , société anonyme au capital social de 157 780 980 euros - rue Jean - Philippe Rameau , 93200 Saint - Denis - RCS Bobigny n 519 037 584

The string 'JUSTIFICATIF DE VOYAGE' (in uppercase) only appears one time, so why is it returned ?

AymDev
  • 6,626
  • 4
  • 29
  • 52
  • Using MySQL/MariaDB or an other RDBMS ? If MySQL/MariaDB, the row is returned because it is case insensitive by default. – AymDev Mar 24 '23 at 08:48
  • I am using SQLServer –  Mar 24 '23 at 08:50
  • Does this answer your question? [Is the LIKE operator case-sensitive with SQL Server?](https://stackoverflow.com/questions/14962419/is-the-like-operator-case-sensitive-with-sql-server) – AymDev Mar 24 '23 at 08:52
  • There's a typo in your text by the way: `méme jour` instead of `même jour`. – AymDev Mar 24 '23 at 08:55
  • @AymDev Yes thank you, I just check and I use insensitiv collation –  Mar 24 '23 at 09:10
  • And that is why you get the row, there is part of the tsring which looks like `dessous le justificatif de voyage concernant`, which is the second match to `JUSTIFICATIF DE VOYAGE`. If the casing is important, you need to `COLLATE` the value. – Thom A Mar 24 '23 at 09:30

2 Answers2

0

Your text does contains twice the sentence "JUSTIFICATIF DE VOYAGE"

  1. first line

  2. line 4

    DECLARE @raw_text VARCHAR(max) =

    'JUSTIFICATIF DE VOYAGE

    Paris , le 11/07/2022

    Bonjour,

    Vous voudrez bien trouver ci - dessous le justificatif de voyage concernant votre commande e - billet du

    11/06/2022 .

    Nous attirons votre attention sur le fait que ce justificatif ne constitue en aucun cas un titre de transport .

    Départ / Arrivée TGV INQUI 6687 Date / Heure

    PARIS GARE DE LYON 15/06 à 16h38

    Montant du voyage 56,00 EUR

    Dossier voyage QORXSD

    LYON PART DIEU 15/06 à 18h56

    TARIF LIBERTE- Carte Libené valide à présenter . Echange et remboursement sans frais jusqu''à 30 min apres

    départ . Des 30 min avant départ , billet échangeable 2 fois max ( méme jour , même trajet ) et non remboursable

    après 1 échange . Siemprunt d''un autre train , échange obligatoire

    Ne - bile :

    Référence client

    Nº FCE

    Point de vente FRXMB

    SNCF vous remercie de votre confiance .

    2- CLASSE

    1 A sur 1 0 Page 1/1

    SNCF

    SNCF Voyageurs , société anonyme au capital social de 157 780 980 euros - rue Jean - Philippe Rameau , 93200 Saint - Denis - RCS Bobigny n 519 037 584 '

    SELECT 1

    WHERE @raw_text LIKE '%JUSTIFICATIF DE VOYAGE%JUSTIFICATIF DE VOYAGE%'

As you see in your text copyied into a variable.

Firt occurrence is in UPPERCASE, second in lowercase.

If you want to distinguish upper from lower just use a collation with a CI parameter like :

COLLATE French_CI_AS

`SELECT 1
`WHERE @raw_text COLLATE French_CI_AS LIKE '%JUSTIFICATIF DE VOYAGE%JUSTIFICATIF DE VOYAGE%'`
SQLpro
  • 3,994
  • 1
  • 6
  • 14
0

The raw text does contain the search expression twice, in rows 1 and 4. You have to specify collation for the search to be case sensitive.

I would do it like that:

DECLARE @SearchedForExpression nvarchar(300) = 'JUSTIFICATIF DE VOYAGE' 
DECLARE @ExpectedNumberOfOccurances int = 2

select raw_text from tableName
 WHERE (LEN(raw_text collate  Latin1_General_CS_AS ) -LEN(REPLACE(raw_text collate  Latin1_General_CS_AS ,@SearchedForExpression,''))) / LEN(@SearchedForExpression) = @ExpectedNumberOfOccurances 
Wojtek
  • 11
  • 4