假设表A中有一个字段birthday,格式为varchar(50),
里面既有 2012-6-5 这样格式,也有 7/28/2006这样的格式。
用select Convert(datetime,birthday) from XXX
和select Cast(birthday as datetime) from XXX
都出现以下的错误:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
发现 7/28/2006 格式的字符串无法转换。请问如何将这列转换成日期格式?
里面既有 2012-6-5 这样格式,也有 7/28/2006这样的格式。
用select Convert(datetime,birthday) from XXX
和select Cast(birthday as datetime) from XXX
都出现以下的错误:
The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.
发现 7/28/2006 格式的字符串无法转换。请问如何将这列转换成日期格式?
-- 日期转换参数大全
--********************************************************************************
select CONVERT(varchar,getdate(),120)
--2009-03-15 15:10:02select CONVERT(varchar(10),getdate(),120)
--2009-03-15select replace(replace(replace(CONVERT(varchar, getdate(), 120 ),'-',''),' ',''),':','')
--20090315151201select CONVERT(varchar(12) , getdate(), 111)
--2009/03/15select CONVERT(varchar(12) , getdate(), 112)
--20090315select CONVERT(varchar(12) , getdate(), 102)
--2009.03.15select CONVERT(varchar(12) , getdate(), 108)
--15:13:26其它我不常用的日期格式转换方法:select CONVERT(varchar(12) , getdate(), 101 )
--03/15/2009select CONVERT(varchar(12) , getdate(), 103 )
--15/03/2009select CONVERT(varchar(12) , getdate(), 104 )
--15.03.2009select CONVERT(varchar(12) , getdate(), 105 )
--15-03-2009select CONVERT(varchar(12) , getdate(), 106 )
--15 03 2009select CONVERT(varchar(12) , getdate(), 107 )
-- 15, 2009select CONVERT(varchar(12) , getdate(), 109 )
--03 15 2009 select CONVERT(varchar(12) , getdate(), 110 )
--03-15-2009select CONVERT(varchar(11) , getdate(), 113 )
--15 03 2009 select CONVERT(varchar(12) , getdate(), 114)
--15:15:55:717
你自己试试 select Cast('7/28/2006' as datetime) or select convert(datetime,'7/28/2006') 应该是其它值有问题
如果yes
先转换第一种 2012-6-5
UNION ALL
第二种 7/28/2006
问题解决。