sql query

ITsolutionWizard
ITsolutionWizard used Ask the Experts™
on
I have c# aspx.net with ms SQL server and have one question about SQL

assume I have a web form and come with start date and end date. the format is 20181201 and 20181231 and they are an integer.
and I have to run a SQL statement like below.

select top 1 * from transacation_history where deposit_date >= 20181201 and deposit <= 20181231
Or
I should use between statement? but I do not know how to do that.
1. Which one run faster?
2. I will convert it into stored procedure. In this logic, is it best practice in ms sql?

Thanks.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
lcohanDatabase Analyst

Commented:
Are there truly two different columns in the above statement? Or rather the same column and it was just typo?
Assuming is just typo if you check the query plans for the two statements below you may see that actually between is converted to GTE/LTE

select top 1 * from transacation_history where deposit_date >= 20181201 and deposit_date <= 20181231;
select top 1 * from transacation_history where deposit_date  between 20181201 and 20181231;


Just make sure index exists on the deposit_date
Senior DBA
Most Valuable Expert 2018
Top Expert 2014
Commented:
1) BETWEEN will be converted to >= and <=, so there will be no performance difference between them.
2) Yes, that is standard best practice in SQL Server.

Author

Commented:
just typo sorry.
I have one column in the table

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial