Click here to Skip to main content
15,884,836 members
Articles / Programming Languages / T-SQL

What is the Difference Between Cast and Convert?

Rate me:
Please Sign up or sign in to vote.
4.97/5 (10 votes)
6 Jan 2016MIT2 min read 52.9K   11   4
Difference between Cast and Convert

In this article, we’re going to explore the CAST and CONVERT functions to understand whether there are any significant differences worth knowing when we want to convert data from one type to another.

Since CAST and CONVERT can be used interchangeably in most situations, you may be wondering if one is better than the other.

Read on! We’ll see whether there really is a difference between CAST and CONVERT in this post.

All the examples for this lesson are based on Microsoft SQL Server Management Studio and the AdventureWorks2012 database.  You can get started using these free tools using my Guide Getting Started Using SQL Server.

Are Cast and Convert Different?

CAST and CONVERT are both used to convert data from one data type to another, and it is no coincidence they share the same entry in MSDN.

Here is an example using both CAST and CONVERT in the same statement:

SQL
SELECT CAST ('10' as int) * 20,
       CONVERT (int, '10') * 20

In both cases, we’re converting from the text value ’10’ to its integer representation.

Hopefully this example provides jogs your memory regarding CAST and CONVERT.  If not, read my blog Data Type Conversion Functions to know all the details.

Similarities with CAST and CONVERT

In many ways, CAST and CONVERT are similar. Both are used to convert data from one type to another. Though their syntax is different, both functions are able to convert values from one formation to another.

Anything you can do with CAST, you can do with CONVERT.  If you’re wondering whether there is a difference in performance, according to Nakul Vachhrajani’s post, there isn’t. In fact, Nakul shows that CAST is really implemented internally via CONVERT!

Differences Between CAST and CONVERT

CAST is part of the ANSI-SQL specification; whereas, CONVERT is not.  In fact, CONVERT is SQL implementation specific.

CONVERT differences lie in that that accepts an optional style parameter which is used for formatting.

For example, when converting a DateTime datatype to Varchar, you can specify the resulting date’s format, such as YYYY/MM/DD or MM/DD/YYYY.

SQL
SELECT CONVERT(VARCHAR,GETDATE(),101) as MMDDYYYY,
       CONVERT(VARCHAR,GETDATE(),111) as YYYYMMDD

The numbers shown in red are the style formatter. There are many style formats you can use. The complete list is here.

Should I Use CAST or Convert?

Unless you have some specific formatting requirements you’re trying to address during the conversion, I would stick with using the CAST function. There are several reasons I can think of:

  1. CAST is ANSI-SQL compliant; therefore, more apt to be used in other database implementation.
  2. There is no performance penalty using CAST.
  3. I think CAST is easier to read, and since it is part of the ANSI specification, your non-SQLServer DBA thinks so too!

What do you think? I would love to know. Please post in the comments which function you would rather use and why. This is a great way for us to learn from each others’ experiences.

The post What is the Difference Between Cast and Convert? appeared first on Essential SQL.

License

This article, along with any associated source code and files, is licensed under The MIT License


Written By
Easy Computer Academy, LLC
United States United States
Hello my name is Kris. I’m here because I am passionate about helping non-techie people to overcome their fear of learning SQL.

I know what it is like to not know where to start or whether the time spent learning is worth the effort. That is why I am here to help you to:
- Get started in an easy to follow step-by-step manner.
- Use your time wisely so you focus on what is important to learn to get the most value from your time.
- Answer your questions. Really! Just post a comment and I’ll respond. I’m here to help.

It wasn’t long ago that I was helping a colleague with some reporting. She didn’t know where to start and soon got overwhelmed and lost as she didn’t know SQL.

I felt really bad, as she was under pressure to get some summary information to her boss, the built-in reports were falling short, and to make them better would require her to know SQL. At that time that seemed impossible! It in dawned on me, it doesn’t have to be that way.

Then I discovered a way for anyone with the desire to easily learn SQL. I worked with my co-worker, started to teach her what I learned and soon she was able to write reports and answer her boss’ questions without getting stressed or ploughing hours into manipulating data in Excel.

It hasn’t always been easy. Sometimes the information seems abstract or too conceptual. In this case I’ve found out that a visual explanation is best. I really like to use diagrams or videos to explain hard-to-grasp ideas.

Having video, pictures, and text really help to reinforce the point and enable learning.

And now I want to help you get the same results.

The first step is simple, click here http://www.essentialsql.com/get-started-with-sql-server/

Comments and Discussions

 
GeneralMy vote of 5 Pin
Renju Vinod7-Feb-16 17:12
professionalRenju Vinod7-Feb-16 17:12 
QuestionGood clarification Pin
Karim Sultan8-Jan-16 10:29
Karim Sultan8-Jan-16 10:29 
GeneralMy vote of 5 Pin
Camilo Reyes1-Jan-16 10:35
professionalCamilo Reyes1-Jan-16 10:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.