Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and medical aesthetics - Did you convert a string into an int numeric type in Sql?
Did you convert a string into an int numeric type in Sql?
There are two kinds.

1 and convert(int, field name)?

Example: select convert(int,' 3')

2.cast (field name is int) for example: select cast('3' is int)

Usually, it is not necessary to convert a string to a numeric type. If you need to compare whether two fields are equal, but one field is a string type and the other is a number type, SQL SERVER will automatically convert the string into a number and then compare it.

Extended data:

Matters needing attention

Both functions are used for type conversion, but the syntax format is different. It is said that there are some differences in conversion, but I am more accustomed to using the convert function. On the one hand, this function is more like the syntax of a function, on the other hand, it can also specify the conversion format when time and value are converted into strings.

For exact numeric data types, the converted string is the value we store. For example:

declare @i int set @ I = 123456789 print ' test:'+convert(varchar(20),@ I)

The output is: test: 123456789.

However, it is not so simple for approximate numeric data types.

declare @i float set @ I = 123456789 print ' test:'+convert(varchar(20),@ I)

Output result: test: 1.23457e+008.