SELECT dbo.fn_Data(getdate(),’yyyymmdd’)

CREATE FUNCTION [dbo].[fn_Data]

(@date as datetime,

@formatstring as varchar(100)

)

RETURNS varchar(100) AS

BEGIN

declare @datestring as varchar(100)

set @datestring=@formatstring

--year
set @datestring=replace(@datestring, 'yyyy', cast(year(@date) as char(4)))
set @datestring=replace(@datestring, 'yy', right(cast(year(@date) as char(4)),2)) --millisecond
set @datestring=replace(@datestring, 'ms', replicate('0',3-len(cast(datepart(ms,@date) as varchar(3)))) + cast(datepart(ms, @date) as varchar(3))) --month
set @datestring=replace(@datestring, 'mm', replicate('0',2-len(cast(month(@date) as varchar(2)))) + cast(month(@date) as varchar(2)))
set @datestring=replace(@datestring, 'm', cast(month(@date) as varchar(2))) --day
set @datestring=replace(@datestring, 'dd', replicate('0',2-len(cast(day(@date) as varchar(2)))) + cast(day(@date) as varchar(2)))
set @datestring=replace(@datestring, 'd', cast(day(@date) as varchar(2))) --hour
set @datestring=replace(@datestring, 'hh', replicate('0',2-len(cast(datepart(hh,@date) as varchar(2)))) + cast(datepart(hh, @date) as varchar(2)))
set @datestring=replace(@datestring, 'h', cast(datepart(hh, @date) as varchar(2))) --minute
set @datestring=replace(@datestring, 'nn', replicate('0',2-len(cast(datepart(n,@date) as varchar(2)))) + cast(datepart(n, @date) as varchar(2)))
set @datestring=replace(@datestring, 'n', cast(datepart(n, @date) as varchar(2))) --second
set @datestring=replace(@datestring, 'ss', replicate('0',2-len(cast(datepart(ss,@date) as varchar(2)))) + cast(datepart(ss, @date) as varchar(2)))
set @datestring=replace(@datestring, 's', cast(datepart(ss, @date) as varchar(2))) return @datestring

END

GO

Sql Server 按格式输出日期的更多相关文章

  1. SQL Server判断是否满足日期格式(YYYYMMDD)以及中文等判断,格式化为YYYY-MM-DD

    SQL Server判断是否满足日期格式(YYYYMMDD)以及中文等判断: 在做sql数据的正确性审核中,需要判断数据是否满足日期格式,网上找不到相关的资料,于是自己花了半天写了一个简单的函数 具体 ...

  2. 【SQL Server】MS SQL Server中的CONVERT日期格式化大全

    CONVERT 函数将某种数据类型的表达式显式转换为另一种数据类型.SQL Server中 将日期格式化. SQL Server 支持使用科威特算法的阿拉伯样式中的数据格式. 在表中,左侧的两列表示将 ...

  3. javascript两行代码按指定格式输出日期时间

    javascript两行代码按指定格式输出日期时间,具体看代码: function date2str(x,y) { var z ={y:x.getFullYear(),M:x.getMonth()+1 ...

  4. SQL Server系统函数:日期函数

    原文:SQL Server系统函数:日期函数 1.返回当前日期和时间 select GETDATE() '当前日期-精确到33毫秒' select GETUTCDATE() 'UTC日期和时间-精确到 ...

  5. sql server 字符串转成日期格式

    在SQL Server数据库中,SQL Server日期时间格式转换字符串可以改变SQL Server日期和时间的格式,是每个SQL数据库用户都应该掌握的.本文我们主要就介绍一下SQL Server日 ...

  6. MS SQL Server中的CONVERT日期格式化大全

    CONVERT 将某种数据类型的表达式显式转换为另一种数据类型.由于某些需求经常用到取日期格式的不同. 现以下可在SQL Server中将日期格式化. SQL Server 支持使用科威特算法的阿拉伯 ...

  7. sql server 时间格式转换

    sql server2000中使用convert来取得datetime数据类型样式(全) 日期数据格式的处理,两个示例: CONVERT(varchar(16), 时间一, 20) 结果:2007-0 ...

  8. 微软BI 之SSIS 系列 - 两种将 SQL Server 数据库数据输出成 XML 文件的方法

    开篇介绍 在 SSIS 中并没有直接提供从数据源到 XML 的转换输出,Destination 的输出对象有 Excel File, Flat File, Database 等,但是并没有直接提供 X ...

  9. SQL Server中smalldatetime的日期范围为何是[1900-01-01,2079-06-06]

    本文目录列表: 1.SQL Server中的基准日期 2.smalldatetime的日期范围 3.smalldatetime的日期范围和无符号2字节整数的关系 4.总结语 5.参考清单列表   SQ ...

随机推荐

  1. 连载《一个程序猿的生命周期》-《发展篇》- 22.城市奋斗者的阿Q精神

    好久没有写文章了,有些人会认为博主肯定是没有什么好写的了.其实不然,是想写的太多,实在是没有时间.上半年一直比较忙,处于加班常态的状态,身心疲惫.相较于城市的伪奋斗者,我算比较实干的,而界定“实干”的 ...

  2. 系统不支持WP开发

    好伤心,,,系统不支持WP开发... 买的ThinkPad S5 自带的win8,既不属于专业版,也不属于家庭版,,不属于各种版本. 其他条件都满足了.. 难道我要还系统吗??

  3. MEF 基础简介 二

    MEF的导出(Export)和导入(Import) using System; using System.Collections.Generic; using System.Linq; using S ...

  4. MyBatis3-配置使用log4j输出日志

    配置步骤: 1.POM的依赖引入 <!-- log4j --> <!-- https://mvnrepository.com/artifact/log4j/log4j --> ...

  5. tomcat开启自启动

    linux方式 #!/bin/bash #chkconfig: #description: Starts and Stops the Tomcat daemon. #by benjamin ##### ...

  6. vuex 、store、state (转载)

    vuex  文档 https://vuex.vuejs.org/zh/guide/state.html

  7. 重装MacOS

    从U盘启动 开启或重新启动您的 Mac 后,立即按住 Option 键。 当您看到“启动管理器”窗口时,松开 Option 键。 选择您的启动磁盘,然后点按箭头或按下 Return 键。 Mac 的启 ...

  8. js的数据类型:单例模式,工厂模式,构造函数

    js数据类型 基本数据类型:string   undefined   null  boolean  number 引用数据类型  Object  array  function 二者的区别 基本数据类 ...

  9. canvas-7global.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 洛谷P2197 nim游戏(Nim游戏)

    题目描述 甲,乙两个人玩Nim取石子游戏. nim游戏的规则是这样的:地上有n堆石子(每堆石子数量小于10000),每人每次可从任意一堆石子里取出任意多枚石子扔掉,可以取完,不能不取.每次只能从一堆里 ...