This SQL Server tutorial explains how to use literals (strings, integers, decimals, and datetime values) in SQL Server with examples.
Description
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
String literals are always surrounded by single quotes (').
For example:
'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?
For example:
'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
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
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
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'