In this post 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 (').
For example:
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:
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):
To workaround string literals that contain single quotes, you need to escape the single quote with an additionl single quote as follows:
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:
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:
Datetime literals are character representations of datetime values that are enclosed in single quotes. Here are some examples of valid datetime literals: