|
Has anyone had this problem before? If you use the function
I would expect that 1d8 and 1D8 wouldn't be numeric. I'm using SQL Server 2005.
(comments are locked)
|
|
Melvyn is right, they are numeric but the odd thing is that you cant convert them to integers. You need to convert them to Float because D denotes to float double precision (inherited from Fortran?). Try:
If you would like to know if the string is possible to convert to an integer you can use a function I found somewhere a while ago. I can't remember where it came from (maybe from www.aspfaq.com ), and I don't want to take credit for it.
Try the function with something like this: Watch out for the scalar-function problem though.
Oct 27 '09 at 08:28 AM
Rob Farley
I voted this down because of problems with the implementation. See my reply below.
Oct 27 '09 at 10:17 AM
Peso
(comments are locked)
|
|
(comments are locked)
|
|
I know an answer has already been accepted, but I just came across this and thought I'd suggest a different solution using CLR. One of the great things about CLR is that it allows you to do things in functions (like error-handling) that standard UDFs don't allow. You can add more data type validation tests (like custom date format tests) by adding other similar public static methods to the class. C# class:
SQL Server create assembly:
SQL Server create function:
SQL Server Usage:
performance results (using SET STATISTICS TIME ON): I just ran a test on 100,000 rows of varchar(10) fields from our database that usually store numbers (old data was not always numeric). 26 of the 100,000 tested were not numbers. I compared the execution time for my CLR solution vs. IsReallyInt (accepted answer) vs. IsNum (another answer) vs. the built-in function ISNUMERIC. They all returned the same 26 rows, but they did it in approximately 1, 3, 4 and .5 seconds, respectively. (26 row(s) affected) (1 row(s) affected) SQL Server Execution Times: CPU time = 1078 ms, elapsed time = 1133 ms. (26 row(s) affected) (1 row(s) affected) SQL Server Execution Times: CPU time = 3062 ms, elapsed time = 3106 ms. (26 row(s) affected) (1 row(s) affected) SQL Server Execution Times: CPU time = 4032 ms, elapsed time = 4292 ms. (26 row(s) affected) (1 row(s) affected) SQL Server Execution Times: CPU time = 516 ms, elapsed time = 526 ms. What's the performance like on that?
Nov 06 '09 at 08:23 AM
Melvyn Harbour 1 ♦♦
edited answer to include performance results
Nov 06 '09 at 09:14 AM
Tom Staab
Nice work! +1...
Nov 06 '09 at 09:57 AM
Matt Whitfield ♦♦
Top notch stuff. Interesting stuff to read, particularly when it nicely exposes the fact that using CLR functions isn't always slower!
Nov 06 '09 at 10:40 AM
Melvyn Harbour 1 ♦♦
(comments are locked)
|
|
Technically, they are numeric, and it's working correctly:
(comments are locked)
|
|
Why are you checking for both AND CHARINDEX('-', @num) <= 1 You have already truncated the data if first character is "-". Try with this data too... declare @num varchar(10) = '²³' Use this function for proper results I agree, there is a danger with copying the code without reading and testing. Actually I posted the original code and haven't used it myself. :)
Oct 27 '09 at 10:40 AM
Håkan Winther
dbo.IsNum('1.234') Fails
Dec 23 '09 at 02:37 PM
dvroman
1.234 isn't an integer, so the function returns false (0) and it does.
Dec 27 '09 at 01:24 PM
Peso
Is it 1 point 234 or 1 thousand 234?
Dec 27 '09 at 01:25 PM
Peso
(comments are locked)
|
1 2 next page »

