Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Count Decimal Places


Count Decimal Places

Author
Message
Keith Saynor
Keith Saynor
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 18

i'm trying to count the number of decimal places in a field.

e.g. mynumber decimal 9 (18,9)

Len(mynumber) result = 11

I've tried converting it to a string Len(STR(mynumber)) result = 10

What I really want is a count of the number of digits following the decimal point ignoring the trailing zeros

e.g. 0.8333 result should be 4

0.99 result should be 2

Any ideas - Thanks





Ken McKelvey
Ken McKelvey
SSC Eights!
SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)

Group: General Forum Members
Points: 958 Visits: 7267

DECLARE @D DECIMAL(18,9)
,@S VARCHAR(20)
,@R VARCHAR(20)

SET @D = 0.8333
SET @S = CAST(@D AS VARCHAR(20))
SET @R = REVERSE(SUBSTRING(@S, CHARINDEX('.', @S) + 1, 20))

SELECT LEN(SUBSTRING(@R, PATINDEX('%[1-9]%', @R), 20))


Ken McKelvey
Ken McKelvey
SSC Eights!
SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)SSC Eights! (958 reputation)

Group: General Forum Members
Points: 958 Visits: 7267

Forgot about no Decimal places:

DECLARE @D DECIMAL(18,9)
,@S VARCHAR(20)
,@R VARCHAR(20)
,@Pos SMALLINT

SET @D = 0.0
SET @S = CAST(@D AS VARCHAR(20))
SET @R = REVERSE(SUBSTRING(@S, CHARINDEX('.', @S) + 1, 20))
SET @Pos = PATINDEX('%[1-9]%' , @R)
IF @Pos = 0
SELECT 0
ELSE
SELECT LEN(SUBSTRING(@R, @Pos, 20))


R Michael
R Michael
SSC Veteran
SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)

Group: General Forum Members
Points: 271 Visits: 275
For what it's worth, here is a set-based approach. It requires a numbers table though.



IF EXISTS ( SELECT 1
FROM dbo.sysobjects
WHERE id = OBJECT_ID(N'[dbo].[CountDP]')
AND xtype IN ( N'FN', N'IF', N'TF' ) )
BEGIN
DROP FUNCTION [dbo].[CountDP]
END
GO

CREATE FUNCTION [dbo].[CountDP]
(
@decNumber DECIMAL(18, 9)
)
RETURNS TINYINT
AS BEGIN
/*******************************************************************************************************
* dbo.CountDP
*
* Usage:
print dbo.countdp(10.0000001) -- 7
print dbo.countdp(10) -- 0
print dbo.countdp(10.000) -- 0
print dbo.countdp(0) -- 0
print dbo.countdp(0.1234567) -- 7
print dbo.countdp(null) -- null
print dbo.countdp() --ERROR

*
* Modifications:
* Developer Name Date Brief description
* ------------------- ----------- ------------------------------------------------------------
*
********************************************************************************************************/

DECLARE @DecCount TINYINT

SELECT @DecCount = ( SELECT ISNULL(MAX(num), N.Num)
FROM Numbers
WHERE Num < 18
AND Num > N.Num
AND SUBSTRING(CAST(@decNumber AS VARCHAR),
Num, 1) NOT IN ( '0', '' )
) - ( Num )
FROM Numbers N
WHERE Num < LEN(CAST(@decNumber AS VARCHAR(18)))
AND SUBSTRING(CAST(@decNumber AS VARCHAR), Num, 1) = '.'
RETURN @DecCount

END
GO


SQL guy and Houston Magician
Sergiy
Sergiy
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5836 Visits: 11406

This function is about 100 times faster:

IF EXISTS (SELECT * FROM sysobjects WHERE name = N'DecimalPlaces')
DROP FUNCTION DecimalPlaces
GO

CREATE FUNCTION dbo.DecimalPlaces
(@A float)
RETURNS tinyint
AS
BEGIN
declare @R tinyint

IF @A IS NULL
RETURN NULL

set @R = 0

while @A - str(@A, 18 + @R, @r) <> 0
begin
SET @R = @R + 1
end

RETURN @R
END
GO


Sergiy
Sergiy
SSCertifiable
SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)SSCertifiable (5.8K reputation)

Group: General Forum Members
Points: 5836 Visits: 11406

No, it's actually much worse.

I tested my function against 16540 row table.
It returned result in 3..5 seconds (I'm not alone on that server )

Than I started same query but using function [dbo].[CountDP].
I's been 2 hours 50 minutes since then, it's still going.

So, there is a reminder: avoid referencing tables inside UDF!
Even if it's such "set based" table as Numbers.


Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45131 Visits: 39923

Old guys rule... 1 million conversions... 11 seconds... works for positive numbers, negative numbers, zero, and NULL...

DECLARE @Places INT
SELECT TOP 1000000 @Places = FLOOR(LOG10(REVERSE(ABS(SomeNumber)+1)))+1
FROM dbo.BigTest



--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
R Michael
R Michael
SSC Veteran
SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)SSC Veteran (271 reputation)

Group: General Forum Members
Points: 271 Visits: 275
Ouch! I guess next time I should get more information on how a solution might be used before posting a response!

Interesting solutions all around, I thought. Very slick guys!

SQL guy and Houston Magician
Vladan
Vladan
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1810 Visits: 751

Jeff,

are you sure the code you posted works? I tried it out of curiosity, and I'm getting some strange results... e.g. for number 99, depending on how I enter it, result is either 1 or -2.

DECLARE @Places INT
SELECT @Places = FLOOR(LOG10(REVERSE(ABS(cast (99 as float))+1)))+1
SELECT @places
-----------
1

(1 row(s) affected)

DECLARE @Places INT
SELECT @Places = FLOOR(LOG10(REVERSE(ABS(99.0000)+1)))+1
SELECT @places
-----------
-2

(1 row(s) affected)





Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45131 Visits: 39923
There would certainly appear to be a fly in the ointment ... thanks for the catch and sorry for the mistake folks... I'll see if I can fix it... Maybe I meant "Old guys drool"

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search