Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a table named tblDailyMaintenceDetails
The query is as follows :
SQL
select * from tblDailyMaintenceDetails  where MaintenceDate between '28-Dec-2008'and '06-Apr-2013'

Is it possible to show the 'fromdate' and 'todate' into the query result that I mentioned in where condition?
Please help me as early as possible.
Posted
Updated 10-Mar-13 1:00am
v2

This is the simplest way:
SQL
select *, CAST('28-Dec-2008' as DATE) as fromdate, CAST('06-Apr-2013' as DATE) as todate from tblDailyMaintenceDetails where MaintenceDate between '28-Dec-2008'and '06-Apr-2013'
 
Share this answer
 
Comments
Maciej Los 10-Mar-13 7:01am    
+5!
Yes - but you would need to change your query slightly. I assume you are issuing this from a langauge of some form, rather than entering directly into SQL yourself, in which case, pass the start and end dates as parameters to the SqlCommand (I'll show it in C#):
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT *, @STARTDATE AS StartDate. @ENDDATE AS EndDate FROM tblDailyMaintenceDetails WHERE MaintenceDate BETWEEN @STARTDATE AND @ENDDATE", con))
        {
        com.Parameters.AddWithValue("@STARTDATE", new DateTime(2008, 12, 28));
        com.Parameters.AddWithValue("@ENDDATE", new DateTime(2013, 4, 6));
        using (SqlDataReader reader = com.ExecuteReader())
            {
            while (reader.Read())
                {
                ...
                }
            }
        }
    }
You don't need to use parameters, it just makes it easier to keep the two dates the same in both parts of the SQL
 
Share this answer
 
Comments
Maciej Los 10-Mar-13 7:01am    
+5!

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