SQL_ Conversion format Functions-cast () and CONVERT ()

Source: Internet
Author: User

An expression that explicitly converts an expression of one data type to another data type. CAST and CONVERT provide similar functionality.

Cast

SELECT CAST('12.5'  as int)--failed to convert varchar value ' 12.5 ' to data type int. SELECT CAST('12.5'  as decimal(5,3))--12.500SELECT CAST(12.5  as varchar)--12.5SELECT CAST('2016/07/01'  as datetime)--2016-07-01 00:00:00.000

Convert

The CONVERT () function is a general function that converts a date to a new data type.

The CONVERT () function can display date/time data in different formats.

Select Convert(varchar( -),GETDATE(), -)--2016-07-01 13:55:36Select Convert(varchar( -),GETDATE(),121)--2016-07-01 13:55:11.017Select Convert(varchar( -),GETDATE(),111)--2016/07/01Select CONVERT(VARCHAR( +),GETDATE())--1 1:43pm

Convert () function conversion format.

Select CONVERT(VARCHAR( +), A)-- ASelect CONVERT(int,' A')-- ASelect CONVERT(decimal,'12.647')-- -Select CONVERT(decimal(5,3),'12.647')--12.647Select CONVERT(datetime,'2016/07/01')--2016-07-01 00:00:00.000Select CONVERT(DateTime2,'2016/07/01')--2016-07-01 00:00:00.0000000

Reference:

https://technet.microsoft.com/zh-cn/library/ms187928 (sql.90). aspx

Http://www.w3school.com.cn/sql/func_convert.asp

SQL_ Conversion format Functions-cast () and CONVERT ()

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.