现在我有一段SQL语句,操控系统表pubs:
select * from titles where pubdate>'27/4/2009 00:00:01'
这样书写会出错,提示从 char 数据类型到 datetime 数据类型的转换导致 datetime 值越界。
问题出现在'27/4/2009 00:00:01'这样一个字符串上,如果换成'2009-4-27 00:00:01'则不会出错请问,如何将'27/4/2009 00:00:01'转换成'2009-4-27 00:00:01',我使用了Convert(varchar(30),'27/4/2009 00:00:01',102)等都还是有问题,请问如何修改,102换成其他的也是有问题
select * from titles where pubdate>'27/4/2009 00:00:01'
这样书写会出错,提示从 char 数据类型到 datetime 数据类型的转换导致 datetime 值越界。
问题出现在'27/4/2009 00:00:01'这样一个字符串上,如果换成'2009-4-27 00:00:01'则不会出错请问,如何将'27/4/2009 00:00:01'转换成'2009-4-27 00:00:01',我使用了Convert(varchar(30),'27/4/2009 00:00:01',102)等都还是有问题,请问如何修改,102换成其他的也是有问题
select CONVERT(varchar,getdate(),120)
--2009-03-15 15:10:02select 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 * from titles where pubdate>convert(varchar(18),'27/4/2009 00:00:01',121)
select isdate('27/4/2009 00:00:01' )
---------------------------
0select cast('27/4/2009 00:00:01' as smalldatetime)
---------------------------------
Server: Msg 296, Level 16, State 3, Line 1
The conversion of char data type to smalldatetime data type resulted in an out-of-range smalldatetime value.
轉型時提示越界了.樓主你這樣的字符串系統竟認為不是符合日期類型的字符串
set @s='27/4/2009 00:00:01'
select
cast(parsename(replace(left(@s,charindex(' ',@s)-1),'/','.'),1)
+'-'+parsename(replace(left(@s,charindex(' ',@s)-1),'/','.'),2)
+'-'+parsename(replace(left(@s,charindex(' ',@s)-1),'/','.'),3)
+' '+right(@s,8) as datetime)
/*-----------------------
2009-04-27 00:00:01.000(1 行受影响)
*/
set @t='27/4/2009 00:00:01'select convert(
datetime,
convert(char(10),
convert(datetime,left(@t,charindex(' ',@t)),103),
120)
+stuff(@t,1,charindex(' ',@t)-1,''),
120)
/*
------------------------------------------------------
2009-04-27 00:00:01.000(所影响的行数为 1 行)*/
set @t='27/4/2009 00:00:01'select convert( datetime,
--截取27/4/2009,并转换为2009-4-27的形式
convert(char(10),
convert(datetime,
left(@t,charindex(' ',@t)),
103),
120)
--截取00:00:01
+stuff(@t,1,charindex(' ',@t)-1,''),
120)
我来写个复杂的:
declare @v varchar(18)
set @v='27/4/2009 00:00:01'select cast( (right(substring(@v,1,patindex('% %',@v)),patindex('% %',@v)-patindex('%/%', stuff(@v,patindex('%/%',@v),1,' ')))+'-'+
substring(@v,patindex('%/%',@v)+1,patindex('%/%', stuff(@v,patindex('%/%',@v),1,''))-patindex('%/%',@v))
+'-'+substring(@v,1,patindex('%/%',@v)-1)+ ' '+substring(@v,patindex('% %',@v),len(@v))) as datetime)
declare @t varchar(18)
set @t='27/4/2009 00:00:01'
select cast(convert(varchar(10),convert(datetime,left(@t,patindex('% %',@t)),103),120)+
substring(@t,charindex(' ',@t),len(@t))
as datetime)
'27/4/2009'这个是通过DATE()函数获取的,一般这个函数获取的格式是'2009-4-27'
可现在某个系统获取的格式确实是'27/4/2009',而这个正如5楼所说,系统不让,提示越界!