Results 1 to 4 of 4
  1. #1
    Join Date
    Dec 2008
    Posts
    2

    Question Unanswered: Help a noob fix his script.

    Hi All,

    I am a noob and need help with this SQL 2005 script that works for every month except January. I know it's computing 1-1=0 (for Jan.) but I don't know how to fix it or restructure my query to work.

    ***********************************************
    select * from table1 where
    datepart(month,lastmodified)=(datepart(month,getda te())-1) and
    datepart(year,lastmodified)=datepart(year,getdate( ))

    lastmodified BETWEEN DateAdd(month, -1 GetDate())
    ************************************************

    Any help and suggestions are welcome!

    Tx, John

  2. #2
    Join Date
    Dec 2008
    Posts
    135
    u want the details from feb to dec in ur result set i think so
    then use
    month(lastmodified) <> 1

  3. #3
    Join Date
    Jun 2003
    Location
    Ohio
    Posts
    12,592
    Provided Answers: 1
    datepart(month,getdate())-1 is going to return zero in January. The datepart portion will return 1, so subtracting 1 from it won't work.
    Try:
    Code:
    select	*
    from	Table
    where	datediff(month, lastmodified, getdate()) = 1
    If it's not practically useful, then it's practically useless.

    blindman
    www.chess.com: "sqlblindman"
    www.LobsterShot.blogspot.com

  4. #4
    Join Date
    Dec 2008
    Posts
    2

    Gracias

    Thanks for the replies....I'll see if they work for me!

    JC

Posting Permissions

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