0

我有一个 C# 语句:

Date.Now.ToString("yyyy_MM_yy_HH_mm_ss_ms") 

现在,我需要在 SQL Server 2008 R2 中使用它。我知道我可以GETDATE用于当前日期。但无法获得这种格式。

4

4 回答 4

5

尝试这个:

select cast(year(getdate()) as char(4))+'_'+
       case when month(getdate())<10 then '0'+cast(month(getdate()) as char(1)) else cast(month(getdate()) as char(2)) end+'_'+
       right(cast(datepart(yy,getdate()) as char(4)),2)+'_'+
       replace(replace(right(convert(varchar(22),getdate(),121),11),':','_'),'.','_')

SQL小提琴演示

于 2012-11-22T07:11:50.267 回答
2

在这里试用功能

阅读有关此的更多信息:SQL Server 2008 中的格式化日期

/****** 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
于 2012-11-22T07:28:37.487 回答
1

尝试这个

SELECT CONVERT(VARCHAR(19), SYSDATETIME(), 120)

对于 YYYY-MM-DD HH:MI:SS(24h) 格式

请参阅此处了解不同的格式

http://www.sql-server-helper.com/sql-server-2008/sql-server-2008-date-format.aspx

于 2012-11-22T07:11:55.853 回答
1

尝试这个:

    select cast(DATEPART(yy,getdate()) as CHAR(4))+'_'+
           cast(DATEPART(mm,getdate()) as CHAR(2))+'_'+
           right(cast(DATEPART(yy,getdate()) as CHAR(4)),2)+'_'+
           cast(DATEPART(HH,getdate()) as CHAR(2))+'_'+
           cast(DATEPART(mi,getdate()) as CHAR(2))+'_'+
           cast(DATEPART(ss,getdate()) as CHAR(2))+'_'+
           cast(DATEPART(MILLISECOND ,getdate()) as CHAR(5))  
于 2012-11-22T07:19:56.343 回答