sql server 获取格式化日期时间
2022/1/17 19:07:29
本文主要是介绍sql server 获取格式化日期时间,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
USE [TCX_32101] GO /****** Object: UserDefinedFunction [dbo].[f_get_format_datetime] Script Date: 2022/1/17 16:27:16 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: taozi -- Create date: 2022年1月17日 -- Description: 获取格式化日期时间 -- ============================================= CREATE FUNCTION [dbo].[f_get_format_datetime] ( @date datetime, @format varchar(100) ) RETURNS varchar(100) AS BEGIN --year set @format=replace(@format, 'yyyy', cast(year(@date) as char(4))) set @format=replace(@format, 'yy', right(cast(year(@date) as char(4)),2)) --millisecond set @format=replace(@format, 'ms', replicate('0',3-len(cast(datepart(ms,@date) as varchar(3)))) + cast(datepart(ms, @date) as varchar(3))) --month set @format=replace(@format, 'mm', replicate('0',2-len(cast(month(@date) as varchar(2)))) + cast(month(@date) as varchar(2))) set @format=replace(@format, 'm', cast(month(@date) as varchar(2))) --day set @format=replace(@format, 'dd', replicate('0',2-len(cast(day(@date) as varchar(2)))) + cast(day(@date) as varchar(2))) set @format=replace(@format, 'd', cast(day(@date) as varchar(2))) --hour set @format=replace(@format, 'hh', replicate('0',2-len(cast(datepart(hh,@date) as varchar(2)))) + cast(datepart(hh, @date) as varchar(2))) set @format=replace(@format, 'h', cast(datepart(hh, @date) as varchar(2))) --minute set @format=replace(@format, 'nn', replicate('0',2-len(cast(datepart(n,@date) as varchar(2)))) + cast(datepart(n, @date) as varchar(2))) set @format=replace(@format, 'n', cast(datepart(n, @date) as varchar(2))) --second set @format=replace(@format, 'ss', replicate('0',2-len(cast(datepart(ss,@date) as varchar(2)))) + cast(datepart(ss, @date) as varchar(2))) set @format=replace(@format, 's', cast(datepart(ss, @date) as varchar(2))) return @format END GO
这篇关于sql server 获取格式化日期时间的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-08Docker下的SqlServer发布订阅启用
- 2023-06-05Docker安装MS SQL Server并使用Navicat远程连接
- 2023-05-25深入浅出 SQL Server CDC 数据同步
- 2023-05-12通过空间占用和执行计划了解SQL Server的行存储索引
- 2023-04-24以SQLserver为例的Dapper详细讲解
- 2022-11-30SQL server高级函数查询
- 2022-11-26SQL SERVER数据库服务器CPU不能全部利用原因分析
- 2022-11-21SQL Server 时间算差值/常用函数
- 2022-11-20调试Archery连接SQL Server提示驱动错误
- 2022-10-22SQL Server 完整、差异备份+完整、差异还原(详细讲解,规避错误)