SQL Server 2008 R2 幾個自訂函數

來源:互聯網
上載者:User

標籤:style   blog   color   io   ar   div   cti   log   sp   

  

 1 --字串轉成時間 2  GO 3 CREATE FUNCTION [dbo].[JBFS_MS_VARCHAR_TO_DATETIME] (@strDate varchar(50) 4 ) 5 RETURNS datetime 6 AS 7 BEGIN 8   declare @tmpTm datetime 9   set @tmpTm =CONVERT(datetime,@strDate);10   RETURN @tmpTm;11 END12 --時間轉成字串13 GO14 CREATE FUNCTION [dbo].[JBFS_MS_DATETIME_TO_VARCHAR] (@date datetime,@inputNum int15 )16 RETURNS varchar(50)17 AS18 BEGIN19   declare @strTm varchar(50)20   set @strTm =CONVERT(varchar(50),@date,@inputNum);21   RETURN @strTm;22 END23 --貨幣轉換千分位字串24 25 CREATE function [dbo].[JBFS_MS_MONEY_TO_VARCHAR](@dec money, @n int)26 returns varchar(32) as27 begin28     declare @str varchar(32), @len int, @left varchar(32), @right varchar(32),@end varchar(32)29     if @n!=‘0‘30     begin31         --四捨五入32        set @str= round(@dec,@n)33        select @left=left(@str,charindex(‘.‘,@str)-1),@len=len(@left)-234        while @len>135        begin36            select @left=stuff(@left,@len,0,‘,‘), @len=@len-337        end38        select @right=left(stuff(@str,1,charindex(‘.‘,@str),‘‘), @n),@len=439        while @len <=len(@right)40        begin41            select @right=stuff(@right,@len,0,‘,‘), @len=@len+442        end43        set @end= @left+‘.‘+@right44     end45     else46     begin47        set @str= round(@dec,@n)48        select @left=left(@str,charindex(‘.‘,@str)-1),@len=len(@left)-249        while @len>150        begin51            select @left=stuff(@left,@len,0,‘,‘), @len=@len-352        end53        select @right=left(stuff(@str,1,charindex(‘.‘,@str),‘‘), @n),@len=454        while @len <=len(@right)55        begin56            select @right=stuff(@right,@len,0,‘,‘), @len=@len+457        end58        set @end= @left59     end60     return @end61 End62 --字串轉換成money63 CREATE function [dbo].[JBFS_MS_VARCHAR_TO_MONEY](@dec varchar(50))64 returns money 65    AS66 BEGIN67     DECLARE @mon MONEY68     SET @mon =cast(@dec as money)69  return @mon70 END

 

SQL Server 2008 R2 幾個自訂函數

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.