SQL help - Need to know the first date of previous month and last date of previous month...

  • mw112009 - Wednesday, April 19, 2017 6:50 AM

    Jeff Moden - Tuesday, April 18, 2017 8:31 PM

    Agreed.  With that in mind, I have the answer that DesNorton posted below.

    ... and with that, I'll ask mw112009 , do you have any questions on how or why it works?

    I already answered this .......in a previous reply ...( as stated below ). Thank You, Thank You.

    Credit to DesNorton
    Jeff: The dates are used as default values in a SSRS report. The preferred format is YYYYMMDD. So what Des provided helps!

    'NO', in other words.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • Phil Parkin - Wednesday, April 19, 2017 6:55 AM

    mw112009 - Wednesday, April 19, 2017 6:50 AM

    Jeff Moden - Tuesday, April 18, 2017 8:31 PM

    Agreed.  With that in mind, I have the answer that DesNorton posted below.

    ... and with that, I'll ask mw112009 , do you have any questions on how or why it works?

    I already answered this .......in a previous reply ...( as stated below ). Thank You, Thank You.

    Credit to DesNorton
    Jeff: The dates are used as default values in a SSRS report. The preferred format is YYYYMMDD. So what Des provided helps!

    'NO', in other words.

    yes Sir! No further help at this moment.

  • mw112009 - Wednesday, April 19, 2017 7:10 AM

    Phil Parkin - Wednesday, April 19, 2017 6:55 AM

    mw112009 - Wednesday, April 19, 2017 6:50 AM

    Jeff Moden - Tuesday, April 18, 2017 8:31 PM

    Agreed.  With that in mind, I have the answer that DesNorton posted below.

    ... and with that, I'll ask mw112009 , do you have any questions on how or why it works?

    I already answered this .......in a previous reply ...( as stated below ). Thank You, Thank You.

    Credit to DesNorton
    Jeff: The dates are used as default values in a SSRS report. The preferred format is YYYYMMDD. So what Des provided helps!

    'NO', in other words.

    yes Sir! No further help at this moment.

    So you understand exactly what the "0", "-1", and "112" values are for?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden - Wednesday, April 19, 2017 7:40 AM

    mw112009 - Wednesday, April 19, 2017 7:10 AM

    Phil Parkin - Wednesday, April 19, 2017 6:55 AM

    mw112009 - Wednesday, April 19, 2017 6:50 AM

    Jeff Moden - Tuesday, April 18, 2017 8:31 PM

    Agreed.  With that in mind, I have the answer that DesNorton posted below.

    ... and with that, I'll ask mw112009 , do you have any questions on how or why it works?

    I already answered this .......in a previous reply ...( as stated below ). Thank You, Thank You.

    Credit to DesNorton
    Jeff: The dates are used as default values in a SSRS report. The preferred format is YYYYMMDD. So what Des provided helps!

    'NO', in other words.

    yes Sir! No further help at this moment.

    So you understand exactly what the "0", "-1", and "112" values are for?

    Of course he does, it gives him the answer he needs.  That is all that matters apparently.  I had asked him the same question and never saw an answer.

  • Lynn Pettis - Wednesday, April 19, 2017 8:07 AM

    Jeff Moden - Wednesday, April 19, 2017 7:40 AM

    mw112009 - Wednesday, April 19, 2017 7:10 AM

    Phil Parkin - Wednesday, April 19, 2017 6:55 AM

    mw112009 - Wednesday, April 19, 2017 6:50 AM

    Jeff Moden - Tuesday, April 18, 2017 8:31 PM

    Agreed.  With that in mind, I have the answer that DesNorton posted below.

    ... and with that, I'll ask mw112009 , do you have any questions on how or why it works?

    I already answered this .......in a previous reply ...( as stated below ). Thank You, Thank You.

    Credit to DesNorton
    Jeff: The dates are used as default values in a SSRS report. The preferred format is YYYYMMDD. So what Des provided helps!

    'NO', in other words.

    yes Sir! No further help at this moment.

    So you understand exactly what the "0", "-1", and "112" values are for?

    Of course he does, it gives him the answer he needs.  That is all that matters apparently.  I had asked him the same question and never saw an answer.

    112 is a formatting constant that gives the YYYYMMDD format ( That was easy ) , the 0 and -1 is used in the DateDiff and DateAdd function to calculate. Cool! Thanks!

  • mw112009 - Wednesday, April 19, 2017 8:09 AM

    Lynn Pettis - Wednesday, April 19, 2017 8:07 AM

    Jeff Moden - Wednesday, April 19, 2017 7:40 AM

    mw112009 - Wednesday, April 19, 2017 7:10 AM

    Phil Parkin - Wednesday, April 19, 2017 6:55 AM

    mw112009 - Wednesday, April 19, 2017 6:50 AM

    Jeff Moden - Tuesday, April 18, 2017 8:31 PM

    Agreed.  With that in mind, I have the answer that DesNorton posted below.

    ... and with that, I'll ask mw112009 , do you have any questions on how or why it works?

    I already answered this .......in a previous reply ...( as stated below ). Thank You, Thank You.

    Credit to DesNorton
    Jeff: The dates are used as default values in a SSRS report. The preferred format is YYYYMMDD. So what Des provided helps!

    'NO', in other words.

    yes Sir! No further help at this moment.

    So you understand exactly what the "0", "-1", and "112" values are for?

    Of course he does, it gives him the answer he needs.  That is all that matters apparently.  I had asked him the same question and never saw an answer.

    112 is a formatting constant that gives the YYYYMMDD format ( That was easy ) , the 0 and -1 is used in the DateDiff and DateAdd function to calculate. Cool! Thanks!

    First part, the 112, but there is more to the 0 and -1.  Can you explain what they are doing, why it gives you the answer to your question.  Based on what you just wrote, I am not sure you really do.

  • Lynn Pettis - Wednesday, April 19, 2017 9:13 AM

    mw112009 - Wednesday, April 19, 2017 8:09 AM

    Lynn Pettis - Wednesday, April 19, 2017 8:07 AM

    Jeff Moden - Wednesday, April 19, 2017 7:40 AM

    mw112009 - Wednesday, April 19, 2017 7:10 AM

    Phil Parkin - Wednesday, April 19, 2017 6:55 AM

    mw112009 - Wednesday, April 19, 2017 6:50 AM

    Jeff Moden - Tuesday, April 18, 2017 8:31 PM

    Agreed.  With that in mind, I have the answer that DesNorton posted below.

    ... and with that, I'll ask mw112009 , do you have any questions on how or why it works?

    I already answered this .......in a previous reply ...( as stated below ). Thank You, Thank You.

    Credit to DesNorton
    Jeff: The dates are used as default values in a SSRS report. The preferred format is YYYYMMDD. So what Des provided helps!

    'NO', in other words.

    yes Sir! No further help at this moment.

    So you understand exactly what the "0", "-1", and "112" values are for?

    Of course he does, it gives him the answer he needs.  That is all that matters apparently.  I had asked him the same question and never saw an answer.

    112 is a formatting constant that gives the YYYYMMDD format ( That was easy ) , the 0 and -1 is used in the DateDiff and DateAdd function to calculate. Cool! Thanks!

    First part, the 112, but there is more to the 0 and -1.  Can you explain what they are doing, why it gives you the answer to your question.  Based on what you just wrote, I am not sure you really do.

    Adding to that, mw112009 do you understand what DATEDIFF and DATEADD do?

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • bmg002 - Wednesday, April 19, 2017 9:34 AM

    Lynn Pettis - Wednesday, April 19, 2017 9:13 AM

    mw112009 - Wednesday, April 19, 2017 8:09 AM

    Lynn Pettis - Wednesday, April 19, 2017 8:07 AM

    Jeff Moden - Wednesday, April 19, 2017 7:40 AM

    mw112009 - Wednesday, April 19, 2017 7:10 AM

    Phil Parkin - Wednesday, April 19, 2017 6:55 AM

    mw112009 - Wednesday, April 19, 2017 6:50 AM

    Jeff Moden - Tuesday, April 18, 2017 8:31 PM

    Agreed.  With that in mind, I have the answer that DesNorton posted below.

    ... and with that, I'll ask mw112009 , do you have any questions on how or why it works?

    I already answered this .......in a previous reply ...( as stated below ). Thank You, Thank You.

    Credit to DesNorton
    Jeff: The dates are used as default values in a SSRS report. The preferred format is YYYYMMDD. So what Des provided helps!

    'NO', in other words.

    yes Sir! No further help at this moment.

    So you understand exactly what the "0", "-1", and "112" values are for?

    Of course he does, it gives him the answer he needs.  That is all that matters apparently.  I had asked him the same question and never saw an answer.

    112 is a formatting constant that gives the YYYYMMDD format ( That was easy ) , the 0 and -1 is used in the DateDiff and DateAdd function to calculate. Cool! Thanks!

    First part, the 112, but there is more to the 0 and -1.  Can you explain what they are doing, why it gives you the answer to your question.  Based on what you just wrote, I am not sure you really do.

    Adding to that, mw112009 do you understand what DATEDIFF and DATEADD do?

    Thank you good teachers for checking my knowledge...
    bmg002- 🙂  Your question was too easy .. ha ha

    Here you go Lynn: This is a quick reply I am sure you we dont need the details here.
    Instructions for FirstOfPrevMonth
    1.) Get # of Months from 1900/01/01 to currentDate ( This is an integer ) using DATEDIFF(MM, 0, GETDATE())
    2.) Subtract 1 ( THAT WILL TAKE US TO THE PREVIOUS MONTH )
    3.) DATEADD(MM, [], 0) gives the first date of the month ( In this case MARCH 1, you added months from 1900/01/01 to March 2017 )
    4.) [] = DATEDIFF(MM, 0, GETDATE())
    5.) Finally the CONVERT( VARCHAR(8), XXX, 112 ) converts it to the YYYYMMDD format

    LastOfPrevMonth
    1.) Select DATEADD(MM, DATEDIFF(MM, 0, GETDATE()), 0) - Gives the first date of the current month
    2.) -1 takes you to the last date of the previous month
    3.) Finally the CONVERT( VARCHAR(8), XXX, 112 ) converts it to the YYYYMMDD format

  • mw112009 - Wednesday, April 19, 2017 10:04 AM

    bmg002 - Wednesday, April 19, 2017 9:34 AM

    Lynn Pettis - Wednesday, April 19, 2017 9:13 AM

    mw112009 - Wednesday, April 19, 2017 8:09 AM

    Lynn Pettis - Wednesday, April 19, 2017 8:07 AM

    Jeff Moden - Wednesday, April 19, 2017 7:40 AM

    mw112009 - Wednesday, April 19, 2017 7:10 AM

    Phil Parkin - Wednesday, April 19, 2017 6:55 AM

    mw112009 - Wednesday, April 19, 2017 6:50 AM

    Jeff Moden - Tuesday, April 18, 2017 8:31 PM

    Agreed.  With that in mind, I have the answer that DesNorton posted below.

    ... and with that, I'll ask mw112009 , do you have any questions on how or why it works?

    I already answered this .......in a previous reply ...( as stated below ). Thank You, Thank You.

    Credit to DesNorton
    Jeff: The dates are used as default values in a SSRS report. The preferred format is YYYYMMDD. So what Des provided helps!

    'NO', in other words.

    yes Sir! No further help at this moment.

    So you understand exactly what the "0", "-1", and "112" values are for?

    Of course he does, it gives him the answer he needs.  That is all that matters apparently.  I had asked him the same question and never saw an answer.

    112 is a formatting constant that gives the YYYYMMDD format ( That was easy ) , the 0 and -1 is used in the DateDiff and DateAdd function to calculate. Cool! Thanks!

    First part, the 112, but there is more to the 0 and -1.  Can you explain what they are doing, why it gives you the answer to your question.  Based on what you just wrote, I am not sure you really do.

    Adding to that, mw112009 do you understand what DATEDIFF and DATEADD do?

    Thank you good teachers for checking my knowledge...
    bmg002- 🙂  Your question was too easy .. ha ha

    Here you go Lynn: This is a quick reply I am sure you we dont need the details here.
    Instructions for FirstOfPrevMonth
    1.) Get # of Months from 1900/01/01 to currentDate ( This is an integer ) using DATEDIFF(MM, 0, GETDATE())
    2.) Subtract 1 ( THAT WILL TAKE US TO THE PREVIOUS MONTH )
    3.) DATEADD(MM, [], 0) gives the first date of the month ( In this case MARCH 1, you added months from 1900/01/01 to March 2017 )
    4.) [] = DATEDIFF(MM, 0, GETDATE())
    5.) Finally the CONVERT( VARCHAR(8), XXX, 112 ) converts it to the YYYYMMDD format

    LastOfPrevMonth
    1.) Select DATEADD(MM, DATEDIFF(MM, 0, GETDATE()), 0) - Gives the first date of the current month
    2.) -1 takes you to the last date of the previous month
    3.) Finally the CONVERT( VARCHAR(8), XXX, 112 ) converts it to the YYYYMMDD format

    Did I pass the test then ?

  • mw112009 - Wednesday, April 19, 2017 10:06 AM

    mw112009 - Wednesday, April 19, 2017 10:04 AM

    bmg002 - Wednesday, April 19, 2017 9:34 AM

    Lynn Pettis - Wednesday, April 19, 2017 9:13 AM

    mw112009 - Wednesday, April 19, 2017 8:09 AM

    Lynn Pettis - Wednesday, April 19, 2017 8:07 AM

    Jeff Moden - Wednesday, April 19, 2017 7:40 AM

    mw112009 - Wednesday, April 19, 2017 7:10 AM

    Phil Parkin - Wednesday, April 19, 2017 6:55 AM

    mw112009 - Wednesday, April 19, 2017 6:50 AM

    Jeff Moden - Tuesday, April 18, 2017 8:31 PM

    Agreed.  With that in mind, I have the answer that DesNorton posted below.

    ... and with that, I'll ask mw112009 , do you have any questions on how or why it works?

    I already answered this .......in a previous reply ...( as stated below ). Thank You, Thank You.

    Credit to DesNorton
    Jeff: The dates are used as default values in a SSRS report. The preferred format is YYYYMMDD. So what Des provided helps!

    'NO', in other words.

    yes Sir! No further help at this moment.

    So you understand exactly what the "0", "-1", and "112" values are for?

    Of course he does, it gives him the answer he needs.  That is all that matters apparently.  I had asked him the same question and never saw an answer.

    112 is a formatting constant that gives the YYYYMMDD format ( That was easy ) , the 0 and -1 is used in the DateDiff and DateAdd function to calculate. Cool! Thanks!

    First part, the 112, but there is more to the 0 and -1.  Can you explain what they are doing, why it gives you the answer to your question.  Based on what you just wrote, I am not sure you really do.

    Adding to that, mw112009 do you understand what DATEDIFF and DATEADD do?

    Thank you good teachers for checking my knowledge...
    bmg002- 🙂  Your question was too easy .. ha ha

    Here you go Lynn: This is a quick reply I am sure you we dont need the details here.
    Instructions for FirstOfPrevMonth
    1.) Get # of Months from 1900/01/01 to currentDate ( This is an integer ) using DATEDIFF(MM, 0, GETDATE())
    2.) Subtract 1 ( THAT WILL TAKE US TO THE PREVIOUS MONTH )
    3.) DATEADD(MM, [], 0) gives the first date of the month ( In this case MARCH 1, you added months from 1900/01/01 to March 2017 )
    4.) [] = DATEDIFF(MM, 0, GETDATE())
    5.) Finally the CONVERT( VARCHAR(8), XXX, 112 ) converts it to the YYYYMMDD format

    LastOfPrevMonth
    1.) Select DATEADD(MM, DATEDIFF(MM, 0, GETDATE()), 0) - Gives the first date of the current month
    2.) -1 takes you to the last date of the previous month
    3.) Finally the CONVERT( VARCHAR(8), XXX, 112 ) converts it to the YYYYMMDD format

    Did I pass the test then ?

    Looks correct to me.
    And I wasn't trying to sound condesending or anything, just wanted to be sure you did understand everything that everyone was saying.  Getting the correct answer is important, but understanding the correct answer is a LOT more important.  Otherwise you cannot support it in the future.

    The above is all just my opinion on what you should do. 
    As with all advice you find on a random internet forum - you shouldn't blindly follow it.  Always test on a test server to see if there is negative side effects before making changes to live!
    I recommend you NEVER run "random code" you found online on any system you care about UNLESS you understand and can verify the code OR you don't care if the code trashes your system.

  • mw112009 - Wednesday, April 19, 2017 10:06 AM

    Did I pass the test then ?

    "It Depends" on the next time you need to do such a thing. 😉

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Thom A - Tuesday, April 18, 2017 9:38 AM

    Have you ever heard the proverb "give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime"?

    Teach a man to build a fire and he'll be warm for a night. Set a man on fire and he'll be warm for the rest of his life.

    (sorry, having a cynical day today :Whistling:)

  • dmbaker - Thursday, April 20, 2017 7:54 AM

    Thom A - Tuesday, April 18, 2017 9:38 AM

    Have you ever heard the proverb "give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime"?

    Teach a man to build a fire and he'll be warm for a night. Set a man on fire and he'll be warm for the rest of his life.

    (sorry, having a cynical day today :Whistling:)

    I prefer this version:
    Give a man a fish and you feed him for a day; teach a man to fish and he'll drink beer for the day.

    The absence of evidence is not evidence of absence
    - Martin Rees
    The absence of consumable DDL, sample data and desired results is, however, evidence of the absence of my response
    - Phil Parkin

  • mw112009 - Tuesday, April 18, 2017 9:19 AM

    To All ( Bran,Luis,Jeff,Phil and anyone who comments ... ) 
    You guys are a great help. Sorry I am not able to pay you.

    Hey,  Helping a needy person is a act of Good Will.  You may not get paid by me but you will receive a bigger return ( Count on that ).
    Why do I say this ? I have complete trust in the Bible.
    Also one more point... When helping others do it with love ( Never complain ...only then will you receive a return for your favor )
    That is how real "Faith" works  ( Galatians 5:6 ,  The only thing that counts is faith expressing itself through love. ) .

    Ha Ha... I can see a few of you may be getting pizzed off by my comments... Hey,no hard feelings...but I have 100% faith in what I wrote above.. if it works for me it works for you. It is a UNIVERSAL LAW.. guaranteed to work.. )

    Using religious beliefs as an excuse to abuse the privilege of belonging to a forum is as corrupt as the practices you would consider sins, so please don't expect any further assistance from me.  You won't get any.   Abuse is abuse.   Period, end of sentence, end of paragraph, end of assistance.   You really should be ashamed of your lazy self.   And didn't your Bible also state "God helps those who help themselves" ?

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • sgmunson - Thursday, April 20, 2017 11:37 AM

     And didn't your Bible also state "God helps those who help themselves" ?

    Err...No.
    https://en.wikipedia.org/wiki/God_helps_those_who_help_themselves

    Luis C.
    General Disclaimer:
    Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

    How to post data/code on a forum to get the best help: Option 1 / Option 2

Viewing 15 posts - 31 through 44 (of 44 total)

You must be logged in to reply to this topic. Login to reply