SQL Custom Percent conversion decimal function code _mssql

Source: Internet
Author: User
Tags numeric
Copy Code code as follows:

--cast and CONVERT functions percentage
DECLARE @dec Decimal (5,3), @var varchar (a), @hun decimal (5,1)
Set @dec =0.025
Set @hun = @dec *100
Set @var =cast (@hun as varchar (20)) + '% '
Select @var

Convert---Decimal to percent function getpercentagestring
---tu 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 (a), @hun decimal (10,1)
SET @hun = @dec *100
SET @var =cast (@hun as varchar (20)) + '% '
Return @var
End
Go
--Test data coating Geovin Du
SELECT [dbo]. [Getpercentagestring] (0.05)
SELECT [dbo]. [Getpercentagestring] (0.25)
SELECT [dbo]. [Getpercentagestring] (1.25)

--1. Whether the query string contains non-numeric characters
SELECT PATINDEX ('%[^0-9]% ', ' 1235x461 ')
SELECT PATINDEX ('%[^0-9]% ', ' 12350461 ')
--2. Whether numeric characters are included in the query string
SELECT PATINDEX ('%[0-9]% ', ' Suyllgoo ')
SELECT PATINDEX ('%[0-9]% ', ' suyllg0o ')


DECLARE @dec Decimal (10,4), @var varchar (a), @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-' Invalid data '
End


--conversion of percentages to fractional functions Getpercentagenumber
---tu 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 (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-' Invalid data '
End
Return @dec
End
Go
--Test data coating Geovin Du
SELECT [dbo]. [Getpercentagenumber] (' 5.23% ')
SELECT [dbo]. [Getpercentagenumber] ('%0.23 ')

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.