Author |
Topic |
dbechberger
Starting Member
2 Posts |
Posted - 2009-01-09 : 10:19:16
|
I have a table that contains data in an nvarchar column such as:Column A5523TBDWhat I would like to be able to do is search on Column A>2 and Column A<5. The problem is that the TBD is in there and it throws an error trying to convert it to a numeric. I tried the code below with the same error. How can I do this?SELECT a.ID FROM (select * FROM tableA WHERE isnumeric(ColumnA)=1) as a WHERE a.ColumnA>=1.0 AND a.ColumnA<=5.0 |
|
jhocutt
Constraint Violating Yak Guru
385 Posts |
Posted - 2009-01-09 : 10:24:44
|
declare @a table ( columna Nvarchar(10))insert into @a select 5 union allselect 5 union allselect 2 union allselect 3 union allselect 1 union allselect 4.5 union allselect 5.1 union allselect 7.0 union allselect 8.0 union allselect 9.0 union allselect 'TBD'select * from @awhere isnumeric(columna) = 1and columna between 1.0 and 5.0"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-09 : 10:31:54
|
try thisdeclare @tab1 table ( a nvarchar(50))insert into @tab1select '5' union allselect '5' union allselect '2' union allselect '3' union allselect 'tbd'select * from( select a from @tab1 where isnumeric(a)= 1 ) p where p.a > =1 and p.a < = 5 |
|
|
revdnrdy
Posting Yak Master
220 Posts |
Posted - 2009-01-09 : 10:34:12
|
quote: Originally posted by dbechberger I have a table that contains data in an nvarchar column such as:Column A5523TBDWhat I would like to be able to do is search on Column A>2 and Column A<5. The problem is that the TBD is in there and it throws an error trying to convert it to a numeric. I tried the code below with the same error. How can I do this?SELECT a.ID FROM (select * FROM tableA WHERE isnumeric(ColumnA)=1) as a WHERE a.ColumnA>=1.0 AND a.ColumnA<=5.0
Perhaps try using an IN clause?SELECT ID FROM TABLEA WHERE ID IN (SELECT ID FROM TABLEA WHERE ColumnA not like 'TBD' AND ColumnA BETWEEN 1.0 AND 5.0)I got a chance to test this and it works. It returns all ID's where Column A value is between 1.0 and 5.0 inclusiver&r |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-09 : 10:37:10
|
quote: Originally posted by jhocutt declare @a table ( columna Nvarchar(10))insert into @a select 5 union allselect 5 union allselect 2 union allselect 3 union allselect 1 union allselect 4.5 union allselect 5.1 union allselect 7.0 union allselect 8.0 union allselect 9.0 union allselect 'TBD'select * from @awhere isnumeric(columna) = 1and columna between 1.0 and 5.0"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking
hi,Put single quotes while inserting numbers into @a table otherwise it will throw error |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-09 : 11:35:02
|
quote: Originally posted by raky
quote: Originally posted by jhocutt declare @a table ( columna Nvarchar(10))insert into @a select 5 union allselect 5 union allselect 2 union allselect 3 union allselect 1 union allselect 4.5 union allselect 5.1 union allselect 7.0 union allselect 8.0 union allselect 9.0 union allselect 'TBD'select * from @awhere isnumeric(columna) = 1and columna between 1.0 and 5.0"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking
hi,Put single quotes while inserting numbers into @a table otherwise it will throw error
i dont think it will throw error. it will implicitly convert values to varchar |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-09 : 11:47:12
|
quote: Originally posted by visakh16
quote: Originally posted by raky
quote: Originally posted by jhocutt declare @a table ( columna Nvarchar(10))insert into @a select 5 union allselect 5 union allselect 2 union allselect 3 union allselect 1 union allselect 4.5 union allselect 5.1 union allselect 7.0 union allselect 8.0 union allselect 9.0 union allselect 'TBD'select * from @awhere isnumeric(columna) = 1and columna between 1.0 and 5.0"God does not play dice" -- Albert Einstein"Not only does God play dice, but he sometimes throws them where they cannot be seen." -- Stephen Hawking
hi,Put single quotes while inserting numbers into @a table otherwise it will throw error
i dont think it will throw error. it will implicitly convert values to varchar
Hi Visakh,Error converting data type varchar to numeric.I got this error when i run the above query without keeping singlequotes...while inserting numbers. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-09 : 11:54:20
|
what about this?insert into @a select 'TBD' union allselect 5 union allselect 5 union allselect 2 union allselect 3 union allselect 1 union allselect 4.5 union allselect 5.1 union allselect 7.0 union allselect 8.0 union allselect 9.0 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-09 : 11:58:38
|
quote: Originally posted by visakh16 what about this?insert into @a select 'TBD' union allselect 5 union allselect 5 union allselect 2 union allselect 3 union allselect 1 union allselect 4.5 union allselect 5.1 union allselect 7.0 union allselect 8.0 union allselect 9.0
yaa this one also returning same error messageMsg 8114, Level 16, State 5, Line 5Error converting data type varchar to numeric. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-09 : 12:05:10
|
oh ok... i think thats because of union all. try just insert a single valueinsert into @aselect 5 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-01-09 : 12:10:02
|
quote: Originally posted by visakh16 oh ok... i think thats because of union all. try just insert a single valueinsert into @aselect 5
yes ur correct. when i executed the below query declare @a table (columna Nvarchar(10))insert into @aselect 5select * from @aIt is taking nearly 1 sec to retrieve the record |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-01-09 : 12:13:41
|
even the below will work. it might take more time to do implicit conversioninsert into @a select 5 union allselect 5 union allselect 2 union allselect 3 union allselect 1 union allselect 4.5 union allselect 5.1 union allselect 7.0 union allselect 8.0 union allselect 9.0 |
|
|
dbechberger
Starting Member
2 Posts |
Posted - 2009-01-09 : 17:27:36
|
The column data given above is only an example of the data. It is not feasible to UNION ALL possible combinations because it is almost unlimited. I tried doing a subquery in the from clause. The subquery returns all the correct numeric value but when used as the subquery I still get an error on convert. Could this be an order of operations sort of problem? |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-01-10 : 02:19:04
|
Jsut want to point out that isnumeric() is not reliableselect isnumeric('12d3')MadhivananFailing to plan is Planning to fail |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|