Click here to Skip to main content
15,884,676 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
transaction table their is a column called date

whenever a trans made the date of trans will be inserted in that column

i need sql query to display trans between two date (from -> to )
Posted

Use the BETWEEN[^] function

e.g.

SQL
CREATE PROCEDURE Select_From_MyTable
(
	@FromDate DATETIME,
	@TODate DATETIME
)

AS

SELECT
	*
FROM
	MY_TABLE 
WHERE 
	MY_DATE BETWEEN @FromDate AND  @TODate


[Edit]Forgot to mention that the between function is inclusive so will include the @FromDate and @ToDate values
 
Share this answer
 
v2
As Reiss says, you can use BETWEEN, or you can simply say
SQL
SELECT * FROM myTable WHERE [date] >= '2011-07-21' AND [date] <= '2011-07-28'
The later form is more flexible, in that it allows exclusive dates and well as inclusive - BETWEEN always includes both the start and end dates in the calculation.
 
Share this answer
 
Comments
Reiss 28-Jul-11 4:14am    
I agree that you have more flexability in your approach, as you can make it non-inclusive if you wish.

This article (http://stackoverflow.com/questions/921282/compare-performance-difference-of-t-sql-between-and-operator) suggests that the execution plan is the same (I haven't tested this), so performance shouldn't be an issue either way
OriginalGriff 28-Jul-11 4:18am    
BETWEEN is a nicer solution - it is easier to read and thus maintain - but you often need Greater-than and less-than-or-equal-to so I thought I'd add the other method so the OP didn't try to twist his data to fit the solution! :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900