Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Converting varchar to int

Author  Topic 

MaryamFarmani
Starting Member

2 Posts

Posted - 2007-06-06 : 12:42:42
select numberemployees, convert(int, substring (numberemployees, 1, (charindex ('.',numberemployees)-1)))
from adagency

numberemployees is a (varchar, 4)

With this code I get number of employees in an integer format and that’s what I want. But the problem is with those numbers without '.'
I mean it works fine with a number like 26.5, which returns 26 ...But I get error on converting a number like 26

Any idea about solving this issue?
Appreciated in advance!
Maryam

harsh_athalye
Master Smack Fu Yak Hacker

5581 Posts

Posted - 2007-06-06 : 12:45:09
[code]select convert(int, convert(float, numberemployees))
From adagency [/code]

Harsh Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

sshelper
Posting Yak Master

216 Posts

Posted - 2007-06-06 : 12:47:53
You can also use the CAST function:

SELECT CAST(CAST(NumberEmployees AS FLOAT) AS INT) as NumberEmployees
FROM adagency

SQL Server Helper
http://www.sql-server-helper.com
Go to Top of Page

MaryamFarmani
Starting Member

2 Posts

Posted - 2007-06-06 : 12:56:36
Yes, it works...
Thanks and God bless you!
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2007-06-07 : 10:47:36
Why did you use varchar datatype to store numbers?

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -