Share number formatting examples in SQL
Use sql statements to format double data, for example, only take out the two digits after the decimal point
. Main method
--Take the number before the decimal place, regardless of rounding
select left('30000.72234', charindex('.', '30000.72234')-1)
Among them: charindex(' .', '30000.72234') to get the position of the decimal point; -1 means everything before the decimal point. If you want to get n after the decimal point, just write +n. It's quite useful.
--Separate every three digits with commas, leave 2 decimal places, and do not consider rounding
select convert(varchar, cast(round(30000.72234,0) as money), 1)
--Separate every three digits with commas, leave no decimal places, consider rounding
select left(convert(varchar, cast(round(30000.72234,0) as money), 1) , charindex('.', convert( varchar, cast(round(30000.72234,0) as money), 1))-1)
二. Comparison of CAST and ROUND
1.
SELECT CAST('123.456' as decimal) will get 123 (the decimal point will be omitted).
If you want to get the two decimal places.
You need to change the above to
SELECT CAST('123.456' as decimal(38, 2))
===>123.46
It will be automatically rounded!
2.
SELECT ROUND(123.75633, 2, 1),
ROUND(123.75633, 2)
The two values obtained by the above SQL are different. The former one is: 123.75000, the latter one is: 123.76000.
Because the former has been truncated before rounding, leaving 2 digits after the decimal point.
The latter is not intercepted, and when rounded, it will naturally get 123.76000
Problem: It is necessary to calculate the ratio of the value queried in the Oracle database. SQL similar to the following:
Select Round((discount/Amount),2) from dual;
The numbers calculated in this way can meet most needs. But for numbers less than 1, a problem arises...data similar to .45 is displayed. The 0 in front of 0.45 is gone.
The following summarizes two methods to solve the problem of missing 0.
The first solution:
SELECT DECODE (TRUNC (1/100),0, REPLACE (1/100, '.', '0.'),TO_CHAR (1/100))
FROM DUAL;
You only need to replace 1/100 with the percentage you need.
The second solution:
SELECT RTrim(To_Char(1/100,'FM99999999990.9999'),'.') FROM dual;
The replacement method is the same as above.
Detailed comparison of the two methods:
The first formula is more complicated, but it can handle general numerical values.
The second one looks simple, but the number of digits in the formatted value must be greater than the number of digits in the calculation. For example, SELECT RTrim(To_Char(200,'FM90.9999'),'.') FROM dual; The displayed value is
##, because it has exceeded the formatting range.
I found out that the value of field a in table tb is 0.4286, what should I do? It can be converted to a percentage of 42.9% (keep one decimal place after the decimal point)
For example
select a from tb
0.4286
The result I want to get is
42.9%
SQL code
select cast(cast(a*100 as decimal(18,1)) as varchar) + '%' from tb
The above is the detailed content of Share number formatting examples in SQL. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











DOS command is a command line tool used in Windows operating system, which can be used to perform various system management tasks and operations. One of the common tasks is to format the hard drive, including the C drive. Formatting the C drive is a relatively dangerous operation because it will erase all data on the C drive and reinitialize the file system. Before performing this operation, make sure you have backed up important files and have a clear understanding of the impact that formatting will have on your computer. The following is formatted in the DOS command line

Nowadays, we will inevitably encounter some problems such as being unable to turn on the phone or lagging, such as system crash, but during use, mobile phones have become an indispensable part of our lives. We are often at a loss, and sometimes, there are no solutions to these problems. To help you solve cell phone problems, this article will introduce you to some methods of cell phone format recovery and restore your phone to normal operation. Back up data - protect important information, such as photos and contacts, from being lost during the formatting process. Before formatting your phone, the first thing to consider is to back up important data and files on your phone. To ensure data security, or choose to transfer files to a cloud storage service, you can back it up by connecting to a computer. Use the system's built-in recovery function - simple

Will formatting a laptop make it faster? If you want to format your Windows laptop but want to know if it will make it faster, this article will help you know the right answer to this question. Will formatting a laptop make it faster? There are many reasons why users format their Windows laptops. But the most common reason is slow performance or speed of your laptop. Formatting a laptop will completely delete all data stored on the C drive or the hard drive partition where Windows operating system is installed. Therefore, every user will think twice before taking this step, especially when it comes to the performance of the laptop. This article will help you understand whether formatting your laptop will speed it up. Formatting your laptop helps

HTML formatting method: 1. Use online HTML formatting tools; 2. Use the HTML formatting shortcut keys that come with the code editor, such as Shift + Alt + F in Visual Studio Code; 3. Use plug-ins, such as Sublime Text HTML/CSS/JS Prettify plug-in; 4. Use command line tools, such as HTML Tidy; 5. Manual formatting according to coding standards and habits.

In the process of using the Windows 10 operating system developed by Microsoft, many users are curious and confused about the new technology called Cortana. Cortana's official name in the Chinese context is "Cortana", which is actually a built-in function of the Windows 10 system. Cortana, an artificial intelligence (AIassistant) service program. Frequently asked questions and solutions. How to open Cortana and not respond. Solution steps. Chinese solution is not supported. How to put the search box into Cortana. What software is Cortana? Answer: "Cortana" It is a cloud platform personal intelligent assistant carefully built by Microsoft. It has two usage modes: login and non-login. When you are logged in

Json file formatting method Json (JavaScriptObjectNotation) is a lightweight data exchange format that is widely used for data transmission and storage in web applications and mobile applications. Json files store data in a structured manner, making it very convenient to parse and use data. However, sometimes we may encounter situations where the Json file format is disordered or difficult to read. In this case, we can use Json file formatting method to optimize Jso

Recommended laptops for dance majors 1. Recommended 2. Because dance majors need to frequently use computers for choreography, rehearsals, performances, etc., they need a laptop with better performance. Dance software and video editing software usually require high processing power and storage space, so choosing a laptop with higher configurations can better meet the needs of dance majors. 3. When choosing a professional dance laptop, you can consider the following aspects: - Processor: Choose a processor with strong performance, such as Intel Corei7 or AMD Ryzen7, to ensure that dance software and video editing software can run smoothly. -Memory: It is recommended to choose at least 16GB of memory to run multiple dance software and video editing software at the same time, and ensure that the system

On May 7, our mobile phone manufacturer officially announced that our company’s GTNeo6 launch conference is scheduled for May 9. GTNoe6 is positioned as a "performance storm", aiming to stir up the mid-range machine situation. In addition, this conference will also be the first AI digital human conference in the mobile phone industry. At that time, Realme Vice President, Global Marketing President, and China President Xu Qi will appear at the press conference in the form of a digital human. Digital man Xu Qi According to the official introduction, Realme GTNoe6, codenamed "Hurricane", is faster and stronger. It will challenge the strongest third-generation Snapdragon 8s flagship and the strongest product in its class. Recently, the Realme GTNeo6 was found to be directly on the e-commerce platform. Some core configurations were exposed, showing that the machine is not only equipped with a Snapdragon 8s processor, but also supports 120W flash charging.
