Click here to Skip to main content
16,010,876 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
SQL
DECLARE @Number NUMERIC(18,6)
SET @Number = '99.1230000'
SELECT  CAST(@Number AS NUMERIC(18,6))
               ,CONVERT(NUMERIC(18,6), @Number)
               ,REPLACE(RTRIM(REPLACE(@Number, '0', ' ')), ' ', '0') [REQUIRED VALUE]
               ,ISNULL(PARSENAME(REPLACE(RTRIM(REPLACE(@Number, '0', ' ')), ' ', '0'),1) ,0) [PARSE VALUE]
               ,LEN(CONVERT(VARCHAR,ISNULL(PARSENAME(REPLACE(RTRIM(REPLACE(@Number, '0', ' ')), ' ', '0'),1) ,0))) [PARSE VALUE LEN]
               , CASE LEN(CONVERT(VARCHAR,ISNULL(PARSENAME(REPLACE(RTRIM(REPLACE(@Number, '0', ' ')), ' ', '0'),1) ,0)))
                 WHEN 0 THEN CONVERT(NUMERIC(18,2),REPLACE(RTRIM(REPLACE(@Number, '0', ' ')), ' ', '0'))
                 WHEN 1 THEN CONVERT(NUMERIC(18,2),REPLACE(RTRIM(REPLACE(@Number, '0', ' ')), ' ', '0'))
                 ELSE REPLACE(RTRIM(REPLACE(@Number, '0', ' ')), ' ', '0')
                 END [FINAL VALUE]







currently i use this query as the requirment of my client

basically it is not comlete til now

i need when my client enter 123.012300
then i need to show 123.0123 only

and when he enter 123.0000
i need to show him 123.00

and when he enter 123.95001
then i need to show 123.95001

and when he enter 123.950010
then i need to show 123.95001 only

and when he enter 123.000200
then i need to show only 123.0002

how can i do this?
Posted
Comments
AmitGajjar 3-Dec-12 5:41am    
this is not the default behavior of any function, you need to create your own function to do this. us if condition along with sql string functions. hope you will do that.
prince_rumeel 3-Dec-12 5:46am    
can u give a short line of code
up i write a big code
help me in same senerio

use this,

SQL
Declare @myvalue varchar(50),
        @Price Varchar(50)

Set @Price = '9' --give input in this line

set @price = case when patindex('%.%',@Price)=0 then @price + '.00' else @price end

set @Myvalue = reverse(substring(@Price,patindex('%.%',@Price)+1,len(@Price)))


select case when len(substring(result,2,len(result)-2))=1 then result + '0' else result end as result from
(
    SELECT
    case
    When  patindex('%.%[1-9]%',@price) = 0 Then
        substring(@price,1,patindex('%.%',@price)-1 )+ '.' + '00'
    else
        substring(@price,1,patindex('%.%',@price)-1) + '.' +  Reverse(substring(@Myvalue,patindex('%[1-9]%',@Myvalue),len(@Myvalue)))
    END  as result
) as a


Happy Coding!
:)
 
Share this answer
 
v2
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900