//code by:博客园-曹永思
--时间差(取2013-2-1和之后的日期):datediff(day,'2013-2-1',[time])>=0 SELECT convert(varchar, getdate(), 100) -- mon dd yyyy hh:mmAM (or PM) -- Oct 2 2008 11:01AM SELECT convert(varchar, getdate(), 101) -- mm/dd/yyyy - 10/02/2008 SELECT convert(varchar, getdate(), 102) -- yyyy.mm.dd -- 2008.10.02 SELECT convert(varchar, getdate(), 103) -- dd/mm/yyyy SELECT convert(varchar, getdate(), 104) -- dd.mm.yyyy SELECT convert(varchar, getdate(), 105) -- dd-mm-yyyy SELECT convert(varchar, getdate(), 106) -- dd mon yyyy SELECT convert(varchar, getdate(), 107) -- mon dd, yyyy SELECT convert(varchar, getdate(), 108) -- hh:mm:ss SELECT convert(varchar, getdate(), 109) -- mon dd yyyy hh:mm:ss:mmmAM (or PM) -- Oct 2 2008 11:02:44:013AM SELECT convert(varchar, getdate(), 110) -- mm-dd-yyyy SELECT convert(varchar, getdate(), 111) -- yyyy/mm/dd SELECT convert(varchar, getdate(), 112) -- yyyymmdd SELECT convert(varchar, getdate(), 113) -- dd mon yyyy hh:mm:ss:mmm -- 02 Oct 2008 11:02:07:577 SELECT convert(varchar, getdate(), 114) -- hh:mm:ss:mmm(24h) SELECT convert(varchar, getdate(), 120) -- yyyy-mm-dd hh:mm:ss(24h) SELECT convert(varchar, getdate(), 121) -- yyyy-mm-dd hh:mm:ss.mmm SELECT convert(varchar, getdate(), 126) -- yyyy-mm-ddThh:mm:ss.mmm -- 2008-10-02T10:52:47.513 -- 利用字符串函数创建不同的日期格式 SELECT replace(convert(varchar, getdate(), 111), '/', ' ') -- yyyy mm dd SELECT convert(varchar(7), getdate(), 126) -- yyyy-mm SELECT right(convert(varchar, getdate(), 106), 8) -- mon yyyy go --通用的日期转换函数CREATE FUNCTION dbo.fnFormatDate (@Datetime DATETIME, @FormatMask VARCHAR(32))RETURNS VARCHAR(32)AS BEGIN DECLARE @StringDate VARCHAR(32) SET @StringDate = @FormatMask IF (CHARINDEX ('YYYY',@StringDate) > 0) SET @StringDate = REPLACE(@StringDate, 'YYYY',DATENAME(YY, @Datetime)) IF (CHARINDEX ('YY',@StringDate) > 0) SET @StringDate = REPLACE(@StringDate, 'YY',RIGHT(DATENAME(YY, @Datetime),2)) IF (CHARINDEX ('Month',@StringDate) > 0) SET @StringDate = REPLACE(@StringDate, 'Month',DATENAME(MM, @Datetime)) IF (CHARINDEX ('MON',@StringDate COLLATE SQL_Latin1_General_CP1_CS_AS)>0) SET @StringDate = REPLACE(@StringDate, 'MON',LEFT(UPPER(DATENAME(MM, @Datetime)),3)) IF (CHARINDEX ('Mon',@StringDate) > 0) SET @StringDate = REPLACE(@StringDate, 'Mon',LEFT(DATENAME(MM, @Datetime),3)) IF (CHARINDEX ('MM',@StringDate) > 0) SET @StringDate = REPLACE(@StringDate, 'MM',RIGHT('0'+CONVERT(VARCHAR,DATEPART(MM, @Datetime)),2)) IF (CHARINDEX ('M',@StringDate) > 0) SET @StringDate = REPLACE(@StringDate, 'M',CONVERT(VARCHAR,DATEPART(MM, @Datetime))) IF (CHARINDEX ('DD',@StringDate) > 0) SET @StringDate = REPLACE(@StringDate, 'DD',right('0'+DATENAME(DD, @Datetime),2)) IF (CHARINDEX ('D',@StringDate) > 0) SET @StringDate = REPLACE(@StringDate, 'D',DATENAME(DD, @Datetime)) RETURN @StringDate END GO
欢迎转载,转载请注明出处,希望帮到更多人。
.net重写URL:http://www.cnblogs.com/yonsy/archive/2012/09/21/2696935.html