560 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owls only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
T-SQL data types - int, float, decimal, varchar, etc. Part six of a six-part series of blogs |
---|
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.
This blog is part of our SQL tutorial. Don't forget that Wise Owl are mainly a provider of SQL (and other) training courses. |
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).
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.
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 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!
Parts of this blog |
---|
|
Some other pages relevant to the above blogs include:
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2023. All Rights Reserved.