首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

【提问】一个Sql话语

2013-10-24 
【提问】一个Sql语句表AIddate12013-10-40 00:00:0022013-10-0112:00:0032014-01-01 12:09:34select * from

【提问】一个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


select * from A
where Isdate(date) = 1
and Convert(datetime,date) >= '2014-01-01 00:00:00'

需要结果
2014-01-01 12:09:34 
---1行结果受影响


如果运行以上语句会报错,无法将varchar转换成datetime,超出长度限制。,,,
急急,,,,,,,,,,,
[解决办法]

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)

[解决办法]
一开始运行会报错,因为你的where中用了convert函数,把字符串转化为日期型,但肯定有数据不符合日期型,后面通过isdate函数,找到了有问题的日期数据,第一条数据是有问题的:2013-10-40 00:00:00,
日期最多到31号,没有40号的。


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 行)

[解决办法]
日期和字符型不能隐式转换,需要显式convert,把匹配的左右两端设为类型一致
----------------------------------------------------------------
-- 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
*/

[解决办法]
引用:
Quote: 引用:

一开始运行会报错,因为你的where中用了convert函数,把字符串转化为日期型,但肯定有数据不符合日期型,后面通过isdate函数,找到了有问题的日期数据,第一条数据是有问题的:2013-10-40 00:00:00,
日期最多到31号,没有40号的。


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 行受影响)
*/

我知道那条数据是有问题的,就是会有这种需求,date的数据类型是varchar,里面可能存了非日期的数据,所以条件判断的时候,要过滤掉非日期的。这个就是我的需求啊,,


呵呵,明白了,这样试试:


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
*/


[解决办法]
可以不用convert
select * from #A where ISDATE([date])=1 and [date] >='2014-01-01 00:00:00' 
[解决办法]

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)
*/

热点排行