When and where Single quotes are used in SQL?
Asked
Active
Viewed 487 times
0
-
The first comment on https://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-back-ticks-in-mysql/41442734 might answer your question. – sticky bit Jun 30 '18 at 13:05
-
1Single quotes are the standard delimiter for string and date constants. That is where they should be used. – Gordon Linoff Jun 30 '18 at 13:17
1 Answers
-1
My answer is related to SQL Server only.
In my knowledge - 2 places
- Used to indicate the beginning and end of a string in SQL. As, defining the values of the data types like varchar, nvarchar, char, uniqueIdentifier, timestamp, datetime, date etc.
- For column alias name. (Also a type to writing string only as said in 1st point)
- To escape the reserved character ['] itself while creating dynamic queries.
For Example -
declare @name nvarchar(50)= 'ABC BCD CDE'
declare @query nvarchar(100)
set @query = 'select replace('''+@name+''', '' '', '''') as ''ColumnName'''
select @query
exec sp_executesql @query

Prateek Sharma
- 132
- 4
-
Some DBMS might accept single quotes to delimit aliases, others won't. In general double quotes are the better choice to delimit aliases, unless the DBMS has it's own name delimiters, e.g. MySQL has backticks, SQLServer has square brackets... – sticky bit Jun 30 '18 at 14:01
-
Ooh, I took the question related with SQL Server only. YES, you are right. – Prateek Sharma Jun 30 '18 at 14:15