Syntax error when resolving SQL to convert varchar values to columns with data type int

Source: Internet
Author: User

Today encountered a mistake like this, the specific error situation is as follows

The solution is as follows.

Database MSSQL When comparing the size, error prompt: "The varchar value ' 24.5 ' is converted to a column of the data type int" syntax error occurs! "The type of the Analysis database design column is varchar, and the above error is reported when the search comparison statement executes the" SELECT * from DataList where price>10 the order by id ".
As long as the price column contains decimals, there is an error, and price is a varchar type and must be converted to a numeric type.

This can be rewritten as: "SELECT * from DataList where convert (Int,convert (numeric (18,9), price)) >10 The ORDER by id"

Alternatively, you can write: "SELECT * from DataList where CONVERT (Int,cast (price as float)) >10 The ORDER by id" resolves the issue.

That is, the type of varchar is converted to numbers, and then to Int.

Another idea: When data is entered, it is possible to determine if it is an integer, and the cast, such as int (price), can also avoid errors in future SQL comparisons.

Appendix, question type Ibid.:

A syntax error occurs when SQL converts a varchar value to a column with a data type of int;

The group table has a varchar field grouppower, with a value of 1,2,3,4, and an SQL statement for
SELECT * FROM Dbo.function where
FunctionId in
(
Select Grouppower from Dbo.group
where groupid=1
)
A syntax error occurred while performing an error converting the varchar value ' 1,2,3,4 ' to a column with a data type of int.

Workaround:
Change the Where condition to:
where CHARINDEX (RTrim (functionId), (select top 1 grouppower from Loan_group where groupid=1)) >0

Syntax error occurred while resolving SQL converting varchar values to columns with data type int

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.