--CAST 和 CONVERT 函数 PercentageDECLARE @dec decimal(5,3), @var varchar(10),@hun decimal(5,1)set @dec=0.025set @hun=@dec*100set @var=cast(@hun as varchar(20))+'%'select @var---小數轉化為百分數函數 GetPercentageString---塗聚文 Geovin Duif 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]GOCREATE function GetPercentageString( @dec decimal(10,3))returns varchar(20)ASBEGIN DECLARE @var varchar(10),@hun decimal(10,1) SET @hun=@dec*100 SET @var=cast(@hun as varchar(20))+'%' RETURN @varENDGO--測試數據 塗聚文 Geovin DuSELECT [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 @SSELECT @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 ENDELSE BEGIN SELECT @dec=NULL --'無效數據' END --百分數轉化為小數函數 GetPercentageNumber---塗聚文 Geovin Duif 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]GOCREATE function GetPercentageNumber( @var varchar(10))returns decimal(10,4)ASBEGIN 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 ENDELSE BEGIN SELECT @dec=NULL --'無效數據' ENDRETURN @decENDGO--測試數據 塗聚文 Geovin DuSELECT [dbo].[GetPercentageNumber] ('5.23%')SELECT [dbo].[GetPercentageNumber] ('%0.23')