sql字符格式的日期转换问题 我用cast将字符格式的数据转换成日期格式的为什么21/09/2010转就报错也就是说sql识别他的过程是m/d/yyyy有没有其他的转换方式可以识别21/09/2010的谢谢~~~~ 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 select convert(datetime,'21/09/2010',103) as date/*date-----------------------2010-09-21 00:00:00.000(1 行受影响)*/ select CONVERT(varchar, getdate(), 120 ) 2004-09-12 11:06:08 select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','') 20040912110608 select CONVERT(varchar(12) , getdate(), 111 ) 2004/09/12 select CONVERT(varchar(12) , getdate(), 112 ) 20040912 select CONVERT(varchar(12) , getdate(), 102 ) 2004.09.12 其它我不常用的日期格式转换方法: select CONVERT(varchar(12) , getdate(), 101 ) 09/12/2004 select CONVERT(varchar(12) , getdate(), 103 ) 12/09/2004 select CONVERT(varchar(12) , getdate(), 104 ) 12.09.2004 select CONVERT(varchar(12) , getdate(), 105 ) 12-09-2004 select CONVERT(varchar(12) , getdate(), 106 ) 12 09 2004 select CONVERT(varchar(12) , getdate(), 107 ) 09 12, 2004 select CONVERT(varchar(12) , getdate(), 108 ) 11:06:08 select CONVERT(varchar(12) , getdate(), 109 ) 09 12 2004 1 select CONVERT(varchar(12) , getdate(), 110 ) 09-12-2004 select CONVERT(varchar(12) , getdate(), 113 ) 12 09 2004 1 select CONVERT(varchar(12) , getdate(), 114 ) 11:06:08.177 create table tb(dt varchar(10))insert into tb values('21/09/2010')go--用parsenameselect cast(parsename(replace(dt,'/','.'),1) + parsename(replace(dt,'/','.'),2)+parsename(replace(dt,'/','.'),3) as datetime) From tb/*------------------------------------------------------ 2010-09-21 00:00:00.000(所影响的行数为 1 行)*/--直接按位取select cast(right(dt,4) + substring(dt,4,2) + left(dt,2) as datetime) from tb/*------------------------------------------------------ 2010-09-21 00:00:00.000(所影响的行数为 1 行)*/drop table tb 只要告诉 sqlserver 你要处理的日期的 基本格式就ok了。set dateformat dmy该选项为 会话级别的 select convert(datetime,'21/09/2010',103) as date,这个在欧美一些地区也用日月年的形式。 请大家帮帮我,急! 关于SQL行转列语句优化的问题 改了SQL 的端口,用原来的连接字符串可以连接,加了端口却不能连接,不解,请高手帮助 关于存储过程取得返回值的问题? 请问这个SQL怎样写,急 转化的问题,,没有一点头绪.在线等待高手!。。 学生考勤系统 SQL Server 2005中如何自定义函数,实现类似Min或MAX函数功能。(支持者有分) 一个数据类型的问题。。 如何取得最大标识列的排列序号? 这个约束能做吗 关于sql server2000远程连接的问题,急问!!!!
select convert(datetime,'21/09/2010',103) as date/*
date
-----------------------
2010-09-21 00:00:00.000(1 行受影响)
*/
2004-09-12 11:06:08
select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')
20040912110608
select CONVERT(varchar(12) , getdate(), 111 )
2004/09/12
select CONVERT(varchar(12) , getdate(), 112 )
20040912
select CONVERT(varchar(12) , getdate(), 102 )
2004.09.12
其它我不常用的日期格式转换方法:
select CONVERT(varchar(12) , getdate(), 101 )
09/12/2004
select CONVERT(varchar(12) , getdate(), 103 )
12/09/2004
select CONVERT(varchar(12) , getdate(), 104 )
12.09.2004
select CONVERT(varchar(12) , getdate(), 105 )
12-09-2004
select CONVERT(varchar(12) , getdate(), 106 )
12 09 2004
select CONVERT(varchar(12) , getdate(), 107 )
09 12, 2004
select CONVERT(varchar(12) , getdate(), 108 )
11:06:08
select CONVERT(varchar(12) , getdate(), 109 )
09 12 2004 1
select CONVERT(varchar(12) , getdate(), 110 )
09-12-2004
select CONVERT(varchar(12) , getdate(), 113 )
12 09 2004 1
select CONVERT(varchar(12) , getdate(), 114 )
11:06:08.177
insert into tb values('21/09/2010')
go--用parsename
select cast(parsename(replace(dt,'/','.'),1) + parsename(replace(dt,'/','.'),2)+parsename(replace(dt,'/','.'),3) as datetime) From tb
/*
------------------------------------------------------
2010-09-21 00:00:00.000(所影响的行数为 1 行)
*/--直接按位取
select cast(right(dt,4) + substring(dt,4,2) + left(dt,2) as datetime) from tb
/*
------------------------------------------------------
2010-09-21 00:00:00.000(所影响的行数为 1 行)
*/drop table tb