Skip to content
Loading
How to Use date variable inside sql query
  • Amit Gupta
    Just you need to use NOT BETWEEN like
     
    1. SELECT *  
    2.   FROM TableName  
    3.  WHERE ColumnName NOT BETWEEN @startDate and @endDate  
     
    Change your tablename and column name as required. Hence, all required procedure should be same. 
    +2
  • Thanks for you reply.I am developing a hotel reservation application.I am trying to find room numbers not beetween two dates.can you help me for that?
    0
  • Amit Gupta
    It would be better, if you have share your code. 
    Eg.
     
    1. string query = @"select top 1 OrderNumber   
    2.                 from tblOrderMaster   
    3.                 where OrderedDate BETWEEN @startDate AND @endDate";  
    4. using(SqlConnection conn = new SqlConnection("connectionString here"))  
    5. {  
    6.     using(SqlCommand cmd = new SqlCommand())  
    7.     {  
    8.         cmd.Connection = conn;  
    9.         cmd.CommandText = query;  
    10.         cmd.Parameters.AddWithValue("@startDate", txtfromcal.Text);  
    11.         cmd.Parameters.AddWithValue("@endDate", txttocal.Text);  
    12.         try  
    13.         {  
    14.             conn.Open();  
    15.             // other codes  
    16.             // to fetch the record  
    17.         }  
    18.         catch(SqlException e)  
    19.         {  
    20.             // do something with   
    21.             // e.ToString()  
    22.         }  
     
    Ref 
    http://stackoverflow.com/questions/14353434/selecting-data-between-2-dates
    0