Results 1 to 4 of 4

Thread: Searching Dates in Query Analyzer

  1. #1
    Join Date
    Oct 2002
    Posts
    5

    Searching Dates in Query Analyzer

    Hello Gang,

    I have a strange problem that I haven't dealt with before.

    I need to execute a piece of code based on date ranges. If the date range is:

    Scenario 1:between 02/28 (Feb 28) and 07/31 (July 31) do x
    -----------------------------------------------------------
    Scenario 2:between 08/01 (Aug 1) and 01/31 (Jan 31) do y

    Can anyone help me with this code. I am having a SCD (Stupid Coder Day) and can't seem to do anything that is scalable - like accounting for leap years (Feb issue) and the fact that Scenario # 2 above is between 2 different calender years.

    Your help is much appreciated.

    JJOSHI

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    Need more details.

  3. #3
    Join Date
    Dec 2004
    Posts
    502
    Like rmiao said, we need more information. Why are you having problems? What are you basing your date ranges on? If you need code to determine beginning and ending dates in a month, you can do this for example:

    SELECT DATEADD(month, DATEDIFF(month, 0, getdate()), 0)
    SELECT DATEADD(month, DATEDIFF(month, -1, getdate()), -1)

    This will give you the beginning and ending dates for the current month. I'm not sure why a leap year and crossing over into the next year are problems for you right now.

  4. #4
    Join Date
    Oct 2002
    Posts
    5
    Well, I am trying to automate a report. The report is supposed to generate a result that will differ based on the date ranges going into the future. E.g.

    [1]. If the run date of the report is between '2/1/20xx' and '7/31/20xx' display <ABC> or

    [2]. If the run date of the report is between '8/1/20xx' and '1/31/20xx' display <PQR>

    In example # 2. I am moving from one year to the next (July to Dec and the one extra month of Jan). So for example, if the guy runs the report between August of 2008 and January of 2009, display <PQR>.

    How do I achieve both # 1 & 2 above in a code? Does this explain better.

    Much appreciated once again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •