Click here to Skip to main content
15,935,259 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to convert the varchar field
JavaScript
RIGHT(CBFieldName,LEN(CBFieldName)-CHARINDEX('-',CBFieldName))

to integer?
My query is as below

select *,RIGHT(CBFieldName,LEN(CBFieldName)-CHARINDEX('-',CBFieldName))AS TEST from FTSTableDetail
where TableName='OUT_CCD'
ORDER BY RIGHT(CBFieldName,LEN(CBFieldName)-CHARINDEX('-',CBFieldName))
Posted
Comments
PIEBALDconsult 29-Dec-15 0:12am    
An example of some input would probably help.

1 solution

If final result of your expr. (
JavaScript
RIGHT(CBFieldName,LEN(CBFieldName)-CHARINDEX('-',CBFieldName))

) is alphanumeric then you can use cast or convert function as follows

JavaScript
CAST(RIGHT(CBFieldName,LEN(CBFieldName)-CHARINDEX('-',CBFieldName)) AS INT) 
 
Share this answer
 
v2
Comments
Korathu123 29-Dec-15 2:47am    
The above solution worked. But if the output in case is not alphanumeric (I mean simply varchar) there will be casting error.
Manoj Kumar Choubey 29-Dec-15 3:35am    
You need to put handle that kind of error by checking numeric value.
Manoj Kumar Choubey 29-Dec-15 3:45am    
CASE ISNUMERIC(RIGHT(CBFieldName,LEN(CBFieldName)-CHARINDEX('-',CBFieldName)))
WHEN 1 THEN CAST( RIGHT(CBFieldName,LEN(CBFieldName)-CHARINDEX('-',CBFieldName)) AS INT)
ELSE
0
END AS columnalias

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