Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am Using the case function to rename the character value in a column. But, I have a word as Jo'Burg in one column and in other column I have as Johannesburg. I tried to rename Johannesburg to Jo'Burg i am getting error.


Can anyone help me ?

What I have tried:

 CASE
WHEN AD.[Site] ='Johannesburg' THEN 'Jo'Burg'
Posted
Updated 11-Aug-21 4:05am
Comments
Richard MacCutchan 11-Aug-21 9:24am    
"i am getting error"
What error?
Member 15309910 11-Aug-21 9:32am    
An expression of non boolean type specified in a context where a condition is expected

Try
SQL
'Jo''Burg'
 
Share this answer
 
Comments
Member 15309910 11-Aug-21 10:06am    
I have mentioned in my question, I have Johannesburg need to convert the name as Jo'Burg.
I need to have the ' between Jo and Burg...
Hope you can help me with that....
I tried 'Jo'Burg' It shows the error I mentioned above
As you may be able to tell from the mangled code highlighting, the string 'Jo'Burg' is not valid. SQL interprets that as the string 'Jo', followed by the unknown operator Burg, and finally an unterminated string '.

To include a single quote in a SQL string, you need to escape it:
SQL
CASE
    WHEN AD.[Site] = 'Johannesburg' THEN 'Jo''Burg'
 
Share this answer
 
Comments
Member 15309910 11-Aug-21 10:05am    
I have mentioned in my question, I have Johannesburg need to convert the name as Jo'Burg.
I need to have the ' between Jo and Burg...
Hope you can help me with that....
I tried 'Jo'Burg' It shows the error I mentioned above
Richard Deeming 11-Aug-21 10:07am    
And as all three solutions have explained, you need to escape the ' in Jo'Burg for SQL to treat it as a valid string.

Use 'Jo''Burg'', and the column will be updated to Jo'Burg.
Member 15309910 11-Aug-21 10:12am    
Still it is not working i am using SSMS, by doing as you mentioned 'Jo''Burg'' the queries below it all getting selected
Richard Deeming 11-Aug-21 10:15am    
You're not paying attention!

Escape the single quote within the string. DO NOT escape the quote at the end of the string.

'Jo''Burg' - ONE single quote, followed by Jo, followed by TWO single quotes, followed by Burg, followed by ONE single quote.
You haven't provided much to go on, but one obvious error is mismatched single quotes:
SQL
THEN 'Jo'Burg'

Single quotes denote the start and end of strings, and you've got a single quote in your string Jo'Burg. Your THEN clause is actually this:
SQL
THEN 'Jo'

and everything after that is unintelligible garbage to SQL.

To escape the single quote as part of the string, you have to double up the single quote character:
SQL
THEN 'Jo''Burg'
 
Share this answer
 
Comments
Member 15309910 11-Aug-21 10:06am    
I have mentioned in my question, I have Johannesburg need to convert the name as Jo'Burg.
I need to have the ' between Jo and Burg...
Hope you can help me with that....
I tried 'Jo'Burg' It shows the error I mentioned above
Dave Kreskowiak 11-Aug-21 10:43am    
You apparently haven't read my post at all. I already told you how to fix it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900