|
Home >> FAQs/Tutorials >> SQL Server FAQ
SQL Server FAQ - CONVERT() - Converting Numeric Expression Data Types
By: FYIcenter.com
(Continued from previous topic...)
How To Convert Numeric Expression Data Types using the CONVERT() Function?
If you want to convert the data type of a numeric expression to a new data type,
you can use the CONVERT(data_type, expression) function.
The tutorial exercise below shows you how to use the CONVERT() function:
-- FLOAT converted to NUMERIC by CONVERT()
DECLARE @pi FLOAT(24);
SET @pi = 3.141592E+00;
SELECT CONVERT(NUMERIC(5,2), @pi);
GO
3.14
-- FLOAT converted to NUMERIC by CONVERT()
DECLARE @x FLOAT(24);
SET @x = 12345.12E+00;
SELECT CONVERT(NUMERIC(10,5), @x);
GO
12345.12012
-- FLOAT converted to INT by CONVERT()
DECLARE @x FLOAT(24);
SET @x = 12345.12E+00;
SELECT CONVERT(INT, @x);
GO
12345
(Continued on next topic...)
- What Is an Expression?
- What Are Arithmetic Operators?
- What Happens to an Arithmetic Operation with Two Different Data Types?
- How To Convert a Numeric Expression from One Data Type to Another?
- How To Convert Numeric Expression Data Types by Assignment Operations?
- How To Convert Numeric Expression Data Types using the CAST() Function?
- How To Convert Numeric Expression Data Types using the CONVERT() Function?
- How To Convert Character Strings into Numeric Values?
- What Happens When Converting Big Values to Integers?
- What Happens When Converting Big Values to NUMERIC Data Types?
- What Are the Mathematical Functions Supported by SQL Server 2005?
- How To Convert Numeric Values to Integers?
- How To Round a Numeric Value To a Specific Precision?
- How To Generate Random Numbers with the RAND() Function?
|