sql 自定义百分比转换小数函数代码
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')

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

AI Hentai Generator
Generate AI Hentai for free.

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



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. .

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

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

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.

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

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 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

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
