SQL DATE to determine years

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • aamir01
    New Member
    • Oct 2007
    • 2

    SQL DATE to determine years

    I have this situation:
    Start date = 11/1/2007
    End date = 2/28/2008

    I have to organize date to determine which year of transaction they fall in. For example using above date, this record should fall in YEAR 1 as only 3 months have passed. If End date is 11/2/2008, it should fall in year 2. However, if I use DATEDIFF(yy,Sta rt date, End date) function it returns 1 for both records. How can I determine which year my record belongs to? I need to do this to organize data from 12 years.

    Thanks much!!
    Aamir.
  • aamir01
    New Member
    • Oct 2007
    • 2

    #2
    I figured out the solution by reading other threads under Fiscal Year issues.

    =============== =============== =============== =========

    Originally posted by aamir01
    I have this situation:
    Start date = 11/1/2007
    End date = 2/28/2008

    I have to organize date to determine which year of transaction they fall in. For example using above date, this record should fall in YEAR 1 as only 3 months have passed. If End date is 11/2/2008, it should fall in year 2. However, if I use DATEDIFF(yy,Sta rt date, End date) function it returns 1 for both records. How can I determine which year my record belongs to? I need to do this to organize data from 12 years.

    Thanks much!!
    Aamir.

    Comment

    • davash6
      New Member
      • Oct 2007
      • 9

      #3
      Originally posted by aamir01
      I have this situation:
      Start date = 11/1/2007
      End date = 2/28/2008

      I have to organize date to determine which year of transaction they fall in. For example using above date, this record should fall in YEAR 1 as only 3 months have passed. If End date is 11/2/2008, it should fall in year 2. However, if I use DATEDIFF(yy,Sta rt date, End date) function it returns 1 for both records. How can I determine which year my record belongs to? I need to do this to organize data from 12 years.

      Thanks much!!
      Aamir.
      U may try like this method using Month istead of Year...


      declare @diff int
      set @diff=datediff( month,'11/1/2007','2/28/2008')
      ----------set @diff=datediff( m,'11/1/2007','11/28/2009')
      if(@diff<12)
      begin
      print 'Fall in Year 1'
      end
      else if(@diff/12=1 and @diff/12<2 )
      begin
      print'Fall in Year 2'
      end
      else print'Fall in Year 3'

      Comment

      Working...