Search code examples
sqlpostgresqlstring-constant

How can you return the result when facing values such as "Cote d'Ivoire" in SQL?



I made a PostgreSQL Table containing data from Oxford University Government Respond Tracker in handling COVID-19.
If anyone interested, here is the link to the data source.

You can download either JSON or CSV; in my case I am using the CSV format.
The file have the following columns:

CountryName
CountryCode
Date    
C1_School closing   
C1_Flag 
C2_Workplace closing    
C2_Flag 
C3_Cancel public events 
C3_Flag 
C4_Restrictions on gatherings   
C4_Flag C5_Close public transport   
C5_Flag 
C6_Stay at home requirements    
C6_Flag 
C7_Restrictions on internal movement    
C7_Flag 
C8_International travel controls    
E1_Income support   
E1_Flag 
E2_Debt/contract relief 
E3_Fiscal measures  
E4_International support    
H1_Public information campaigns 
H1_Flag 
H2_Testing policy   
H3_Contact tracing  
H4_Emergency investment in healthcare   
H5_Investment in vaccines   
M1_Wildcard 
ConfirmedCases  
ConfirmedDeaths 
StringencyIndex 
StringencyIndexForDisplay   
StringencyLegacyIndex   
StringencyLegacyIndexForDisplay 
GovernmentResponseIndex 
GovernmentResponseIndexForDisplay   
ContainmentHealthIndex  
ContainmentHealthIndexForDisplay    
EconomicSupportIndex    
EconomicSupportIndexForDisplay

Back to the question, so I want to create a simple SQL query such as this one

SELECT * FROM govt_respond_tracker
WHERE countryname in ('list_of_african_countries')
Order BY countryname ASC,
datec ASC;

The idea of this query is to call all of the African countries available in the file.
However, the problem arises when I am about to call "Cote d'Ivoire" (Ivory Coast).
The single quotation mark causes an error that prevents returning the result that I want.

This result that I want is just SQL output that includes the "Cote d'Ivoire" as its value in the SQL Output.

So, my question what's my best approach to this issue?

Many thanks for your time!


Solution

  • You can replace single quote to double single quote like '' e.g. 'Cote d''Ivoire' and the other is you can use (E'\\') e.g. E'Cote d\\'Ivoire' to escape single quote

    I think your best bet is hardcoding the list

    SELECT * FROM govt_respond_tracker
    WHERE countryname in ('country1', 'country2', 'Cote d''Ivoire')
    Order BY countryname ASC,
    datec ASC;