How To Convert Character Strings into Numeric Values?
Submitted by: AdministratorSometimes you need to convert numeric values enclosed in character strings back to numeric values by using the CONVERT() function. When converting character strings to values with CONVERT(), you need to remember two rules:
* Leading and trailing space characters are ignored.
* The input string must represent a numeric literal that matches the target numeric data type.
* If the input string does not represent a numeric literal that matches the target numeric data type, you will get a conversion error.
The tutorial exercise below shows you how to use the CONVERT() function to convert strings to values:
SELECT CONVERT(NUMERIC(10,5), ' 12345.12 ');
GO
12345.12000
-- Input does not match the target data type
SELECT CONVERT(INT, '12345.12');
GO
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar
value '12345.12' to data type int.
-- Input does not match the target data type
SELECT CONVERT(NUMERIC(10,5), '12345.12E+00');
GO
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
-- Double conversion works
SELECT CONVERT(NUMERIC(10,5),
CONVERT(FLOAT(24), ' 12345.12E+00 '));
GO
12345.12012
SELECT CONVERT(FLOAT(24), ' 12345.12E+00 ');
Submitted by: Administrator
* Leading and trailing space characters are ignored.
* The input string must represent a numeric literal that matches the target numeric data type.
* If the input string does not represent a numeric literal that matches the target numeric data type, you will get a conversion error.
The tutorial exercise below shows you how to use the CONVERT() function to convert strings to values:
SELECT CONVERT(NUMERIC(10,5), ' 12345.12 ');
GO
12345.12000
-- Input does not match the target data type
SELECT CONVERT(INT, '12345.12');
GO
Msg 245, Level 16, State 1, Line 1
Conversion failed when converting the varchar
value '12345.12' to data type int.
-- Input does not match the target data type
SELECT CONVERT(NUMERIC(10,5), '12345.12E+00');
GO
Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric.
-- Double conversion works
SELECT CONVERT(NUMERIC(10,5),
CONVERT(FLOAT(24), ' 12345.12E+00 '));
GO
12345.12012
SELECT CONVERT(FLOAT(24), ' 12345.12E+00 ');
Submitted by: Administrator
Read Online MS SQL Server Job Interview Questions And Answers
Top MS SQL Server Questions
☺ | How To Convert Binary Strings into Integers in MS SQL Server? |
☺ | What Happens If NULL Values Are Involved in Bitwise Operations? |
☺ | What Happens If You Insert a Duplicate Key for the Primary Key Column in MS SQL Server? |
☺ | What Happens If Strings Are Casted into Wrong Code Pages in MS SQL Server? |
☺ | How To Use Subqueries with the IN Operators in MS SQL Server? |
Top Databases Programming Categories
☺ | RDBMS Interview Questions. |
☺ | SQL Interview Questions. |
☺ | SSRS Interview Questions. |
☺ | Sybase Interview Questions. |
☺ | Database Administrator (DBA) Interview Questions. |