【提问】一个Sql语句
表A
Id date
1 2013-10-40 00:00:00
2 2013-10-01 12:00:00
3 2014-01-01 12:09:34
create table A(Id int, date datetime)
insert into A
select 1, '2013-10-04 00:00:00' union all
select 2, '2013-10-01 12:00:00' union all
select 3, '2014-01-01 12:09:34'
select * from A
where Isdate(date) = 1
and Convert(datetime,date) >= '2014-01-01 00:00:00'
----------------------------
Id date
----------- -----------------------
3 2014-01-01 12:09:34.000
(1 row(s) affected)
if object_id('a') is not null
drop table a
go
create table A(Id int, date varchar(30))
insert into A
select 1, '2013-10-40 00:00:00' union all
select 2, '2013-10-01 12:00:00' union all
select 3, '2014-01-01 12:09:34'
--报错了
select * from A
where Isdate(date) = 1
and Convert(datetime,date) >= '2014-01-01 00:00:00'
/*
Id date
----------- ------------------------------
消息 242,级别 16,状态 3,第 10 行
从 varchar 数据类型到 datetime 数据类型的转换产生一个超出范围的值。
*/
--找到有问题的数据
select *,
case when isdate(date) = 1 then '是日期'
when isdate(date) = 0 then '不是日期'
end
from A
--where Isdate(date) = 1
--and Convert(datetime,date) >= '2014-01-01 00:00:00'
/*
Id date
----------- ------------------------------ --------
1 2013-10-40 00:00:00 不是日期
2 2013-10-01 12:00:00 是日期
3 2014-01-01 12:09:34 是日期
(3 行受影响)
*/
----------------------------------------------------------------
-- Author :fredrickhu(小F,向高手学习)
-- Date :2013-10-22 22:50:08
-- Version:
-- Microsoft SQL Server 2008 (SP3) - 10.0.5500.0 (Intel X86)
--Sep 22 2011 00:28:06
--Copyright (c) 1988-2008 Microsoft Corporation
--Enterprise Edition on Windows NT 6.1 <X86> (Build 7601: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([Id] int,[date] varchar(50))
insert [tb]
select 1,'2013-10-10 00:00:00' union all
select 2,'2013-10-01 12:00:00' union all
select 3,'2014-01-01 12:09:34'
--------------开始查询--------------------------
select
*
from
(select
*
from
tb
where
Isdate(date)=1)t
where
Convert(datetime,date) >= '2014-01-01 00:00:00'
select * from tb
where Isdate(date) = 1
and Convert(datetime,date) >= '2014-01-01 00:00:00'
--select * from tb
----------------结果----------------------------
/*
(3 行受影响)
Id date
----------- --------------------------------------------------
3 2014-01-01 12:09:34
(1 行受影响)
Id date
----------- --------------------------------------------------
3 2014-01-01 12:09:34
(1 行受影响)
*/
create table #tb(Id int, date varchar(30))
insert into #tb
select 1, '2013-10-40 00:00:00' union all
select 2, '2013-10-01 12:00:00' union all
select 3, '2014-01-01 12:09:34'
select *
from #tb
where Isdate(date) = 1 and Convert(varchar(19),date,120) >= '2014-01-01 00:00:00'
drop table #tb
/*
32014-01-01 12:09:34
*/
(所影响的行数为 1 行)
----------------------------------------------------------------
-- Author :DBA_Huangzj(發糞塗牆)
-- Date :2013-10-23 07:33:55
-- Version:
-- Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64)
--Dec 28 2012 20:23:12
--Copyright (c) Microsoft Corporation
--Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据:[A]
if object_id('[A]') is not null drop table [A]
go
create table [A]([Id] int,[date] varchar(30))
insert [A]
select 1,'2013-10-40 00:00:00' union all
select 2,'2013-10-01 12:00:00' union all
select 3,'2014-01-01 12:09:34'
--------------开始查询--------------------------
select * from A
where Isdate(date) = 1
and Convert(VARCHAR(30),[date],121) >='2014-01-01 00:00:00'
----------------结果----------------------------
/*
Id date
----------- ------------------------------
3 2014-01-01 12:09:34
*/
if object_id('a') is not null
drop table a
go
create table A(Id int, date varchar(30))
insert into A
select 1, '2013-10-40 00:00:00' union all
select 2, '2013-10-01 12:00:00' union all
select 3, '2014-01-01 12:09:34'
select * from A
where Isdate(date) = 1
and Convert(VARCHAR(20),[date],121) >='2014-01-01 00:00:00'
/*
Iddate
32014-01-01 12:09:34
*/
create table 表A
(Id int, [date] varchar(30))
insert into 表A
select 1, '2013-10-40 00:00:00' union all
select 2, '2013-10-01 12:00:00' union all
select 3, '2014-01-01 12:09:34'
select Id,[date]
into #t
from 表A
where isdate([date])=1
select Id,[date]
from #t
where convert(datetime,[date])>=cast('2014-01-01 00:00:00' as datetime)
/*
Id date
----------- ------------------------------
3 2014-01-01 12:09:34
(1 row(s) affected)
*/