本文介绍了在两个日期之间选择sql server查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,





这是我的存储过程:



Hi all,


This is my stored procedure:

USE [NCP]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spTest]
(
	@DateFrom datetime,
	@DateTo datetime
)
as
begin
    set nocount on

    SELECT
		FORMAT(convert(datetime,SCHEDULE_NEXT_DATE,104), 'yyyy-MM-dd HH:mm:ss') AS SCHEDULE_NEXT_DATE
	FROM dbo.TestTable
	WHERE
		SAMPLE_TYPE = 'SampleSchedule'
		AND SCHEDULE_NEXT_DATE BETWEEN @DateFrom AND @DateTo
end





我的TestTable列如下所示:



SCHEDULE_NEXT_DATE varchar(MAX) - 允许空值

当我选择前1000行时数据看起来像这样



SCHEDULE_NEXT_DATE

21-04-2015 10:30

21-04-2015 10:35

22-04-2015 10:30

22-04-2015 10:35



当我用sql profiler检查查询时,我没有得到任何结果。任何人都可以帮我解决这个疑问吗?



exec spTest @ DateFrom ='2015-04-21 10:30:00',@ DateTo ='2015-04- 21 10:35:00'



结果:0行



My TestTable column looks like this:

SCHEDULE_NEXT_DATE varchar(MAX) - Allow Nulls
Data looks like this when i select the top 1000 rows

SCHEDULE_NEXT_DATE
21-04-2015 10:30
21-04-2015 10:35
22-04-2015 10:30
22-04-2015 10:35

When i check the query with sql profiler i get no result. can anyone help me with this query?

exec spTest @DateFrom='2015-04-21 10:30:00',@DateTo='2015-04-21 10:35:00'

Result: 0 rows

推荐答案

WHERE
    SAMPLE_TYPE = 'SampleSchedule'
AND
    Convert(datetime, SCHEDULE_NEXT_DATE, 104) BETWEEN @DateFrom AND @DateTo


这篇关于在两个日期之间选择sql server查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 20:13