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.
| Author |
Topic |
|
HGClubTec
Starting Member
12 Posts |
Posted - 2010-04-30 : 09:54:37
|
| My database has a character field in which the data contains some text some numbers. What I thought I could do was a test for numeric and if it passed, then cast the field to a number. So I have this:Case When IsNumeric(UDD.UserValue) <> 1 Then UDD.UserValue Else Cast(UDD.UserValue as numeric) EndBut I keep getting a 'Error converting data type nvarchar to numeric'Can I not do this?TIA! |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-04-30 : 10:08:41
|
| That's 'coz you've got both types in the same column. Your CASE is returning either the original field (Varchar) or the fields CAST as numeric - you cant have both data types in the SAME column! |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-04-30 : 10:10:47
|
| [code]Case When IsNumeric(UDD.UserValue) = 1 Then Cast(UDD.UserValue as numeric) ELSE NULL End AS MyNumericColumn,Case When IsNumeric(UDD.UserValue) = 1 Then NULL Else UDD.UserValue End AS MyVarcharColumn[/code] |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-04-30 : 10:13:40
|
| IsNumeric(UDD.UserValue)=1 can be used effectively byUDD.UserValue not like '%[^0-9]%'MadhivananFailing to plan is Planning to fail |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-04-30 : 10:22:07
|
| Datatype for CAST is "numeric" so I assumed the UserValue field might have any valid "Float" data ... |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-04-30 : 10:40:39
|
| But the column UserValue is varchar datatypeselect Case When UDD.UserValue not like '%[^0-9.]%' Then Cast(UDD.UserValue as numeric) ELSE NULL End AS MyNumericColumn,Case When UDD.UserValue not like '%[^0-9.]%' Then NULL Else UDD.UserValue End AS MyVarcharColumnfrom (select 'iuahsd' as UserValue union allselect '12d3' union allselect '128.7') as UDDselect Case When IsNumeric(UDD.UserValue) = 1 Then Cast(UDD.UserValue as numeric) ELSE NULL End AS MyNumericColumn,Case When IsNumeric(UDD.UserValue) = 1 Then NULL Else UDD.UserValue End AS MyVarcharColumnfrom (select 'iuahsd' as UserValue union allselect '12d3' union allselect '128.7') as UDDMadhivananFailing to plan is Planning to fail |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2010-04-30 : 10:59:34
|
[^0-9.] wont' save you from two decimal points in the field ... although I suppose you could do UDD.UserValue not like '%[^0-9.]%'AND UDD.UserValue not like '%.%.%' but might as well sue IsNumeric() at that point and live with its annoyances ... |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-05-07 : 03:34:20
|
quote: Originally posted by Kristen [^0-9.] wont' save you from two decimal points in the field ... although I suppose you could do UDD.UserValue not like '%[^0-9.]%'AND UDD.UserValue not like '%.%.%' but might as well sue IsNumeric() at that point and live with its annoyances ...
Yes. You are correctMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|