SQL Server 2008中的创建格式化日期(1)

php中文网
发布: 2016-06-07 16:09:16
原创
1109人浏览过

让我们来创建dbo.format_date 函数,如下所示。 /******Object:UserDefinedFunction[dbo].[format_date] Script Date :05/12/200923:19:35******/ IFEXISTS( SELECT * FROM sys.objects WHERE object_id=OBJECT_ID(N '[dbo].[format_date]' ) AND type in (N

让我们来创建dbo.format_date 函数,如下所示。

绘蛙
绘蛙

电商场景的AI创作平台,无需高薪聘请商拍和文案团队,使用绘蛙即可低成本、批量创作优质的商拍图、种草文案

绘蛙 179
查看详情 绘蛙
<OL class=dp-sql><LI class=alt><SPAN><SPAN>/****** Object: UserDefinedFunction [dbo].[format_date]   </SPAN></SPAN><LI><SPAN>Script </SPAN><SPAN class=keyword>Date</SPAN><SPAN>: 05/12/2009 23:19:35 ******/  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN>IF EXISTS (</SPAN><SPAN class=keyword>SELECT</SPAN><SPAN> * </SPAN><SPAN class=keyword>FROM</SPAN><SPAN> sys.objects  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>WHERE</SPAN><SPAN> object_id = OBJECT_ID(N</SPAN><SPAN class=string>'[dbo].[format_date]'</SPAN><SPAN>)  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=op>AND</SPAN><SPAN> type </SPAN><SPAN class=op>in</SPAN><SPAN> (N</SPAN><SPAN class=string>'FN'</SPAN><SPAN>, N</SPAN><SPAN class=string>'IF'</SPAN><SPAN>, N</SPAN><SPAN class=string>'TF'</SPAN><SPAN>, N</SPAN><SPAN class=string>'FS'</SPAN><SPAN>, N</SPAN><SPAN class=string>'FT'</SPAN><SPAN>))  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>DROP</SPAN><SPAN> </SPAN><SPAN class=keyword>FUNCTION</SPAN><SPAN> [dbo].[format_date]  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN>GO  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN>/****** Object: UserDefinedFunction [dbo].[format_date]  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN>Script </SPAN><SPAN class=keyword>Date</SPAN><SPAN>: 05/12/2009 23:19:18 ******/  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>SET</SPAN><SPAN> ANSI_NULLS </SPAN><SPAN class=keyword>ON</SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN>GO  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>SET</SPAN><SPAN> QUOTED_IDENTIFIER </SPAN><SPAN class=keyword>ON</SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN>GO  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>SET</SPAN><SPAN> CONCAT_NULL_YIELDS_NULL </SPAN><SPAN class=keyword>OFF</SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN>go  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>CREATE</SPAN><SPAN> </SPAN><SPAN class=keyword>function</SPAN><SPAN> [dbo].[format_date]  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN>(@inputdate datetime ,@format </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(500))  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>returns</SPAN><SPAN> </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(500)  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>as</SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>begin</SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @</SPAN><SPAN class=func>year</SPAN><SPAN> </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(4) </SPAN><SPAN class=comment>--YYYY </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @shortyear </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(4) </SPAN><SPAN class=comment>--Yr </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @quarter </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(4) </SPAN><SPAN class=comment>--QQ </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @</SPAN><SPAN class=func>month</SPAN><SPAN> </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(2) </SPAN><SPAN class=comment>--MM </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @week </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(2) </SPAN><SPAN class=comment>--WW </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @</SPAN><SPAN class=func>day</SPAN><SPAN> </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(2) </SPAN><SPAN class=comment>--DD </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @24hours </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(2) </SPAN><SPAN class=comment>--24HH </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @12hours </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(2) </SPAN><SPAN class=comment>--HH </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @minutes </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(2) </SPAN><SPAN class=comment>--MI </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @seconds </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(2) </SPAN><SPAN class=comment>--SS </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @milliseconds </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(3) </SPAN><SPAN class=comment>--MS </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @microseconds </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(6) </SPAN><SPAN class=comment>--MCS </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @nanoseconds </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(9) </SPAN><SPAN class=comment>--NS </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @dayname </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(15) </SPAN><SPAN class=comment>--DAY </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @monthname </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(15) </SPAN><SPAN class=comment>--MONTH </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @shortmonthname </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(15) </SPAN><SPAN class=comment>--MON </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @AMPM </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(15) </SPAN><SPAN class=comment>--AMPM </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @TZ </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(15) </SPAN><SPAN class=comment>--TZ </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @UNIXPOSIX </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(15) </SPAN><SPAN class=comment>--UNIXPOSIX </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=comment>--UCASE </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=comment>--LCASE </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>declare</SPAN><SPAN> @formatteddate </SPAN><SPAN class=keyword>varchar</SPAN><SPAN>(500)  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=comment>--Assign current date and time to </SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN>if (@inputdate </SPAN><SPAN class=keyword>is</SPAN><SPAN> </SPAN><SPAN class=op>NULL</SPAN><SPAN> </SPAN><SPAN class=op>or</SPAN><SPAN> @inputdate =</SPAN><SPAN class=string>''</SPAN><SPAN>)  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>begin</SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>set</SPAN><SPAN> @inputdate = getdate()  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>end</SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN>if (@format </SPAN><SPAN class=keyword>is</SPAN><SPAN> </SPAN><SPAN class=op>NULL</SPAN><SPAN> </SPAN><SPAN class=op>or</SPAN><SPAN> @format =</SPAN><SPAN class=string>''</SPAN><SPAN>)  </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>begin</SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>set</SPAN><SPAN> @format =</SPAN><SPAN class=string>'YYYY-MM-DD 12HH:MI:SS AMPM'</SPAN><SPAN> </SPAN><LI class=alt><SPAN> </SPAN><LI><SPAN class=keyword>end</SPAN><SPAN> </SPAN></LI></OL>
登录后复制
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号