SQL Server: Literals
This SQL Server tutorial explains how to use literals (strings, integers, decimals, and datetime values) in SQL Server with examples.
In SQL Server, a literal is the same as a constant. We’ll cover several types of literals – string, integer, decimal, and datetime literals.
String literals are always surrounded by single quotes (‘).
'TechOnTheNet.com' 'ABC' '123'
These string literal examples contain of strings enclosed in single quotes. But what happens if you want your string to also contain a single quote within the text?
'TechOnTheNet.com is great! It's my favorite site!'
In this example, the literal string contains a single quote in the word,
It's. This additional single quote will cause the literal to terminate at the second quote, as follows (and possibly result in an error being raised):
'TechOnTheNet.com is great! It'
To workaround string literals that contain single quotes, you need to escape the single quote with an additionl single quote as follows:
'TechOnTheNet.com is great! It''s my favorite site!'
Integer literals can be either positive numbers or negative numbers, but do not contain decimals. If you do not specify a sign, then a positive number is assumed. Here are some examples of valid integer literals:
2014 +2014 -2014
Decimal literals can be either positive numbers or negative numbers and contain decimals. If you do not specify a sign, then a positive number is assumed. Here are some examples of valid decimal literals:
2014.6 +2014.6 -2014.6
Datetime literals are character representations of datetime values that are enclosed in single quotes. Here are some examples of valid datetime literals:
'May 12, 2014' '2012/05/12' '2014/05/12 09:49:12'