Discussion Locked

How to handle ' in a name

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I am working on a system the will be needing to make database calls using last name as a search parameter. I have been told that it is not possible to handle a ' in a name reliably (like O'Sullivan.) Can someone give me coding examples that will work in Java and Javascript? I have done this in other environments by using two ', to indicate that the ' should be handled as a character and not a delimiter.

7 replies

by jay eckles

You could also use \ as an escape character in Java and Javascript.

//Java example:
String str = new String( "O\'Sullivan" ) ;

//Javascript example:
var str = 'O\'Sullivan' ;

Your DBMS may also require you to use a specific escape character when performing transactions like insert or update:
update names_tbl set last_name='O\'Sullivan' where ssn='123-45-6789'

Good luck.

Jay

by nvjain73

I agree to what is given in Answer 2 by Jay except that for saving into the database you should use the PreparedStatment class. This class will be able to handle the ' in the strings that are passed to the query.

This discussion is closed to new replies.