Results 1 to 4 of 4

Thread: How to select previous month's date?

  1. #1
    Join Date
    Jan 2009
    Posts
    97

    How to select previous month's date?

    Hi,

    My job profile includes each day observation. I want to create new tables, which will contains the same data, but only the last previous observation from each month.


    I used below code:


    Select * into AB_CAD_M from AB_CAD where ObsDateCAD = ?

    Do you aware about some method to select this last date from previous month using SQL query? Please help me on this.
    Last edited by Saaz; 14-11-2009 at 02:13 PM.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: How to select previous month's date?

    Please use below method:


    Select P1.*
    into AB_CAD_M
    from AB_CAD as P1 inner join
    (
    select
    dateadd(month,datediff(month,-1,ObsDateCAD ),-1) as Obs_Date_CAD1,
    dateadd(month,datediff(month,-1,ObsDateCAD ),0) as Obs_Date_CAD2

    from AB_CAD

    group by ObsDateCAD

    ) as t2
    on t1.ObsDateCAD >= P2.Obs_Date_CAD1 and P1.ObsDateCAD < P2.Obs_Date_CAD2

  3. #3
    Join Date
    Apr 2008
    Posts
    2,005

    smile Re: How to select previous month's date?

    If your problem not yet solved then please use below query:

    where ObsDateCAD = (select max(ObsDateCAD) from CAD group by Year(ObsDateCAD),Month(ObsDateCAD))


    //In above query the group by function group the records according to year and month

    hopefully this will help you.

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: How to select previous month's date?

    Below query will definitely help you in your problem:

    select colmn1, colmn2, ...
    from
    (select
    row_num() over (partition by ObsDateCAD order by ObsDateCAD desc) as rownumber,
    *
    from CAD A1
    where A1.ObsDateCAD=
    (
    select
    max(ObsDateCAD) as ObsDateCAD
    from CAD
    where year(A1.ObsDateCAD)=year(ObsDateCAD)
    and month(A1.ObsDateCAD)=month(ObsDateCAD)
    )
    )dt
    where rownumber=1

Similar Threads

  1. calculation of date, month and year using excel
    By Edgar-Arular in forum Windows Software
    Replies: 4
    Last Post: 06-04-2011, 04:26 PM
  2. Replies: 4
    Last Post: 14-05-2010, 12:16 AM
  3. Restore windows xp to previous date
    By chaosblade in forum Operating Systems
    Replies: 3
    Last Post: 20-08-2009, 10:35 AM
  4. Select the last 10 records related to a date
    By windows_user in forum Software Development
    Replies: 5
    Last Post: 22-04-2009, 07:55 PM
  5. How to Retrive data of the Previous month with Loop
    By StudyBoy in forum Software Development
    Replies: 2
    Last Post: 27-02-2009, 11:11 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,565,715.59844 seconds with 16 queries