Click here to Skip to main content
15,886,519 members
Articles / Database Development / SQL Server
Tip/Trick

How to convert nvarchar, money and smallmoney values to int

Rate me:
Please Sign up or sign in to vote.
4.82/5 (4 votes)
24 Jun 2011CPOL 28.7K   4  
This tip enables you to convert nvarchar, money and smallmoney values to int.
Simply, we can use the given conversion syntax to convert Nvarchar, smallmoney and money datatype values to int values.

select (CAST(CAST(ColumnName AS float) AS INT))

or
select (CASE WHEN ISNUMERIC(ColumnName)=1 THEN CAST(CAST(ColumnName AS float) AS INT)END )


Illustration

Create a table and insert values as:
create table payment(id varchar(5),amount nvarchar(15))
insert into payment values('E001',5000.00)

after that out of "select * from payment" will be
id amount
--- --------
E001 5000.00

Using Syntax, write code as:
select (CAST(CAST(amount AS float) AS INT)) from Payment

Output for amount columns will be:
amount
-------
5000

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --