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.

  1. Data Types in T-SQL for SQL Server
  2. Text Data Types in T-SQL
  3. Number Data Types in T-SQL
  4. Date and Time Data Types in SQL
  5. Logical, Boolean, Yes/No or Bit Data Types in SQL
  6. 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:

Purchase with value of 20

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:

Error message 245 converting data

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:

CAST(What you're converting, data type you're converting to)

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 text description of an amount

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:

CONVERT(Data type to convert to, what you're converting)

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! 

This blog has 0 threads Add post