Home Database Mysql Tutorial sql 自定义百分比转换小数函数代码

sql 自定义百分比转换小数函数代码

Jun 07, 2016 pm 06:02 PM
decimal percentage

sqlserver中自定义百分比转换小数函数,需要的朋友可以参考下。

代码如下:
--CAST 和 CONVERT 函数 Percentage
DECLARE @dec decimal(5,3), @var varchar(10),@hun decimal(5,1)
set @dec=0.025
set @hun=@dec*100
set @var=cast(@hun as varchar(20))+'%'
select @var

---小数转化为百分数函数 GetPercentageString
---涂聚文 Geovin Du
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetPercentageString]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[GetPercentageString]
GO
CREATE function GetPercentageString
(
@dec decimal(10,3)
)
returns varchar(20)
AS
BEGIN
DECLARE @var varchar(10),@hun decimal(10,1)
SET @hun=@dec*100
SET @var=cast(@hun as varchar(20))+'%'
RETURN @var
END
GO
--测试数据 涂聚文 Geovin Du
SELECT [dbo].[GetPercentageString] (0.05)
SELECT [dbo].[GetPercentageString] (0.25)
SELECT [dbo].[GetPercentageString] (1.25)

--1. 查询字符串中是否包含非数字字符
SELECT PATINDEX('%[^0-9]%', '1235X461')
SELECT PATINDEX('%[^0-9]%', '12350461')
--2. 查询字符串中是否包含数字字符
SELECT PATINDEX('%[0-9]%', 'SUYLLGoO')
SELECT PATINDEX('%[0-9]%', 'SUYLLG0O')


DECLARE @dec decimal(10,4), @var varchar(10),@hun decimal(10,4),@I INT,@K INT,@S VARCHAR(20)
SET @var='5.23%'
--SELECT @K=LEN(@var)
--SELECT @S=LEFT(@var,@K-1)
SELECT @S=RIGHT(@var,1)
--SELECT @S
SELECT @I=CHARINDEX('%',@S)
IF @I>0
BEGIN
SET @var=REPLACE(@var,'%','')
SET @hun=CAST(@var AS decimal(10,4))
SET @dec=@hun/100
SELECT @dec
END
ELSE
BEGIN
SELECT @dec=NULL --'无效数据'
END


--百分数转化为小数函数 GetPercentageNumber
---涂聚文 Geovin Du
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[GetPercentageNumber]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[GetPercentageNumber]
GO
CREATE function GetPercentageNumber
(
@var varchar(10)
)
returns decimal(10,4)
AS
BEGIN
DECLARE @dec decimal(10,4), @hun decimal(10,4),@I INT,@K INT,@S VARCHAR(20)
SELECT @S=RIGHT(@var,1)
--SELECT @S
SELECT @I=CHARINDEX('%',@S)
IF @I>0
BEGIN
SET @var=REPLACE(@var,'%','')
SET @hun=CAST(@var AS decimal(10,4))
SET @dec=@hun/100
--SELECT @dec
END
ELSE
BEGIN
SELECT @dec=NULL --'无效数据'
END
RETURN @dec
END
GO
--测试数据 涂聚文 Geovin Du
SELECT [dbo].[GetPercentageNumber] ('5.23%')
SELECT [dbo].[GetPercentageNumber] ('%0.23')
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to convert integer to decimal in javascript How to convert integer to decimal in javascript Nov 03, 2021 pm 05:59 PM

In JavaScript, you can use the toFixed() function to convert an integer into a decimal. This function can convert an integer into a number with a specified number of decimal places; the syntax is "number.toFixed(x)", and the parameter "x" specifies the number of decimal places. .

How to convert fraction to percentage in Python? How to convert fraction to percentage in Python? Aug 25, 2023 pm 01:25 PM

Fractions and percentages are two basic mathematical concepts used in various fields such as finance, data analysis, and statistics. Converting a fraction to a percentage is a simple but necessary operation that allows us to represent fractional values ​​in a more understandable way. Python, one of the most popular programming languages, offers several methods for converting fractions to percentages, and understanding these methods is crucial for anyone working with data in Python. This article takes an in-depth look at converting fractions to percentages in Python. We'll explore different ways to do this and provide examples to help you understand each method. Whether you are a beginner or an experienced Python programmer, this article gives you the key to doing this in Python

