Detailed description of the difference between cast and convert for forced type conversion in SQL SERVER, castconvert

Source: Internet
Author: User

Detailed description of the difference between cast and convert for forced type conversion in SQL SERVER, castconvert

Difference between cast and convert forced conversions in SQL SERVER

In SQL SERVER, cast and convert functions can be used for type conversion, and their functions are the same,

Only syntax is different.

Cast is generally easier to use. convert has the advantage of formatting the date and value.

Select CAST ('000000' as int) -- 123 select CONVERT (int, '000000') -- 123 select CAST (123 as int) -- 123 select CONVERT (int, 123.4) -- 123 select CAST ('2017. 4 'as int) select CONVERT (int, '123. 4 ') -- Conversion failed when converting the varchar value' 123. 4 'to data type int. select CAST ('2017. 4 'as decimal) -- 123 select CONVERT (decimal, '2017. 4') -- 123 select CAST ('2014. 1. 4 'as decimal (123.40) -- 123 select CONVERT (decimal (), '192. 4 ') -- 123.40 declare @ Num moneyset @ Num = 1234.56 select CONVERT (varchar (20), @ Num, 0) -- 1234.56 select CONVERT (varchar (20), @ Num, 1) -- 1,234.56 select CONVERT (varchar (20), @ Num, 2) -- 1234.5600

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.