SQL Server FAQ-convert the numeric string of scientific notation to the decimal type

Source: Internet
Author: User

When operating the database today, You need to convert the string to the decimal type. The Code is as follows:

 

Select cast ('0. 12' as decimal (18, 2 ));

Select convert (decimal (18, 2), '0. 12 ');

 

When you need to convert the numeric string of the scientific notation into a decimal, the following two writing methods are reported:

MSG 8114, level 16, state 5, line 1

Error converting data type varchar to numeric.

 

Select cast ('0. 12e + 006 'As decimal (18, 2 ));

Select convert (decimal (18, 2), '0. 12e + 006 ');

 

I found a lot of information online and did not find the answer. Finally, we accidentally discovered that when the float type is converted to a string, a numeric string with scientific notation will be generated:

Select cast (1234400000 as float) as varchar)

1.2344e + 009

 

In reverse thinking, the numeric string of scientific notation can be converted to the float type, and then to the decimal type.

Select cast ('1. 2344e + 009' as float) as decimal ))

 

 

Summary: if it is a numeric string of scientific notation, it can be first converted to float and then converted to another numeric type.

When operating the database today, You need to convert the string to the decimal type. The Code is as follows:

 

Select cast ('0. 12' as decimal (18, 2 ));

Select convert (decimal (18, 2), '0. 12 ');

 

When you need to convert the numeric string of the scientific notation into a decimal, the following two writing methods are reported:

MSG 8114, level 16, state 5, line 1

Error converting data type varchar to numeric.

 

Select cast ('0. 12e + 006 'As decimal (18, 2 ));

Select convert (decimal (18, 2), '0. 12e + 006 ');

 

I found a lot of information online and did not find the answer. Finally, we accidentally discovered that when the float type is converted to a string, a numeric string with scientific notation will be generated:

Select cast (1234400000 as float) as varchar)

1.2344e + 009

 

In reverse thinking, the numeric string of scientific notation can be converted to the float type, and then to the decimal type.

Select cast ('1. 2344e + 009' as float) as decimal ))

 

 

Summary: if it is a numeric string of scientific notation, it can be first converted to float and then converted to another numeric type.

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.