Python program to get percentage of word frequency Python program to get percentage of word frequency Sep 08, 2023 pm 06:29 PM

In this article, we will learn how to get word frequency as a percentage in Python. Suppose we have obtained a list of string inputs. Now, we will find the percentage of each word in the given list of input strings. The formula (OccurrenceofXword/Totalwords)*100 uses the sum(), Counter(), join() and split() functions. Use the join(), split() and count() functions. Use the countOf() function of the operator module. Method 1: Use sum(), Counter(), join() and split() functions join() is Py

How does php handle decimals? Introduction to various methods How does php handle decimals? Introduction to various methods Mar 28, 2023 pm 03:45 PM

PHP is one of the most popular programming languages ​​for web development. In PHP, the processing of decimals is very important, because in actual programming work, we often need to perform precise operations on decimals. This article will introduce several ways to deal with decimals in order to perform decimal operations more efficiently.

Harnessing the PHP BCMath Extension: Opening the Door to High-Precision Mathematics Harnessing the PHP BCMath Extension: Opening the Door to High-Precision Mathematics Feb 23, 2024 am 09:22 AM

The PHPBCMath extension is a powerful tool in the PHP standard library. It provides a rich function library that can handle complex calculations such as large numbers, decimals, and fractions. Compared with PHP's built-in mathematical operators and functions, BCMath extensions can provide higher precision and a wider range of operations to meet the needs of high-precision mathematical calculations in various fields such as science, engineering, and finance. In order to use the BCMath extension, you need to include the following code in your PHP script: Among them, the bcscale() function is used to set the number of digits after the decimal point. The default value is 0, which means no decimals are retained. You can adjust this value as needed to control calculation accuracy

Detailed explanation of linux top command Detailed explanation of linux top command Feb 20, 2024 am 09:20 AM

Press 1 to see the usage of each CPU. The first line is the current time. The running time is the currently logged in user. Load balancing (1 minute, 5 minutes, 10 minutes). The loadaverage data checks the number of active processes every 5 seconds and calculates the value. If the number divided by the number of logical CPUs exceeds 5, the system is overloaded. The second line has a total of 248 processes, 1 running, 247 sleeping, 0 stopped, 0 zombie processes. The third line us (userspace): the percentage of cpu occupied by user space sy (sysctl): the percentage of cpu occupied by kernel space ni()— The percentage of CPU occupied by the process that has changed the priority id (idolt): idle CPU percentage wa (wait): IO waiting occupied CP

How to enable battery percentage display on iPhone? How to enable battery percentage display on iPhone? May 08, 2023 pm 12:07 PM

How to Show Battery Percentage on iPhone Previously, you had to swipe down to open Control Center to find the exact percentage of remaining battery power. However, the option to see the precise battery percentage on the top edge of the lock screen is back. To show battery percentage on your iPhone: Open the Settings app from your iPhone home screen. Swipe down the Settings menu and tap Battery from the list. Next, switch the battery percentage button to the on position. Finally, you'll see the exact percentage of remaining power above the battery icon in the upper right corner of the screen. When the feature is enabled, percentage numbers will appear when you browse the Lock screen, Home screen, and most apps. Can't see battery percentage? If you don't see the

One in five PCs now has Windows 11 installed, but adoption appears to be slowing One in five PCs now has Windows 11 installed, but adoption appears to be slowing Apr 30, 2023 pm 05:10 PM

Microsoft's Windows 11 continues to grow, as the latest February 2022 data from an AdDuplex report shows. The operating system is quickly approaching the 20% figure, meaning roughly one in five PCs now runs Windows 11. The public version of Windows 11, Windows 1121H2, is now at 19.3%. Meanwhile, the rate of insiders flying various preview builds is now 0.3%. This brings Windows 11's total share to 19.6%, and by March, this percentage share may exceed 20%. Windows 11 and many Wind

See all articles