Click here to Skip to main content
15,887,355 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi!

I have been trying to convert this column to int.

Here is my query:
SQL
SELECT CAST(SUBSTRING(loanAmount, PATINDEX('%[^0 ]%', loanAmount + ' '), LEN(loanAmount))AS INT)
FROM [dbCMSextract].[dbo].[tblLoanMaster_copy]

But it keeps returning me this error:

Msg 245, Level 16, State 1, Line 1<br />
Conversion failed when converting the varchar value '1.00' to data type int.


Sample Data
loanAmount: 00001.00
Posted

SQL
CAST('00001.00' as float) 
will work
if you need int type
SQL
SELECT CAST( CAST('00001.00' as float) as int)

or
SQL
SELECT CAST(ROUND('00001.00',0,1) AS INT)
 
Share this answer
 
v2
Try this ,
SQL
SELECT Cast(Cast(SUBSTRING('1.00', PATINDEX('%[^0 ]%', '1.00' + ' '), LEN('1.00')) as Decimal) as Int)

Or
SQL
SELECT Cast(Cast(SUBSTRING('1.00', PATINDEX('%[^0 ]%', '1.00' + ' '), LEN('1.00')) as float) as Int)
 
Share this answer
 
v3

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900