zoukankan      html  css  js  c++  java
  • Sql Server 按格式输出日期

    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

  • 相关阅读:
    windows server2008自动登录
    WindosServer2008 激活问题。
    [转]10分钟写出你的第一个包含CRUD的Rails程序
    SQL 2008操作相关
    没有域环境安装SharePoint2010
    D3D10彻底抛弃了固定图形管线
    MultiUser01 – 简介
    6种Socket I/O 模型性能比较,图
    Dr程序耗尽了CPU
    IDXGIOutput接口
  • 原文地址:https://www.cnblogs.com/JinweiChang/p/10461801.html
Copyright © 2011-2022 走看看