BLOGS BY TOPIC▼
BLOGS BY AUTHOR▼
BLOGS BY YEAR▼
Whether you're creating tables in SQL Server, using SQL variables or passing parameters to stored procedures, you'll need to understand what the possible data types that you can use are.
- Data Types in T-SQL for SQL Server
- Text Data Types in T-SQL
- Number Data Types in T-SQL
- Date and Time Data Types in SQL
- Logical, Boolean, Yes/No or Bit Data Types in SQL
- Converting between Data Types (this blog)
This blog is part of our SQL tutorial. Don't forget that Wise Owl are mainly a provider of SQL (and other) training courses.
Posted by Andy Brown on 05 October 2012
You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.
Converting between Data Types
I think it's a fair statement to say that SQL is fussy about its data types. In SQL, you definitely can't put a round peg in a square hole, or mix apples and pears (although you can still mix your metaphors, as this sentence shows).
When you Need to Convert Data Types
Nearly always, is the short answer. A longer answer is that you need to convert data types when either SQL will try to do it for you (and get it wrong), or when SQL will generate an error otherwise. Here's an example of each.
In the following code, we've created a purchase of 2 toys costing 9 units each. The average value of each item should be 4.5:
-- create a new table to hold purchases
CREATE TABLE tblPayments(
Purchase varchar(100),
Quantity int,
Amount int
)
-- add a purchase
INSERT INTO tblPayments (
Purchase, Quantity, Amount
) VALUES (
'Fluffy toy', 2, 9
)
-- display this purchase
SELECT
Purchase,
Amount / Quantity AS Value
FROM
tblPayments
However, what this code actually shows is:

SQL sees an int data type and assumes that everything is an integer.
In the example above SQL converted the data type wrongly. Sometimes, you'll just get an error when you try to use mixed data types. Suppose you try to show the price above in text format:
-- display this purchase
SELECT
Purchase,
Amount + ' dollars'
FROM
tblPayments
This SQL will yield the following error:

SQL sees the Amount is an integer, and thinks the dollars text string should be too.
The easiest answer is to make sure in SQL that you are ALWAYS comparing data of exactly the same type.
The Conversion Functions
SQL has two separate conversion functions: CAST and CONVERT. The syntax of CAST is:
Here's how we could have solved our two problems above. First the inaccurate calculation of the average value of the purchase:
-- display this purchase
SELECT
Purchase,
-- convert numbers before division
CAST(Amount AS float) /
CAST(Quantity AS float) AS Value
FROM
tblPayments
Now the concatenation error:
-- display this purchase
SELECT
Purchase,
CAST(Amount AS varchar(10)) + ' dollars'
FROM
tblPayments
This would give:

The integer has been converted to a text string before concatenation.
If you're concatenating text it's usually best to use varchar and not char, as char would put aside a fixed number of characters, leading to blanks in your final displayed answer.
The CONVERT Functon
The CONVERT function works well for dates, but CAST is easier to use for numbers and text (being much more intuitive). The syntax of CONVERT is:
So we could have written our examples above as:
-- display this purchase
SELECT
Purchase,
-- convert numbers before division
CONVERT(float,Amount) /
CONVERT(float,Quantity) AS Value
FROM
tblPayments
and:
-- display this purchase
SELECT
Purchase,
CONVERT(varchar(10),Amount) + ' dollars'
FROM
tblPayments
CAST and CONVERT do the same thing, but CAST puts the arguments in a more logical order!
- Data Types in T-SQL for SQL Server
- Text Data Types in T-SQL
- Number Data Types in T-SQL
- Date and Time Data Types in SQL
- Logical, Boolean, Yes/No or Bit Data Types in SQL
- Converting between Data Types (this blog)