Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert text to numeric 2

Status
Not open for further replies.

hapax

Programmer
Nov 10, 2006
105
US
How do I convert a "text" type value to a "numeric" type, in T-SQL?

I tried the following but it throws an error:

SELECT CAST(my_text as numeric)

Note: The text values are in proper number format, so I don't need to worry about that too much. I just need to convert them to numerics so I can perform calculations on them
 
You can cast to varchar first.
Code:
CREATE TABLE #T1  (Col1 TEXT)

INSERT INTO #T1 SELECT '1'
INSERT INTO #T1 SELECT '2.2'

SELECT CONVERT(DECIMAL(18,2), CONVERT(VARCHAR(100), Col1)) FROM #T1 

DROP TABLE #T1
 
or use the substring function. I was gonna post a soltion but saw that RiverGuy beat me to it.



-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
I haven't gotten it working yet. Here's my actual line (in a WHERE clause):

SELECT CONVERT(decimail(25,15), CONVERT(text), gam.attribute_detail_text) < @NorthLatLine

do you see anything wrong?
 
OK, I fixed the misspelling of decimal, but it still doesn't work:

SELECT CONVERT(decimal(25,15), CONVERT(text), gam.attribute_detail_text) < @NorthLatLine
 
Code:
SELECT CONVERT(decimal(25,15), CONVERT(VarChar(50), gam.attribute_detail_text)) < @NorthLatLine


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Ah. You should give the star to RiverGuy. I didn't really do much.


-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
What seems to be the problem of doing it?

I didn't want to give the star myself because some people may view it as a voting cartel. I have not given a star to anyone in a long time. Truth be told, I'd prefer if nobody would give me stars, either. Some people are hung up on them.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top