Declare @FromDate as datetime,
@ToDate as datetime
set @FromDate = '2000-01-01',
@ToDate = '2001-01-01'
Select *
from Mytable
Where
Convert(varchar,ses_begin_date,101) >= Convert(varchar,@FromDate, 101)
AND Convert(varchar,ses_begin_date, 101) <= Convert(varchar,@ToDate,101)
虽然其中已有数据,但不返回任何数据。
答案 0 :(得分:1)
您不需要CONVERSION。只需使用此
Declare @FromDate as datetime,
@ToDate as datetime
set @FromDate = '2000-01-01',
@ToDate = '2001-01-01'
Select *
from Mytable
Where
ses_begin_date>= @FromDate
AND ses_begin_date<= @ToDate
此外,如果您的目标是获取2000年的所有数据,请使用
Declare @FromDate as datetime,
@ToDate as datetime
set @FromDate = '2000-01-01',
@ToDate = '2001-01-01'
Select *
from Mytable
Where
ses_begin_date>= @FromDate
AND ses_begin_date< @ToDate
答案 1 :(得分:0)
Select *
from Mytable
Where
ses_begin_date>='2000-01-01'
AND ses_begin_date<= '2001-01-01'
答案 2 :(得分:0)
使用DateDiff功能
Select *
From Table
Where Datediff(day,@datefrom, ses_begin_date)<=0
and Datediff(day,ses_begin_date,@dateto)<=0
答案 3 :(得分:0)
如果您使用的SQL Server高于2005,即。 2008年或以上比此查询 将导致日期范围内的所有记录。
您没有获得记录,因为您使用的是“datetime”,其中还包括可能不会产生所有必需记录的时间部分。但是,如果您只使用日期部分,则应比较时将其转换为“datetime”或“date”类型。
这应该有用。
DECLARE
@FromDate DATE ,
@ToDate DATE;
SELECT @FromDate = '2000-01-01',
@ToDate = '2001-01-01'
要么
SELECT
*
FROM
@temp
WHERE
(CONVERT(DATE, ses_begin_date) >= @FromDate
AND CONVERT(DATE, ses_begin_date) <= @ToDate)
或者
SELECT
*
FROM
@temp
WHERE
(CONVERT(DATE, ses_begin_date) BETWEEN @FromDate AND @ToDate).
如果您使用的版本低于2008,则可能将其转换为“int”以删除日期部分而不是进行比较。即
DECLARE
@FromDate int ,
@ToDate int;
SELECT @FromDate = CONVERT(INT, CONVERT(VARCHAR(10),'2000-01-01',112)),
@ToDate = CONVERT(INT, CONVERT(VARCHAR(10),'2001-01-01',112))
- 要么
SELECT
*
FROM
@temp
WHERE
(CONVERT(INT, CONVERT(VARCHAR(10), ses_begin_date, 112)) >= @FromDate
AND CONVERT(INT, CONVERT(VARCHAR(10), ses_begin_date, 112)) <= @ToDate)
- 或
SELECT
*
FROM
@temp
WHERE
(CONVERT(INT, CONVERT(VARCHAR(10), ses_begin_date, 112)) BETWEEN @FromDate AND @ToDate)