July 14, 2008 at 1:02 am
In my table i have data in varchar. I am creating the view table in that want data in numeric.
I have tried following query but it gives error message "error converting data type varchar to numeric"
SELECT strTimeStamp, CAST(strItem AS numeric(18, 2)) AS Expr1 FROM tblDataEntry AS tblDataEntry
I also tried with
SELECT strTimeStamp, CONVERT(decimal,strItem) AS Expr1 FROM tblDataEntry AS tblDataEntry
Please tell me how to convert the same so i can use in MSCHART for generating graph.
July 14, 2008 at 2:16 am
There are some values in that column that cannot be converted to numeric. You're going to have to find those values and either remove them or exclude them from the query.
You can use IsNumeric to check,however there are some values that return true from isnumeric, but cannot be converted to numeric
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 14, 2008 at 3:51 am
Yes i found some values there as non numeric so i have added the condition like WHERE (tblItem.strItem NOT LIKE '%[A-Z+:-_]%')
Thanks for the same.
July 14, 2008 at 6:44 am
Obviously, that WHERE clause won't catch everything... but this will...
WHERE tblItem.strItem NOT LIKE '%[^0-9]%'
The circumflex (^) also means "NOT".
--Jeff Moden
Change is inevitable... Change for the better is not.
July 14, 2008 at 6:46 am
... heh... and before anyone even thinks of suggesting ISNUMERIC, try this...
SELECT ISNUMERIC('$1000') UNION ALL
SELECT ISNUMERIC('1,000') UNION ALL
SELECT ISNUMERIC('3d2') UNION ALL
SELECT ISNUMERIC('2e3')
--Jeff Moden
Change is inevitable... Change for the better is not.
July 14, 2008 at 6:51 am
Actually, I did suggest ISNumeric, with the warning that it's not perfect. Does your like allow for - and .?
Here's one that caught me very, very badly the other day...
SELECT ISNUMERIC (' - 0.00 ') -- 1
SELECT CAST(' - 0.00 ' AS NUMERIC (7,2)) -- error
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
July 14, 2008 at 8:00 am
GilaMonster (7/14/2008)
Actually, I did suggest ISNumeric, with the warning that it's not perfect. Does your like allow for - and .?
No... but those can easily be added while excluding a broader range of characters than the Where clause previously offered by the OP.
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply