拙建
用两个datetimtpicker1,2 分别选择8月1日和8月29日
一个str:string
adoquery.close;
adoquery.clear
adoquery.add('where str>'''+datetostr(datetimtpicker1)+'''');
adoquery.add('and str<''' +datetostr(datetimtpicker2)+'''');
adoquery.open;
begin
while not adoquery.first do
begin
adoquery.delete;
adoquery.next;
end;
end;
用两个datetimtpicker1,2 分别选择8月1日和8月29日
一个str:string
adoquery.close;
adoquery.clear
adoquery.add('where str>'''+datetostr(datetimtpicker1)+'''');
adoquery.add('and str<''' +datetostr(datetimtpicker2)+'''');
adoquery.open;
begin
while not adoquery.first do
begin
adoquery.delete;
adoquery.next;
end;
end;
--创建测试表
CREATE TABLE TestTable
(iNo INT IDENTITY(1,1),vField VARCHAR(1000))--插入测试记录
INSERT INTO TestTable
(vField)
VALUES('2004-08-12 12:04:30 qqaa' )INSERT INTO TestTable
(vField)
VALUES('2004-05-12 12:04:30 qqaa' )--删除2004年8月的记录
DELETE TestTable WHERE vField LIKE '%2004-08%'DROP TABLE TestTable
CREATE TABLE TestTable
(iNo INT IDENTITY(1,1),vField VARCHAR(1000))--插入测试记录
INSERT INTO TestTable
(vField)
VALUES('2004-08-12 12:04:30 qqaa' )INSERT INTO TestTable
(vField)
VALUES('2004-05-12 12:04:30 qqaa' )INSERT INTO TestTable
(vField)
VALUES('2004-08-30 12:04:30 qqaa' )INSERT INTO TestTable
(vField)
VALUES('2004-08-31 12:04:30 qqaa' )--删除2004年8月的记录
DELETE TestTable WHERE vField LIKE '%2004-08%' AND vField NOT LIKE '%2004-08-30%' AND vField NOT LIKE '%2004-08-31%'DROP TABLE TestTableSELECT * FROM TestTable
DELETE TestTable WHERE vField LIKE '%2004-08%' AND vField NOT LIKE '%2004-08-3%'
drop table [dbo].[table1]
create table table1(ID int not null identity primary key ,key_date varchar(30) not null)goinsert into table1
select '2004-08-01 12:04:30 qqaa ' union all
select '2004-08-12 12:04:30 qqaa ' union all
select '2004-08-23 01:04:30 qqbb ' union all
select '2004-08-18 16:04:30 bbaa ' union all
select '2004-08-30 12:09:40 qqca ' union all
select '2004-08-31 11:09:40 qqcf ' union all
select '2004-08-31 12:06:40 qqcg '
goselect * from table1
/*
ID key_date
1 2004-08-01 12:04:30 qqaa
2 2004-08-12 12:04:30 qqaa
3 2004-08-23 01:04:30 qqbb
4 2004-08-18 16:04:30 bbaa
5 2004-08-30 12:09:40 qqca
6 2004-08-31 11:09:40 qqcf
7 2004-08-31 12:06:40 qqcg */
----------方法一-------
delete from table1 where convert(datetime,left(key_date,10))>='2004-08-01' and convert(datetime,left(key_date,10))<='2004-08-30'----------方法二-------
delete from table1 where key_date like '2004-08-[012]%' or key_date like '2004-08-30%'select * from table1
/*
ID key_date
6 2004-08-31 11:09:40 qqcf
7 2004-08-31 12:06:40 qqcg */
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[table1]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[table1]
create table table1(ID int not null identity primary key ,key_date varchar(30) not null)goinsert into table1
select '2004-08-01 12:04:30 qqaa ' union all
select '2004-08-12 12:04:30 qqaa ' union all
select '2004-08-23 01:04:30 qqbb ' union all
select '2004-08-18 16:04:30 bbaa ' union all
select '2004-08-30 12:09:40 qqca ' union all
select '2004-08-31 11:09:40 qqcf ' union all
select '2004-08-31 12:06:40 qqcg '
goselect * from table1
/*
ID key_date
1 2004-08-01 12:04:30 qqaa
2 2004-08-12 12:04:30 qqaa
3 2004-08-23 01:04:30 qqbb
4 2004-08-18 16:04:30 bbaa
5 2004-08-30 12:09:40 qqca
6 2004-08-31 11:09:40 qqcf
7 2004-08-31 12:06:40 qqcg */
----------方法一-------
delete from table1 where convert(datetime,left(key_date,10))>='2004-08-01' and convert(datetime,left(key_date,10))<='2004-08-29'----------方法二-------
delete from table1 where key_date like '2004-08-[012]%'
select * from table1
/*
ID key_date
5 2004-08-30 12:09:40 qqca
6 2004-08-31 11:09:40 qqcf
7 2004-08-31 12:06:40 qqcg */