sql 自定义格式DateTime SQL函数 - 模仿.NET样式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql 自定义格式DateTime SQL函数 - 模仿.NET样式相关的知识,希望对你有一定的参考价值。

/****** Object:  UserDefinedFunction [dbo].[format_date]    Script Date: 05/12/2009 23:19:35 ******/  
 IF     EXISTS   (SELECT   *   FROM sys.objects WHERE object_id   =   OBJECT_ID(N   '[dbo].[format_date]'   )   AND    TYPE    IN   (N   'FN'   , N   'IF'   , N   'TF'   , N   'FS'   , N   'FT'   ))  
 DROP   FUNCTION [dbo].[format_date]  
 GO  

 /****** Object:  UserDefinedFunction [dbo].[format_date]    Script Date: 05/12/2009 23:19:18 ******/  
 SET   ANSI_NULLS   ON  
 GO  

 SET   QUOTED_IDENTIFIER   ON  
 GO  

 SET   CONCAT_NULL_YIELDS_NULL OFF  
 GO  
 CREATE   function [dbo].[format_date] (@inputdate datetime ,@format varchar(500))  
 returns varchar(500)  
 AS  
 BEGIN  
 DECLARE   @   YEAR   varchar(4)       --YYYY  
 DECLARE   @shortyear varchar(4)      --Yr  
 DECLARE   @quarter varchar(4)       --QQ  
 DECLARE   @   MONTH   varchar(2)       --MM  
 DECLARE   @week varchar(2)       --WW  
 DECLARE   @   DAY   varchar(2)        --DD  
 DECLARE   @24hours varchar(2)       --24HH  
 DECLARE   @12hours varchar(2)       --HH  
 DECLARE   @minutes varchar(2)       --MI  
 DECLARE   @seconds varchar(2)       --SS  
 DECLARE   @milliseconds varchar(3)     --MS  
 DECLARE   @microseconds varchar(6)     --MCS  
 DECLARE   @nanoseconds varchar(9)      --NS  
 DECLARE   @dayname varchar(15)      --DAY  
 DECLARE   @monthname varchar(15)      --MONTH  
 DECLARE   @shortmonthname varchar(15)        --MON  
 DECLARE   @AMPM  varchar(15)      --AMPM  
 DECLARE   @TZ  varchar(15)          --TZ  
 DECLARE   @UNIXPOSIX  varchar(15)     --UNIXPOSIX  
              --UCASE  
              --LCASE  

 DECLARE   @formatteddate varchar(500)    

 --Assign current date and time to  
 IF (@inputdate   IS    NULL    OR   @inputdate   =   ''   )  
 BEGIN  
 SET   @inputdate   =   getdate()  
 END  

 IF (@format   IS    NULL    OR   @format   =   ''   )  
 BEGIN  
 SET   @format   =   'YYYY-MM-DD 12HH:MI:SS AMPM'  
 end  

 --set all values  

 SET   @   YEAR          =    CONVERT   (varchar(4),   YEAR   (@inputdate))  
 SET   @shortyear       =    RIGHT   (@   YEAR   ,2)  
 SET   @quarter      =    CONVERT   (varchar(1),datepart(QQ,(@inputdate)))  
 SET   @   MONTH          =    RIGHT   (   '0'   +   CONVERT   (varchar(2),   MONTH   (@inputdate)),2)  
 SET   @week       =    RIGHT   (   '0'   +   CONVERT   (varchar(2),datepart(ww,(@inputdate))),2)  
 SET   @   DAY          =    RIGHT   (   '0'   +   CONVERT   (varchar(2),   DAY   (@inputdate)),2)  
 SET   @24hours         =    RIGHT   (   '0'   +   CONVERT   (varchar(2),datepart(hh,@inputdate)),2)  
 SET   @TZ              =    CONVERT   (varchar(10),datename(TZ,   CONVERT   (varchar(20),@inputdate)))  
 SET   @UNIXPOSIX       =    CONVERT   (varchar(15),datediff(ss,   CONVERT   (datetime,   '01/01/1970 00:00:000'   ),@inputdate))  

 IF datepart(hh,@inputdate)   >   12  
 BEGIN  
 SET   @12hours         =    RIGHT   (   '0'   +   CONVERT   (varchar(2),datepart(hh,@inputdate))   -   12,2)  
 end  
 ELSE  
 BEGIN  
 SET   @12hours         =    RIGHT   (   '0'   +   CONVERT   (varchar(2),datepart(hh,@inputdate)) ,2)  
 end  

 IF datepart(hh,@inputdate)   >   11  
 BEGIN  
 SET   @AMPM   =   'PM'  
 end  
 ELSE  
 BEGIN  
 SET   @AMPM   =   'AM'  
 end  

 SET   @minutes         =    RIGHT   (   '0'   +   CONVERT   (varchar(2),datepart(n,@inputdate)),2)  
 SET   @seconds         =    RIGHT   (   '0'   +   CONVERT   (varchar(2),datepart(ss,@inputdate)),2)  
 SET   @milliseconds   =    CONVERT   (varchar(3),datepart(ms,@inputdate))  
 SET   @microseconds   =    CONVERT   (varchar(6),datepart(mcs,@inputdate))  
 SET   @nanoseconds     =    CONVERT   (varchar(9),datepart(ns,@inputdate))  
 SET   @dayname         =   datename(weekday,@inputdate)  
 SET   @monthname       =   datename(mm,@inputdate)  
 SET   @shortmonthname   =    LEFT   (datename(mm,@inputdate),3)  
 SET   @formatteddate   =   @format  
 SET   @formatteddate   =   replace(@formatteddate,   'MONTH'   ,@monthname)  
 SET   @formatteddate   =   replace(@formatteddate,   'MON'   ,@shortmonthname)  
 SET   @formatteddate   =   replace(@formatteddate,   'AMPM'   ,@AMPM)  

 SET   @formatteddate   =   replace(@formatteddate,   'YYYY'   ,@   YEAR   )  
 SET   @formatteddate   =   replace(@formatteddate,   'Yr'   ,@shortyear)  
 SET   @formatteddate   =   replace(@formatteddate,   'QQ'   ,@quarter)  
 SET   @formatteddate   =   replace(@formatteddate,   'WW'   ,@week)  
 SET   @formatteddate   =   replace(@formatteddate,   'MM'   ,@   MONTH   )  
 SET   @formatteddate   =   replace(@formatteddate,   'DD'   ,@   DAY   )  
 SET   @formatteddate   =   replace(@formatteddate,   '24HH'   ,@24hours)  
 SET   @formatteddate   =   replace(@formatteddate,   '12HH'   ,@12hours)  
 SET   @formatteddate   =   replace(@formatteddate,   'Mi'   ,@minutes)  
 SET   @formatteddate   =   replace(@formatteddate,   'SS'   ,@seconds)  
 SET   @formatteddate   =   replace(@formatteddate,   'MS'   ,@milliseconds)  
 SET   @formatteddate   =   replace(@formatteddate,   'MCS'   ,@microseconds)  
 SET   @formatteddate   =   replace(@formatteddate,   'NS'   ,@nanoseconds)  
 SET   @formatteddate   =   replace(@formatteddate,   'DAY'   ,@dayname)  
 SET   @formatteddate   =   replace(@formatteddate,   'TZ'   ,@TZ)  
 SET   @formatteddate   =   replace(@formatteddate,   'UNIXPOSIX'   ,@UNIXPOSIX)  

 IF charindex(   'ucase'   ,@formatteddate)   <>   0  
 BEGIN  
 SET   @formatteddate   =   replace(@formatteddate,   'ucase'   ,   ''   )  
 SET   @formatteddate   =   UPPER   (@formatteddate)  
 END  

 IF charindex(   'lcase'   ,@formatteddate)   <>   0  
 BEGIN  
 SET   @formatteddate   =   replace(@formatteddate,   'lcase'   ,   ''   )  
 SET   @formatteddate   =   LOWER   (@formatteddate)  
 END  


 return @formatteddate  
 end  


 GO  

以上是关于sql 自定义格式DateTime SQL函数 - 模仿.NET样式的主要内容,如果未能解决你的问题,请参考以下文章

Excel中的SQL Server Datetime对象持续重新格式化问题

sql server数据库中定义了datetime数据类型,在插入数据时输入格式是“2001-9-1”可以,是“9/1/2001”

sql 里怎么把varchar转成datetime格式

如何优化Oracle在where条件中用了自定义函数的SQL语句

什么是 Vaex 函数将字符串解析为 datetime64,相当于 pandas to_datetime,允许自定义格式?

我写了一个SQL自定义函数,执行不成功,急!! 具体如下: GO CREATE FUNCTIO