| Author |
Topic |
|
Axxib
Starting Member
6 Posts |
Posted - 2008-08-19 : 11:31:26
|
| Hi AllCan someone please help: I need to be able to count the number of énding zero's in a decimal field (before the decimal point), for example if field 'Amount' has the values 10000.11, 11000.11, 20000.11 and 22000.11 I need to be able to specify all values where the number of ending zero's is 4 and get the result 10000.11 and 20000.11.Is there a way in sql that this can be acheived? |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-19 : 11:50:32
|
[code]DECLARE @Sample TABLE ( Data DECIMAL(10, 2) )INSERT @SampleSELECT 30300.10 UNION ALLSELECT 10000.11 UNION ALLSELECT 11000.11 UNION ALLSELECT 20000.11 UNION ALLSELECT 90000 UNION ALLSELECT 50000.00 UNION ALLSELECT 22000.11SELECT DataFROM @SampleWHERE Data LIKE '%' + REPLICATE('0', 4) + '.%'[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-19 : 11:51:33
|
| SELECT Amount FROM YourTable WHERE SUBSTRING(REVERSE(CAST(Amount AS varchar(50))),CHARINDEX('.',REVERSE(CAST(Amount AS varchar(50))))+1,@number)=REPLICATE('0',@number)where @number is number of zeroes you want. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-08-19 : 11:55:07
|
quote: Originally posted by Peso
DECLARE @Sample TABLE ( Data DECIMAL(10, 2) )INSERT @SampleSELECT 30300.10 UNION ALLSELECT 10000.11 UNION ALLSELECT 11000.11 UNION ALLSELECT 20000.11 UNION ALLSELECT 90000 UNION ALLSELECT 50000.00 UNION ALLSELECT 22000.11SELECT DataFROM @SampleWHERE Data LIKE '%' + REPLICATE('0', 4) + '.%' E 12°55'05.25"N 56°04'39.16"
Wont this return all values that have n or more ending zeroes where n is what we are looking for? |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2008-08-19 : 11:58:12
|
Maybe this? DECLARE @Yak TABLE(Val DECIMAL(18,8))DECLARE @DigitCount INTSET @DigitCount = 4INSERT @YakSELECT 10000.11UNION ALL SELECT 11000.11UNION ALL SELECT 20000.11 UNION ALL SELECT 22000.11 SELECT ValFROM @YakWHERE CAST(Val AS INT) % POWER(10, @DigitCount) = 0 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-19 : 11:58:41
|
quote: Originally posted by visakh16 Wont this return all values that have n or more ending zeroes where n is what we are looking for?
You are right. So does your example.DECLARE @Sample TABLE ( Data DECIMAL(10, 2) )INSERT @SampleSELECT 30300.10 UNION ALLSELECT 10000.11 UNION ALLSELECT 11000.11 UNION ALLSELECT 20000.11 UNION ALLSELECT 10.12 UNION ALLSELECT 100000.1 UNION ALLSELECT 90000 UNION ALLSELECT 50000.00 UNION ALLSELECT 22000.11-- PesoSELECT DataFROM @SampleWHERE Data LIKE '%[^0]' + REPLICATE('0', 4) + '.%'--LampreySELECT DataFROM @SampleWHERE CAST(Data AS INT) % POWER(10, 4) = 0-- VisakhSELECT DataFROM @SampleWHERE SUBSTRING(REVERSE(CAST(Data AS VARCHAR(50))), CHARINDEX('.', REVERSE(CAST(Data AS VARCHAR(50)))) + 1, 4) = REPLICATE('0', 4) E 12°55'05.25"N 56°04'39.16" |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2008-08-19 : 13:54:37
|
One more way:declare @sample table (data decimal(30, 2) )insert @sampleselect 4444433303030000000.10 union allselect 4444433303030030000.10 union allselect 30303.10 union allselect 30300.10 union allselect 10000.11 union allselect 11000.11 union allselect 20000.11 union allselect 10.12 union allselect 100000.1 union allselect 90000 union allselect 50000.00 union allselect 22000.11print 'Show trailing zero count'select Data, Trailing_Zeros = len(floor(Data)) - len(convert(bigint,reverse(floor(Data))))from @sampleprint 'Select where trailing zero count = 4'select Datafrom @samplewhere 4 = len(floor(Data)) - len(convert(bigint,reverse(floor(Data))))Results:Show trailing zero countData Trailing_Zeros -------------------------------- -------------- 4444433303030000000.10 74444433303030030000.10 430303.10 030300.10 210000.11 411000.11 320000.11 410.12 1100000.10 590000.00 450000.00 422000.11 3(12 row(s) affected)Select where trailing zero count = 4Data -------------------------------- 4444433303030030000.1010000.1120000.1190000.0050000.00(5 row(s) affected) CODO ERGO SUM |
 |
|
|
Axxib
Starting Member
6 Posts |
Posted - 2008-08-19 : 15:35:02
|
| Thanks for all your help guys! |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-08-20 : 03:53:53
|
Another way similar to MVJ's methodselect data from( select data,cast(data as int) as data_int from @sample) as twhere len(data_int)-len(replace(data_int,'0','')) =4 MadhivananFailing to plan is Planning to fail |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-20 : 05:33:57
|
Madhi, what happens to the value "100100.11"? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-20 : 06:28:53
|
And this is not a sample data provided by OP, but what happens to values less than zero?DECLARE @Sample TABLE ( Data DECIMAL(10, 2) )INSERT @SampleSELECT 30300.10 UNION ALLSELECT 10000.11 UNION ALLSELECT 11000.11 UNION ALLSELECT 20000.11 UNION ALLSELECT 10.12 UNION ALLSELECT 100000.1 UNION ALLSELECT 90000 UNION ALLSELECT 50000.00 UNION ALLSELECT 300300.0 UNION ALLSELECT -70000.0 UNION ALLSELECT 22000.11-- PesoSELECT DataFROM @SampleWHERE Data LIKE '%[^0]' + REPLICATE('0', 4) + '.%'--LampreySELECT DataFROM @SampleWHERE CAST(Data AS INT) % POWER(10, 4) = 0-- VisakhSELECT DataFROM @SampleWHERE SUBSTRING(REVERSE(CAST(Data AS VARCHAR(50))), CHARINDEX('.', REVERSE(CAST(Data AS VARCHAR(50)))) + 1, 4) = REPLICATE('0', 4)-- MVJSELECT DataFROM @SampleWHERE 4 = LEN(FLOOR(Data)) - LEN(CONVERT(BIGINT, REVERSE(FLOOR(Data))))-- MadhiSELECT DataFROM ( SELECT Data, CAST(Data AS INT) AS Data_INT FROM @Sample ) AS tWHERE LEN(Data_INT) - LEN(REPLACE(Data_INT, '0', '')) = 4 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-08-20 : 07:27:48
|
quote: Originally posted by Peso Madhi, what happens to the value "100100.11"? E 12°55'05.25"N 56°04'39.16"
Then my where condition would bewhere data_int/10000.0 not like '%0.%' and right(data_int/10000.0,6)=0and data_int/10000.0 like '%.0%'MadhivananFailing to plan is Planning to fail |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-20 : 09:51:27
|
Oh! Multiple WHERE clause... Your second suggestion is about 3 times faster than my first suggestion. Very good!But my second suggestion is about 4 times faster than your second suggestion.Here is my second suggestion, which is about 12 times faster than my first suggestionSELECT DataFROM @SampleWHERE CAST(Data AS INT) % 10000 = 0 AND (CAST(Data AS INT) / 10000) % 10 > 0 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2008-08-20 : 10:28:49
|
The reason I did it by computing the number of trailing zeros was that I thought it was more flexible. I thought there was a possibility that the OP had not stated the requirements exactly.-- as specifiedwhere 4 = len(floor(Data)) - len(convert(bigint,reverse(floor(Data))))-- 4 or morewhere 4 <= len(floor(Data)) - len(convert(bigint,reverse(floor(Data))))-- 1 or 6where 4 <= len(floor(Data)) - len(convert(bigint,reverse(floor(Data)))) in (1,6) CODO ERGO SUM |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-08-21 : 02:54:47
|
quote: Originally posted by Peso Oh! Multiple WHERE clause... Your second suggestion is about 3 times faster than my first suggestion. Very good!But my second suggestion is about 4 times faster than your second suggestion.Here is my second suggestion, which is about 12 times faster than my first suggestionSELECT DataFROM @SampleWHERE CAST(Data AS INT) % 10000 = 0 AND (CAST(Data AS INT) / 10000) % 10 > 0 E 12°55'05.25"N 56°04'39.16"
This eliminates negative numbersSecond condition should be(CAST(Data AS INT) / 10000) % 10 <> 0MadhivananFailing to plan is Planning to fail |
 |
|
|
|