locked
How to compare Two Dates(Parameter) with Two Dates(Database Columns) RRS feed

  • Question

  • Hello Readers. I m stuck with a problem. While writting query for Generating Fee Challan for a Class i came across a requirement where i have to generate the Fee Challan for a specific Class in a specific session in Specific Dates like. Class=1 Session=2009 and 01-March-2009 to 30-June-2009. These two dates have to be layed between two Database Fields i.e FromDateRange_Temp and ToDateRange_Temp.

     All Portion of query goes well but when it comes to dates i am halted and confused how to compare two dates with two dates 

    I am desperately waiting for your solution.
    Thank You
    Hamad Salahuddin
    Hamad Salahuddin
    Monday, October 26, 2009 11:01 AM

Answers

  • Try Below,

    SELECT *  FROM TableName
    WHERE Class = 1
    AND Session = '2009'
    AND DATEDIFF(Day, '01-March-2009', FromDateRange_Temp ) = 0
    AND DATEDIFF(Day, '30-June-2009', ToDateRange_Temp ) = 0


    OR


    SELECT *  FROM TableName
    WHERE Class = 1
    AND Session = '2009'
    AND date Between  '01-March-2009' AND '30-June-2009'

    OR

    SELECT *  FROM TableName
    WHERE Class = 1
    AND Session = '2009'
    AND date Between  FromDateRange_Temp AND ToDateRange_Temp

    Best Regards,
    Melissa Suciadi


    If you have found this post helpful, please click the 'Vote as Helpful' link (the green triangle and number on the top-left).

    If this post answers your question, click the 'Mark As Answered' link below. It helps others who experience the same issue in future to find the solution.

    • Marked as answer by Kalman Toth Thursday, October 29, 2009 6:09 AM
    Monday, October 26, 2009 11:10 AM
  • Hamad,
    try this..!!!
    Declare @variable1 datetime, @variable2 datetime

    select @variable1 ='01-March-2009 ', @variable2 = '30-June-2009.'

    Select * from table where FromDateRange >=@variable1 and ToDateRange <= @variable2

    The above reply is based on my understanding of your problem. post the query which you have written for this requirement.

    Thanks,
    Bharani M
    - Please mark the post as answered if it answers your question.

    • Proposed as answer by Bharani 3010 Monday, October 26, 2009 11:20 AM
    • Marked as answer by Kalman Toth Thursday, October 29, 2009 6:12 AM
    Monday, October 26, 2009 11:13 AM

All replies

  • Try Below,

    SELECT *  FROM TableName
    WHERE Class = 1
    AND Session = '2009'
    AND DATEDIFF(Day, '01-March-2009', FromDateRange_Temp ) = 0
    AND DATEDIFF(Day, '30-June-2009', ToDateRange_Temp ) = 0


    OR


    SELECT *  FROM TableName
    WHERE Class = 1
    AND Session = '2009'
    AND date Between  '01-March-2009' AND '30-June-2009'

    OR

    SELECT *  FROM TableName
    WHERE Class = 1
    AND Session = '2009'
    AND date Between  FromDateRange_Temp AND ToDateRange_Temp

    Best Regards,
    Melissa Suciadi


    If you have found this post helpful, please click the 'Vote as Helpful' link (the green triangle and number on the top-left).

    If this post answers your question, click the 'Mark As Answered' link below. It helps others who experience the same issue in future to find the solution.

    • Marked as answer by Kalman Toth Thursday, October 29, 2009 6:09 AM
    Monday, October 26, 2009 11:10 AM
  • Declare 
    
    @Class int,
    
    @Session int,
    
    @FromDate Datetime,
    
    @ToDate DateTime
    
     
    
    Set @Class=1
    
    Set @Session=2009
    
    Set @FromDate='01 mar 2009'
    
    Set @ToDate='30 Jun 2009'
    
    Select * from Table_name 
    
    Where
    
    Class=@Class AND
    
    Session=@Session AND
    
    FromDateRange_Temp <=@FromDate AND
    
    ToDateRange_Temp >=ToDateRang
    
    Monday, October 26, 2009 11:11 AM
  • Hamad,
    try this..!!!
    Declare @variable1 datetime, @variable2 datetime

    select @variable1 ='01-March-2009 ', @variable2 = '30-June-2009.'

    Select * from table where FromDateRange >=@variable1 and ToDateRange <= @variable2

    The above reply is based on my understanding of your problem. post the query which you have written for this requirement.

    Thanks,
    Bharani M
    - Please mark the post as answered if it answers your question.

    • Proposed as answer by Bharani 3010 Monday, October 26, 2009 11:20 AM
    • Marked as answer by Kalman Toth Thursday, October 29, 2009 6:12 AM
    Monday, October 26, 2009 11:13 AM
  • Hello Hamad,

    Can you look at the following query if I understand it correctly:

    select *
    from <table name>
    where Class = 1
    and Session = 2009
    and (FromDateRage_Temp >= convert(datetime,'01-March-2009') and ToDateRange_Temp <= convert(datetime, '30-June-2009'))
    Kiran (www.ggktech.com)
    Monday, October 26, 2009 11:14 AM