Click here to Skip to main content
16,021,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

Please help me,
I have table in sqlsever with three different columns like day, month, and year. and in this columns values are interger respectively. table structure like as below:

Day Month Year
3 10 2011
4 11 2011
5 12 2012
6 1 2012

i passed the value from my application day, month and year from dropdown.
now i want to retrive data from this.
suppose i passed the From value day-3 month-10 year-2011 and To value day-6 month-1 year-2012. how to use between for this to retrive records.


Thanks in advance....
Posted

Trymsomething like -
select * from table where CAST(DATEPART(year, DATE)+'-'+ DATEPART(month, DATE) +'-'+ DATEPART(day, DATE) AS DATETIME) > @startDate AND CAST(DATEPART(year, DATE)+'-'+ DATEPART(month, DATE) +'-'+ DATEPART(day, DATE) AS DATETIME)< @endDate

where @startDate and @endDate are your date time parameters.
 
Share this answer
 
Comments
dA.d 20-Mar-13 1:13am    
thanks for replay....
above part i used in my stored procedure but showing error can u show me how can i put the values in above query
Abhinav S 20-Mar-13 2:07am    
For a stored procedure, you can pass @startDate and @endDate as parameters.
These will be your ranges for comparing the date.
SQL
select * from table
where
convert(datetime,(convert(varchar(4),year) + '-' + convert(varchar(2),month) + '-' + convert(varchar(2),day))) between '2011-10-03' and '2012-01-06'

Happy Coding!
:)
 
Share this answer
 
Hi ,

Try This Query..

SQL
SELECT Column_Name 
FROM Table_Name
WHERE CONVERT(VARCHAR(10),(CAST(YEAR_Column AS VARCHAR(4))+'-'+CAST(Month_Column AS VARCHAR(4))+'-'+CAST(Day_Column AS VARCHAR(4))), 112) 
    BETWEEN CONVERT(VARCHAR(10),From_Date, 112) AND CONVERT(VARCHAR(10),To_Date, 112)

GVPrabu
 
Share this answer
 
v2

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