Skip to content
Fix Code Error

How to insert a value that contains an apostrophe (single quote)?

March 13, 2021 by Code Error
Posted By: Anonymous

What is the correct SQL syntax to insert a value with an apostrophe in it?

Insert into Person
  (First, Last)
Values
  'Joe',
  'O'Brien'

I keep getting an error as I think the apostrophe after the O is the ending tag for the value.

Solution

Escape the apostrophe (i.e. double-up the single quote character) in your SQL:

INSERT INTO Person
    (First, Last)
VALUES
    ('Joe', 'O''Brien')
              /
          right here  

The same applies to SELECT queries:

SELECT First, Last FROM Person WHERE Last = 'O''Brien'

The apostrophe, or single quote, is a special character in SQL that specifies the beginning and end of string data. This means that to use it as part of your literal string data you need to escape the special character. With a single quote this is typically accomplished by doubling your quote. (Two single quote characters, not double-quote instead of a single quote.)

Note: You should only ever worry about this issue when you manually edit data via a raw SQL interface since writing queries outside of development and testing should be a rare occurrence. In code there are techniques and frameworks (depending on your stack) that take care of escaping special characters, SQL injection, etc.

Answered By: Anonymous

Related Articles

  • Reference - What does this regex mean?
  • How do I include certain conditions in SQL Count
  • I do not use TBB, but I get linker errors related to TBB
  • How to create a column with a count of rows between…
  • insert tables in dataframe with years from 2000 to…
  • What are the undocumented features and limitations…
  • PHP parse/syntax errors; and how to solve them
  • 'foo' was not declared in this scope c++
  • What is The Rule of Three?
  • Ukkonen's suffix tree algorithm in plain English
  • Create User Profile Piece
  • Why are elementwise additions much faster in…
  • How many times the loss function is triggered from…
  • How to perform a conditional groupby calculation in…
  • error LNK2005: ✘✘✘ already defined in…
  • transition in svelte is working only in the first time
  • How can I parse a CSV string with JavaScript, which…
  • python 3.2 UnicodeEncodeError: 'charmap' codec can't…
  • “tag already exists in the remote" error after…
  • Output not incrementing correctly - C++
  • Maximum XOR With an Element From Array | Leetcode
  • How can I fix MySQL error #1064?
  • how do I replace a static array with a dynamic one?
  • loop and eliminate unwanted lines with beautiful soup
  • SQL query return data from multiple tables
  • What can we use in Select Query instead of cursor
  • Database development mistakes made by application developers
  • What is a NullReferenceException, and how do I fix it?
  • show all tags in git log
  • Escaping single quote in PHP when inserting into MySQL
  • CUDA: Pass device function as an argument to global function
  • data.table vs dplyr: can one do something well the…
  • How to horizontally align ul to center of div?
  • How can I get a List from some class properties with…
  • Vue - Deep watching an array of objects and…
  • Java error - "invalid method declaration; return…
  • Smart way to truncate long strings
  • Multidimensional Array [][] vs [,]
  • Program.Mattor(): not all code paths return a value.…
  • store struct of info in single linked list
  • How to count input whenever the user input something…
  • Is there a way to make published filter a 'safeFilter'
  • What is attr_accessor in Ruby?
  • HashMap with multiple values under the same key
  • What's the difference between including files with…
  • XML Serialize generic list of serializable objects
  • How to run server-sent events in svelte component in sapper
  • Extend Aurelia Validation Rules on a per class basis
  • Copy a file in a sane, safe and efficient way
  • How to use Regular Expressions (Regex) in Microsoft…
  • How do I measure execution time of a command on the…
  • jQuery.parseJSON throws “Invalid JSON” error due to…
  • How do I establish a link/relationship between objects?
  • What does this symbol mean in JavaScript?
  • slim php get route with parameter (user login system)
  • Creating Vue Search Bar | How to hide/show data…
  • What is git tag, How to create tags & How to…
  • Pandas / Python - Compare numerically each row with…
  • When use ResponseEntity and @RestController for…
  • Difference between single and double quotes in Bash
  • Is this request generated by EF Core buggy or is it my code?
  • What is your most productive shortcut with Vim?
  • How to UPSERT (MERGE, INSERT ... ON DUPLICATE…
  • For Loop on Lua
  • Multiple separate IF conditions in SQL Server
  • XMLHttpRequest cannot load ✘✘✘ No…
  • Vue routes doesn't work
  • What does "Fatal error: Unexpectedly found nil while…
  • Obtain most recent value for based on index in a…
  • How do I sort an observable collection?
  • How does PHP 'foreach' actually work?
  • What's the difference between eval, exec, and compile?
  • How to apply rolling t.test with pandas?
  • AppCompat v7 r21 returning error in values.xml?
  • Syntax error due to using a reserved word as a table…
  • static vector much slower than global in recursive function
  • Multiple scenarios @RequestMapping produces JSON/XML…
  • What exactly does Double mean in java?
  • Use string contains function in oracle SQL query
  • Error: 0xC0202009 at Data Flow Task, OLE DB…
  • How to prevent scrolling the whole page?
  • cannot make a static reference to the non-static field
  • Python Binomial Coefficient
  • Setting initial (state) values for ODE system in…
  • Logic error on counting consonants in a string C++
  • How to calculate distance between two locations…
  • Vue.js 3: props type validation with custom type
  • Unexpected end of JSON input while parsing
  • Regex matching in a Bash if statement
  • Java double.MAX_VALUE?
  • Vertical Align Center in Bootstrap 4
  • How to handle Vue 2 memory usage for large data (~50…
  • Is it possible to apply CSS to half of a character?
  • How to use AVX instructions to optimize ReLU written in C
  • How to initialize an array of custom objects
  • Reference — What does this symbol mean in PHP?
  • What is the difference between float and double?
  • How to parse deserialized object back into arrays?
  • Equals(=) vs. LIKE
  • How to properly do JSON API GET requests and assign…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How do I put a variable inside a string?

Next Post:

Restore a postgres backup file using the command line?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error