如何在where子句中传递datetime参数? [英] How to pass datetime parameter in where clause?

查看:115
本文介绍了如何在where子句中传递datetime参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个查询来根据条件检索记录。输入参数是datetime datetype。



我的查询是



从tblname中选择*其中fromdate =''parameter1''和todate = ''parameter2''



输入值是



1.参数1 =''26/04 / 2013 00:00:00''

2. Paramater2 =''27 / 04/2013 00:00:00''



如果我传递''26 -APR-13''和'27 / 03/13'这样的参数,我可以检索记录。但是,如果我尝试传递直接输入,我没有得到它。我尝试使用TO_DATE(Parameter1,''DD / MM / yyyy hh24:mi:ss'')函数。即使这样我也无法找回它。





有人请帮我解决这个问题吗?

Hi, i am writing a query to retrieve the records based on the condition. the input parameters are datetime datetype.

My query is

Select * from tblname where fromdate = ''parameter1'' and todate = ''parameter2''

Input values are

1. Parameter1 = ''26/04/2013 00:00:00''
2. Paramater2 = ''27/04/2013 00:00:00''

If i pass the parameters like ''26-APR-13'' and ''27/03/13'', i can retrieve the records. But if i try to pass direct input, I am not getting it. I tried by using TO_DATE(Parameter1 , ''DD/MM/yyyy hh24:mi:ss'') function. Even then i cannot retrieve it.


Anyone please help me to resolve this?

推荐答案

Hi, 

--Convert any date input to the stored procedure to datetime
--Example:

declare @tempStartDate datetime
declare @tempEndDate datetime

select @tempStartDate = convert(datetime, @InputStartDate)
select @tempEndDate = convert(datetime, @InputEndDate)

--write the query to retrieve records
select * from dbo.TempTable where fromDate >= @tempStartDate and toDate <= @tempEndDate

--of course, your data will be displayed as result based on whatever value records you have (fromDate and toDate with time component or without time component)





希望这会有所帮助。



快乐编码.. !!



Hope this helps.

Happy coding..!!


这篇关于如何在where子句中传递datetime参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