The difference between cast and convert forced type conversions in SQL Server

Source: Internet
Author: User

In SQL Server, both the cast and CONVERT functions are available for type conversions, and their functions are the same.

It's just a different syntax.

Cast is generally easier to use, and the advantage of convert is that dates and values can be formatted.

CodeSelect CAST('123'  as int)--123Select CONVERT(int,'123')--123Select CAST(123.4  as int)--123Select CONVERT(int,123.4)--123Select CAST('123.4'  as int)Select CONVERT(int,'123.4')--Conversion failed when converting the varchar value ' 123.4 ' to data type int.Select CAST('123.4'  as decimal)--123Select CONVERT(decimal,'123.4')--123Select CAST('123.4'  as decimal(9,2))--123.40Select CONVERT(decimal(9,2),'123.4')--123.40Declare @Num  MoneySet @Num = 1234.56Select CONVERT(varchar( -),@Num,0)--1234.56Select CONVERT(varchar( -),@Num,1)--1,234.56Select CONVERT(varchar( -),@Num,2)--1234.5600

The difference between cast and convert forced type conversions in SQL Server

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.