| Author |
Topic |
|
abhi143
Starting Member
32 Posts |
Posted - 2006-06-17 : 06:00:43
|
| have three STRING data type fields...suppose in first field the value is = 45.125455and in second field the value is = .4545Now the multiplication of first and second field will come in third field.and the third field, result is = 20.5095192975Now i want to display in third field only = 509519(After decimal..up to 6 digit) by using stored Procedure |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-06-17 : 08:16:54
|
quote: Originally posted by abhi143 Now i want to display in third field only = 509519(After decimal..up to 6 digit) by using stored Procedure
What you most likely would want to use is-- prepare test datadeclare @num table (i varchar(50))insert @num select '20.509519297500000'-- do the workSELECT i, substring(i, charindex('.', i) + 1, 6)FROM @numPlease not that this does not do a correct round off. It clips the string.Peter LarssonHelsingborg, Sweden |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-06-17 : 13:35:06
|
| [code]select num, dec =floor((num-floor(num))*1000000)from (select num = 45.125455*.4545) a[/code]Results:[code]num dec --------------- ---------------------------------- 20.5095192975 509519(1 row(s) affected)[/code]CODO ERGO SUM |
 |
|
|
abhi143
Starting Member
32 Posts |
Posted - 2006-06-19 : 01:58:14
|
| Thanks michael..it works |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-06-19 : 02:47:31
|
quote: Originally posted by abhi143 Thanks michael..it works
Really? Michael and Abhi, try Michael's solution with a negative number or a product having less than 6 decimal digits.select num, dec = floor((num - floor(num)) * 1000000)from (select num = -45.125455 * .4545 union all select 4.012 * .5 union all select -4.012 * .5) anum dec-------------- -------20.5095192975 4904802.0060000000 6000-2.0060000000 994000 and compare to-- prepare test datadeclare @num table (i varchar(50))insert @num select '20.509519297500000' union allselect '20' union allselect '-13' union allselect '15.' union allselect '2.006' union allselect '.05' union allselect '234.1' union allselect '-69.09' union allselect '-20.509519297500000'-- do the workSELECT i, case when charindex('.', i) = 0 then '' else substring(i, charindex('.', i) + 1, 6) endFROM @numi (no column name)------------------- ----------------20.509519297500000 50951920-1315..05 05234.1 12.006 006-69.09 09-20.509519297500000 509519Peter LarssonHelsingborg, Sweden |
 |
|
|
abhi143
Starting Member
32 Posts |
Posted - 2006-06-22 : 02:14:42
|
| yes..peso...i found bug in Michael case..after applying some other data..Now i have very much comfortable in u r case.thanks both of u |
 |
|
|
abhi143
Starting Member
32 Posts |
Posted - 2006-06-22 : 04:27:51
|
| still iam facing problem in Peso and Michael Script..In Peso.. script..it will not display up to 6 digit...for example- 2.006 006(Wrong)it should be 2.006 006000(correct)Plz help me |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-06-22 : 06:45:47
|
quote: Originally posted by abhi143 for example- 2.006 006 (Wrong)it should be 2.006 006000 (correct)Plz help me
Just add some zeros to the end of the code!-- do the workSELECT i, case when charindex('.', i) = 0 then '000000' else left(substring(i, charindex('.', i) + 1, 6) + '000000', 6) endFROM @numIn your original posting, you wrotequote: (After decimal..up to 6 digit)
You should have written always six digits.Peter LarssonHelsingborg, Sweden |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2006-06-22 : 06:55:28
|
Here is a generic solution that allows you to choose any number of digits.-- prepare test datadeclare @num table (i varchar(50))insert @num select '20.509519297500000' union allselect '20' union allselect '-13' union allselect '15.' union allselect '2.006' union allselect '.05' union allselect '234.1' union allselect '-69.09' union allselect '-20.509519297500000'declare @digits tinyint-- do the workselect @digits = 6SELECT i, case when charindex('.', i) = 0 then replicate('0', @digits) else left(substring(i, charindex('.', i) + 1, @digits) + replicate('0', @digits), @digits) endFROM @numPeter LarssonHelsingborg, Sweden |
 |
|
|
abhi143
Starting Member
32 Posts |
Posted - 2006-06-23 : 05:13:50
|
| can we create a function for that logic..??????? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-23 : 05:15:10
|
quote: Originally posted by abhi143 can we create a function for that logic..???????
Yes. Why not. KH |
 |
|
|
abhi143
Starting Member
32 Posts |
Posted - 2006-06-23 : 05:16:58
|
| A function, in which user have a flexibility that as per his choice he can take the digit after decimal..i.e it can be 4 digit or 6 digit or 2 digit |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-23 : 05:30:30
|
quote: Originally posted by abhi143 A function, in which user have a flexibility that as per his choice he can take the digit after decimal..i.e it can be 4 digit or 6 digit or 2 digit
Just create the function with 2 input parameters, the 1st is the varchar and the 2nd is the @digits KH |
 |
|
|
abhi143
Starting Member
32 Posts |
Posted - 2006-06-23 : 05:39:48
|
| can you give the example for that. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-06-23 : 05:53:12
|
it is very simple. I just take Peso's code and change the column i into a variable @i-- Check if the function exists, drop itif exists (select * from sysobjects where xtype = 'FN' and name = 'fn_test') drop function dbo.fn_testgo-- Create the functioncreate function dbo.fn_test( @i varchar(50), @digits int)returns varchar(10)asbegin return ( select case when charindex('.', @i) = 0 then replicate('0', @digits) else left(substring(@i, charindex('.', @i) + 1, @digits) + replicate('0', @digits), @digits) end )endgo-- prepare test datadeclare @num table (i varchar(50))insert @num select '20.509519297500000' union allselect '20' union allselect '-13' union allselect '15.' union allselect '2.006' union allselect '.05' union allselect '234.1' union allselect '-69.09' union allselect '-20.509519297500000'declare @digits tinyint-- do the workselect @digits = 6SELECT i, case when charindex('.', i) = 0 then replicate('0', @digits) else left(substring(i, charindex('.', i) + 1, @digits) + replicate('0', @digits), @digits) end, dbo.fn_test(i, @digits) as [fn_test]FROM @num KH |
 |
|
|
abhi143
Starting Member
32 Posts |
Posted - 2006-06-23 : 06:13:32
|
| thanks KHTAN |
 |
|
|
|