如何在 SQL Server中 将sql server日期格式化化

Sql Server 日期格式化函数 (类似Format函数),一般用于存储过程中 -
- ITeye技术网站
Sql Server 中一个非常强大的日期格式化函数
Select CONVERT(varchar(100), GETDATE(), 0): 05 16 AM
Select CONVERT(varchar(100), GETDATE(), 1): 05/16/06
Select CONVERT(varchar(100), GETDATE(), 2): 06.05.16
Select CONVERT(varchar(100), GETDATE(), 3): 16/05/06
Select CONVERT(varchar(100), GETDATE(), 4): 16.05.06
Select CONVERT(varchar(100), GETDATE(), 5): 16-05-06
Select CONVERT(varchar(100), GETDATE(), 6): 16 05 06
Select CONVERT(varchar(100), GETDATE(), 7): 05 16, 06
Select CONVERT(varchar(100), GETDATE(),: 10:57:46
Select CONVERT(varchar(100), GETDATE(), 9): 05 16 :46:827AM
Select CONVERT(varchar(100), GETDATE(), 10): 05-16-06
Select CONVERT(varchar(100), GETDATE(), 11): 06/05/16
Select CONVERT(varchar(100), GETDATE(), 12): 060516
Select CONVERT(varchar(100), GETDATE(), 13): 16 05 :46:937
Select CONVERT(varchar(100), GETDATE(), 14): 10:57:46:967
Select CONVERT(varchar(100), GETDATE(), 20):
10:57:47
Select CONVERT(varchar(100), GETDATE(), 21):
10:57:47.157
Select CONVERT(varchar(100), GETDATE(), 22): 05/16/06 10:57:47 AM
Select CONVERT(varchar(100), GETDATE(), 23):
Select CONVERT(varchar(100), GETDATE(), 24): 10:57:47
Select CONVERT(varchar(100), GETDATE(), 25):
10:57:47.250
Select CONVERT(varchar(100), GETDATE(), 100): 05 16 AM
Select CONVERT(varchar(100), GETDATE(), 101): 05/16/2006
Select CONVERT(varchar(100), GETDATE(), 102):
Select CONVERT(varchar(100), GETDATE(), 103): 16/05/2006
Select CONVERT(varchar(100), GETDATE(), 104): 16.05.2006
Select CONVERT(varchar(100), GETDATE(), 105): 16-05-2006
Select CONVERT(varchar(100), GETDATE(), 106): 16 05 2006
Select CONVERT(varchar(100), GETDATE(), 107): 05 16, 2006
Select CONVERT(varchar(100), GETDATE(), 108): 10:57:49
Select CONVERT(varchar(100), GETDATE(), 109): 05 16 :49:437AM
Select CONVERT(varchar(100), GETDATE(), 110): 05-16-2006
Select CONVERT(varchar(100), GETDATE(), 111):
Select CONVERT(varchar(100), GETDATE(), 112):
Select CONVERT(varchar(100), GETDATE(), 113): 16 05 :49:513
Select CONVERT(varchar(100), GETDATE(), 114): 10:57:49:547
Select CONVERT(varchar(100), GETDATE(), 120):
10:57:49
Select CONVERT(varchar(100), GETDATE(), 121):
10:57:49.700
Select CONVERT(varchar(100), GETDATE(), 126): T10:57:49.827
Select CONVERT(varchar(100), GETDATE(), 130): 18 ???? ?????? :49:907AM
Select CONVERT(varchar(100), GETDATE(), 131): 18/04/:49:920AM
常用:
Select CONVERT(varchar(100), GETDATE(),: 10:57:46
Select CONVERT(varchar(100), GETDATE(), 24): 10:57:47
Select CONVERT(varchar(100), GETDATE(), 108): 10:57:49
Select CONVERT(varchar(100), GETDATE(), 12): 060516
Select CONVERT(varchar(100), GETDATE(), 23): &
存储过程中常用知识点:
循环
declare @i int
set @i=1
while @i&30
begin
&& insert into test (userid) values(@i)
&& set @i=@i+1
end
用变量代替表名的方法
declare @fld nvarchar(255)
declare @sql nvarchar(255)
select @fld = 'userName'
select @sql = 'select ' + @fld + ' from tbl'
exec sp_executesql @sql
备份收缩数据库
backup log register with NO_LOG
backup log register with TRUNCATE_ONLY
DBCC SHRINKDATABASE(register)
guxinghanshe
浏览: 21478 次
来自: 北京sql中怎么将varchar类型的时间格式化为特定格式存储
&来源:读书人网&【读书人网():综合教育门户网站】
sql中如何将varchar类型的时间格式化为特定格式存储?原有数据库中的时间字段类型为varchar,数据格式类似为
sql中如何将varchar类型的时间格式化为特定格式存储?原有中的时间字段类型为varchar,数据格式类似为 & 8:8:8
现在需要将原有中的时间一列的数据格式改为 & 08:08:08并还在的原有库中存储。请问该如何实现??
多谢。 [解决办法]
--用update语句更新原有字段中的数据
update 表名 set 字段名 = convert(varchar(20),cast(字段名 as datetime),120)SQL&SERVER中如何格式化日期。。
SELECT convert(varchar, getdate(),
100) -- mon dd yyyy hh:mmAM (or
-- Oct 2 2008
SELECT convert(varchar, getdate(),
101) -- mm/dd/yyyy -
10/02/2008&
SELECT convert(varchar, getdate(),
102) -- yyyy.mm.dd --
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(),
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(),
yyyy-mm-ddThh:mm:ss.mmm
-- T10: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
--通用的日期转换函数CREATE FUNCTION dbo.fnFormatDate
(@Datetime DATETIME, @FormatMask VARCHAR(32))RETURNS
VARCHAR(32)AS
DECLARE @StringDate VARCHAR(32)
SET @StringDate =
@FormatMask
(CHARINDEX ('YYYY',@StringDate)
SET @StringDate
= REPLACE(@StringDate, 'YYYY',DATENAME(YY,
@Datetime))
(CHARINDEX ('YY',@StringDate) & 0)
SET @StringDate
= REPLACE(@StringDate, 'YY',RIGHT(DATENAME(YY,
@Datetime),2))
(CHARINDEX ('Month',@StringDate)
SET @StringDate
= REPLACE(@StringDate, 'Month',DATENAME(MM,
@Datetime))
(CHARINDEX ('MON',@StringDate
COLLATE SQL_Latin1_General_CP1_CS_AS)&0)
SET @StringDate
= REPLACE(@StringDate, 'MON',LEFT(UPPER(DATENAME(MM,
@Datetime)),3))
(CHARINDEX ('Mon',@StringDate) &
SET @StringDate
= REPLACE(@StringDate, 'Mon',LEFT(DATENAME(MM,
@Datetime),3))
(CHARINDEX ('MM',@StringDate) & 0)
SET @StringDate
= REPLACE(@StringDate, 'MM',RIGHT('0'+CONVERT(VARCHAR,DATEPART(MM,
@Datetime)),2))
(CHARINDEX ('M',@StringDate) & 0)
SET @StringDate
= REPLACE(@StringDate, 'M',CONVERT(VARCHAR,DATEPART(MM,
@Datetime)))
(CHARINDEX ('DD',@StringDate) & 0)
SET @StringDate
= REPLACE(@StringDate, 'DD',right('0'+DATENAME(DD,
@Datetime),2))
(CHARINDEX ('D',@StringDate) & 0)
SET @StringDate
= REPLACE(@StringDate, 'D',DATENAME(DD,
@Datetime))&
RETURN @StringDate
2.通过dateadd函数来制定时间间隔
dateadd(year,-1,convert(varchar, getdate(),
101))前一年,后一年反之
select dateadd(month,-1,convert(varchar, getdate(),
101))前一月,后一月反之
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。0&& 或&& 100&& (*)&&&& 默认值&& mon&& dd&& yyyy&& hh:miAM(或&& PM)&&&& & 1&& 101&& 美国&& mm/dd/yyyy&&&& & 2&& 102&& ANSI&& yy.mm.dd&&&& & 3&& 103&& 英国/法国&& dd/mm/yy&&&& & 4&& 104&& 德国&& dd.mm.yy&&&& & 5&& 105&& 意大利&& dd-mm-yy&&&& & 6&& 106&& -&& dd&& mon&& yy&&&& & 7&& 107&& -&& mon&& dd,&& yy&&&& & 8&& 108&& -&& hh:mm:ss&&&& & -&& 9&& 或&& 109&& (*)&&&& 默认值&& +&& 毫秒&& mon&& dd&& yyyy&& hh:mi:ss:mmmAM(或&& PM)&&&& & 10&& 110&& 美国&& mm-dd-yy&&&& & 11&& 111&& 日本&& yy/mm/dd&&&& & 12&& 112&& ISO&& yymmdd&&&& & -&& 13&& 或&& 113&& (*)&&&& 欧洲默认值&& +&& 毫秒&& dd&& mon&& yyyy&& hh:mm:ss:mmm(24h)&&&& & 14&& 114&& -&& hh:mi:ss:mmm(24h)&&&& & -&& 20&& 或&& 120&& (*)&&&& ODBC&& 规范&& yyyy-mm-dd&& hh:mm:ss[.fff]&&&& & -&& 21&& 或&& 121&& (*)&&&& ODBC&& 规范(带毫秒)&& yyyy-mm-dd&& hh:mm:ss[.fff]&&&& & -&& 126(***)&& ISO8601&& yyyy-mm-dd&& Thh:mm:ss:mmm(不含空格)&&&& & -&& 130*&& 科威特&& dd&& mon&& yyyy&& hh:mi:ss:mmmAM&&&& & -&& 131*&& 科威特&& dd/mm/yy&& hh:mi:ss:mmmAM&&&
Sql Server 中一个非常强大的日期格式化函数Select CONVERT(varchar(100), GETDATE(), 0): 05 16 AMSelect CONVERT(varchar(100), GETDATE(), 1): 05/16/06Select CONVERT(varchar(100), GETDATE(), 2): 06.05.16Select CONVERT(varchar(100), GETDATE(), 3): 16/05/06Select CONVERT(varchar(100), GETDATE(), 4): 16.05.06Select CONVERT(varchar(100), GETDATE(), 5): 16-05-06Select CONVERT(varchar(100), GETDATE(), 6): 16 05 06Select CONVERT(varchar(100), GETDATE(), 7): 05 16, 06Select CONVERT(varchar(100), GETDATE(), 8): 10:57:46Select CONVERT(varchar(100), GETDATE(), 9): 05 16 :46:827AMSelect CONVERT(varchar(100), GETDATE(), 10): 05-16-06Select CONVERT(varchar(100), GETDATE(), 11): 06/05/16Select CONVERT(varchar(100), GETDATE(), 12): 060516Select CONVERT(varchar(100), GETDATE(), 13): 16 05 :46:937Select CONVERT(varchar(100), GETDATE(), 14): 10:57:46:967Select CONVERT(varchar(100), GETDATE(), 20):
10:57:47Select CONVERT(varchar(100), GETDATE(), 21):
10:57:47.157Select CONVERT(varchar(100), GETDATE(), 22): 05/16/06 10:57:47 AMSelect CONVERT(varchar(100), GETDATE(), 23): Select CONVERT(varchar(100), GETDATE(), 24): 10:57:47Select CONVERT(varchar(100), GETDATE(), 25):
10:57:47.250Select CONVERT(varchar(100), GETDATE(), 100): 05 16 AMSelect CONVERT(varchar(100), GETDATE(), 101): 05/16/2006Select CONVERT(varchar(100), GETDATE(), 102): Select CONVERT(varchar(100), GETDATE(), 103): 16/05/2006Select CONVERT(varchar(100), GETDATE(), 104): 16.05.2006Select CONVERT(varchar(100), GETDATE(), 105): 16-05-2006Select CONVERT(varchar(100), GETDATE(), 106): 16 05 2006Select CONVERT(varchar(100), GETDATE(), 107): 05 16, 2006Select CONVERT(varchar(100), GETDATE(), 108): 10:57:49Select CONVERT(varchar(100), GETDATE(), 109): 05 16 :49:437AMSelect CONVERT(varchar(100), GETDATE(), 110): 05-16-2006Select CONVERT(varchar(100), GETDATE(), 111): Select CONVERT(varchar(100), GETDATE(), 112): Select CONVERT(varchar(100), GETDATE(), 113): 16 05 :49:513Select CONVERT(varchar(100), GETDATE(), 114): 10:57:49:547Select CONVERT(varchar(100), GETDATE(), 120):
10:57:49Select CONVERT(varchar(100), GETDATE(), 121):
10:57:49.700Select CONVERT(varchar(100), GETDATE(), 126): T10:57:49.827Select CONVERT(varchar(100), GETDATE(), 130): 18 ???? ?????? :49:907AMSelect CONVERT(varchar(100), GETDATE(), 131): 18/04/:49:920AM
常用:Select CONVERT(varchar(100), GETDATE(), 8): 10:57:46Select CONVERT(varchar(100), GETDATE(), 24): 10:57:47Select CONVERT(varchar(100), GETDATE(), 108): 10:57:49Select CONVERT(varchar(100), GETDATE(), 12): 060516Select CONVERT(varchar(100), GETDATE(), 23):
SQL中CONVERT转化函数的用法
CONVERT的使用方法:
////////////////////////////////////////////////////////////////////////////////////////
格式:CONVERT(data_type,e&xpression[,style])
说明:此样式一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,char,varchar)相互转换的时候才用到.
例子:Select CONVERT(varchar(30),getdate(),101) now结果为now---------------------------------------09/15/2001
/////////////////////////////////////////////////////////////////////////////////////
style数字在转换时间时的含义如下
-------------------------------------------------------------------------------------------------Style(2位表示年份) | Style(4位表示年份) | 输入输出格式 -------------------------------------------------------------------------------------------------- | 0 or 100 | mon dd yyyy hh:miAM(或PM) -------------------------------------------------------------------------------------------------1 | 101 | mm/dd/yy -------------------------------------------------------------------------------------------------2 | 102 | yy-mm-dd -------------------------------------------------------------------------------------------------3 | 103 | dd/mm/yy -------------------------------------------------------------------------------------------------4 | 104 | dd-mm-yy -------------------------------------------------------------------------------------------------5 | 105 | dd-mm-yy -------------------------------------------------------------------------------------------------6 | 106 | dd mon yy -------------------------------------------------------------------------------------------------7 | 107 | mon dd,yy -------------------------------------------------------------------------------------------------8 | 108 | hh:mm:ss -------------------------------------------------------------------------------------------------- | 9 or 109 | mon dd yyyy hh:mi:ss:mmmmAM(或PM)-------------------------------------------------------------------------------------------------10 | 110 | mm-dd-yy -------------------------------------------------------------------------------------------------11 | 111 | yy/mm/dd -------------------------------------------------------------------------------------------------12 | 112 | yymmdd -------------------------------------------------------------------------------------------------- | 13 or 113 | dd mon yyyy hh:mi:ss:mmm(24小时制) -------------------------------------------------------------------------------------------------14 | 114 | hh:mi:ss:mmm(24小时制) -------------------------------------------------------------------------------------------------- | 20 or 120 | yyyy-mm-dd hh:mi:ss(24小时制) -------------------------------------------------------------------------------------------------- | 21 or 121 | yyyy-mm-dd hh:mi:ss:mmm(24小时制)
阅读(...) 评论()电脑,计算机,编程,硬件,学习,方法,技巧,办公,系统,网页
扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
sql server语句中日期时间格式化查询
举报该文档为侵权文档。
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口

我要回帖

更多关于 sqlserver格式化代码 的文章

 

随机推荐