How To Convert Numeric Expression Data Types using the CAST() Function?
Submitted by: AdministratorIf you want to convert the data type of a numeric expression to a new data type, you can use the CAST(expression AS data_type) function. The tutorial exercise below shows you how to use the CAST() function:
-- FLOAT converted to NUMERIC by CAST()
DECLARE @pi FLOAT(24);
SET @pi = 3.141592E+00;
SELECT CAST(@pi AS NUMERIC(5,2));
GO
3.14
-- FLOAT converted to NUMERIC by CAST()
DECLARE @x FLOAT(24);
SET @x = 12345.12E+00;
SELECT CAST(@x AS NUMERIC(10,5));
GO
12345.12305
-- FLOAT converted to INT by CAST()
DECLARE @x FLOAT(24);
SET @x = 12345.12E+00;
SELECT CAST(@x AS INT);
GO
12345
Submitted by: Administrator
-- FLOAT converted to NUMERIC by CAST()
DECLARE @pi FLOAT(24);
SET @pi = 3.141592E+00;
SELECT CAST(@pi AS NUMERIC(5,2));
GO
3.14
-- FLOAT converted to NUMERIC by CAST()
DECLARE @x FLOAT(24);
SET @x = 12345.12E+00;
SELECT CAST(@x AS NUMERIC(10,5));
GO
12345.12305
-- FLOAT converted to INT by CAST()
DECLARE @x FLOAT(24);
SET @x = 12345.12E+00;
SELECT CAST(@x AS INT);
GO
12345
Submitted by: Administrator
Read Online MS SQL Server Job Interview Questions And Answers
Top MS SQL Server Questions
☺ | How To Select Some Specific Rows from a Table in MS SQL Server? |
☺ | How To Sort Query Output in Descending Order in MS SQL Server? |
☺ | How To Find Out What Is the Default Collation in a Database? |
☺ | How To Write Character String Constants or Literals 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. |