Format floating point numbers in SQL

Source: Internet
Author: User

The database designs fields as decimal, so each stored field has five valid numbers ~ Example: 50000.00000
In my previous task, we set two numbers and two conditions in four fields to 0 <x <= 100, all numbers have five valid decimals. The SQL statement is as follows:

Select Factorname, Rs. factorid,
Rs. rewardstandardid, Rs. ruletemplateid,
Case   ABS (Conditionlowerbound) When   999999999   Then   ''   Else   Convert ( Varchar , Conditionlowerbound) +
Case   Convert ( Varchar , Conditionlowerboundcontain) When   0   Then   ' < '   When   1   Then   ' <= '   End
End   + ' X ' +   Case   ABS (Conditionupperbound) When   999999999   Then   ''   Else   +
Case   Convert ( Varchar , Conditionupperboundcontain) When   0   Then   ' < '   When   1   Then   ' <= ' End +
Convert ( Varchar , Conditionupperbound) End   As Expression, standardvalue
From Rewardstandard rs Left   Join Rewardcomputingfactor RF On Rs. factorid = RF. factorid
Where   1 = 1

The output is as follows:

Obviously, the expression is ugly ~ Therefore, the current task should change it to a normal number:
I searched the internet and found that there were no prepared answers. Maybe the university thought it was very simple, or I couldn't express it clearly. For example, I do not know how to better express this title. Haha ~

Try the following methods: Select Right ( 1000 + 4555 , 3 )

Select Stuff ( Convert ( Varchar ,( 1000 + 4655 )), 1 , 1 , '' )

Select Replicate ( ' 0 ' , 3 - Len ( Convert ( Varchar , 5 ))) + Convert ( Varchar , 5 )


Select   Right ( 1000 + Conditionupperbound, 10 ) From DBO. rewardstandard
Select   *   From Rewardstandard

Select To_char ( 12.1 , ' 99.00 ' ) From Rewardstandard


Declare   @ Jj   Decimal ( 5 , 2 )
Set   @ Jj = 123.00
Select   Convert ( Varchar ( 30 ), @ Jj )

Select   Convert ( Varchar , Convert ( Float , Conditionupperbound )) From DBO. rewardstandard

I found that there is a good method to take the last few digits after formatting, for example, 5 format to 005, select right (1000 + 5, 3), huh, huh, isn't it very easy?

My problem has not been solved yet ~ Finally, I accidentally combined and found a solution. First, convert the number to float and then varchar, as shown below:Select Convert(Varchar,Convert(Float, Conditionupperbound ))FromDBO. rewardstandard

So, My SQL is like this ~ Select Factorname, Rs. factorid,
Rs. rewardstandardid, Rs. ruletemplateid, billingmonth, Rs. basetableid, Rs. factorid,
Case   ABS (Conditionlowerbound) When   999999999   Then   ''   Else   Convert ( Varchar , Convert ( Float , Conditionlowerbound )) +
Case   Convert ( Varchar , Conditionlowerboundcontain) When   0   Then   ' < '   When   1   Then   ' <= '   End
End   + ' X ' +   Case   ABS (Conditionupperbound) When   999999999   Then   ''   Else   +
Case   Convert ( Varchar , Convert ( Float , Conditionupperboundcontain )) When   0   Then   ' < '   When   1   Then   ' <= ' End +
Convert ( Varchar , Convert ( Float , Conditionupperbound )) End   As Expression, Convert ( Varchar , Convert ( Float , Standardvalue ))
From Rewardstandard rs Left   Join Rewardcomputingfactor RF On Rs. factorid = RF. factorid
Where   1 = 1

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.