Please explain how get the last day of previous month in SQL Server [on hold]











up vote
-4
down vote

favorite












Please explain how to get the last day of the previous month in SQL Server.



I don't want to use built in function.










share|improve this question









New contributor




Darshan M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as unclear what you're asking by marc_s, Hong Ooi, H. Pauwelyn, EdChum, Rob 2 days ago


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.















  • SELECT DAY(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)))
    – AlleXyS
    2 days ago










  • can you please explain...
    – Darshan M
    2 days ago










  • Downvoted because no attempt
    – SMor
    2 days ago















up vote
-4
down vote

favorite












Please explain how to get the last day of the previous month in SQL Server.



I don't want to use built in function.










share|improve this question









New contributor




Darshan M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











put on hold as unclear what you're asking by marc_s, Hong Ooi, H. Pauwelyn, EdChum, Rob 2 days ago


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.















  • SELECT DAY(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)))
    – AlleXyS
    2 days ago










  • can you please explain...
    – Darshan M
    2 days ago










  • Downvoted because no attempt
    – SMor
    2 days ago













up vote
-4
down vote

favorite









up vote
-4
down vote

favorite











Please explain how to get the last day of the previous month in SQL Server.



I don't want to use built in function.










share|improve this question









New contributor




Darshan M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Please explain how to get the last day of the previous month in SQL Server.



I don't want to use built in function.







sql-server






share|improve this question









New contributor




Darshan M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Darshan M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 2 days ago









marc_s

565k12610921244




565k12610921244






New contributor




Darshan M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 days ago









Darshan M

11




11




New contributor




Darshan M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Darshan M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Darshan M is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




put on hold as unclear what you're asking by marc_s, Hong Ooi, H. Pauwelyn, EdChum, Rob 2 days ago


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.






put on hold as unclear what you're asking by marc_s, Hong Ooi, H. Pauwelyn, EdChum, Rob 2 days ago


Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • SELECT DAY(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)))
    – AlleXyS
    2 days ago










  • can you please explain...
    – Darshan M
    2 days ago










  • Downvoted because no attempt
    – SMor
    2 days ago


















  • SELECT DAY(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)))
    – AlleXyS
    2 days ago










  • can you please explain...
    – Darshan M
    2 days ago










  • Downvoted because no attempt
    – SMor
    2 days ago
















SELECT DAY(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)))
– AlleXyS
2 days ago




SELECT DAY(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)))
– AlleXyS
2 days ago












can you please explain...
– Darshan M
2 days ago




can you please explain...
– Darshan M
2 days ago












Downvoted because no attempt
– SMor
2 days ago




Downvoted because no attempt
– SMor
2 days ago












2 Answers
2






active

oldest

votes

















up vote
0
down vote













Something like this will work in SQL Server 2012 or newer:
(all versions before that are out of mainstream support anyway and should be upgraded)



DECLARE @Today DATE = SYSDATETIME();
DECLARE @Year INT = YEAR(@Today);
DECLARE @Month INT = MONTH(@Today);

DECLARE @FirstOfMonth DATE = DATEFROMPARTS(@Year, @Month, 1);

DECLARE @LastOfPreviousMonth DATE = DATEADD(DAY, -1, @FirstOfMonth);

SELECT @LastOfPreviousMonth;


but of course, this uses a lot of built-in functions - so why exactly don't you want to use any built-in functions?!??!?!






share|improve this answer




























    up vote
    0
    down vote













    I have no idea what you mean by without built in functions; as you have to use them. A couple of methods though:



    DATEADD(DAY,-1,DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()),0))
    EOMONTH(DATEADD(MONTH,-1,GETDATE()))


    You're not going to be able to achieve this weekend without using at least a couple of functions, and without your explanation as to why, I can see no good reason why you should't be using them.






    share|improve this answer




























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      0
      down vote













      Something like this will work in SQL Server 2012 or newer:
      (all versions before that are out of mainstream support anyway and should be upgraded)



      DECLARE @Today DATE = SYSDATETIME();
      DECLARE @Year INT = YEAR(@Today);
      DECLARE @Month INT = MONTH(@Today);

      DECLARE @FirstOfMonth DATE = DATEFROMPARTS(@Year, @Month, 1);

      DECLARE @LastOfPreviousMonth DATE = DATEADD(DAY, -1, @FirstOfMonth);

      SELECT @LastOfPreviousMonth;


      but of course, this uses a lot of built-in functions - so why exactly don't you want to use any built-in functions?!??!?!






      share|improve this answer

























        up vote
        0
        down vote













        Something like this will work in SQL Server 2012 or newer:
        (all versions before that are out of mainstream support anyway and should be upgraded)



        DECLARE @Today DATE = SYSDATETIME();
        DECLARE @Year INT = YEAR(@Today);
        DECLARE @Month INT = MONTH(@Today);

        DECLARE @FirstOfMonth DATE = DATEFROMPARTS(@Year, @Month, 1);

        DECLARE @LastOfPreviousMonth DATE = DATEADD(DAY, -1, @FirstOfMonth);

        SELECT @LastOfPreviousMonth;


        but of course, this uses a lot of built-in functions - so why exactly don't you want to use any built-in functions?!??!?!






        share|improve this answer























          up vote
          0
          down vote










          up vote
          0
          down vote









          Something like this will work in SQL Server 2012 or newer:
          (all versions before that are out of mainstream support anyway and should be upgraded)



          DECLARE @Today DATE = SYSDATETIME();
          DECLARE @Year INT = YEAR(@Today);
          DECLARE @Month INT = MONTH(@Today);

          DECLARE @FirstOfMonth DATE = DATEFROMPARTS(@Year, @Month, 1);

          DECLARE @LastOfPreviousMonth DATE = DATEADD(DAY, -1, @FirstOfMonth);

          SELECT @LastOfPreviousMonth;


          but of course, this uses a lot of built-in functions - so why exactly don't you want to use any built-in functions?!??!?!






          share|improve this answer












          Something like this will work in SQL Server 2012 or newer:
          (all versions before that are out of mainstream support anyway and should be upgraded)



          DECLARE @Today DATE = SYSDATETIME();
          DECLARE @Year INT = YEAR(@Today);
          DECLARE @Month INT = MONTH(@Today);

          DECLARE @FirstOfMonth DATE = DATEFROMPARTS(@Year, @Month, 1);

          DECLARE @LastOfPreviousMonth DATE = DATEADD(DAY, -1, @FirstOfMonth);

          SELECT @LastOfPreviousMonth;


          but of course, this uses a lot of built-in functions - so why exactly don't you want to use any built-in functions?!??!?!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          marc_s

          565k12610921244




          565k12610921244
























              up vote
              0
              down vote













              I have no idea what you mean by without built in functions; as you have to use them. A couple of methods though:



              DATEADD(DAY,-1,DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()),0))
              EOMONTH(DATEADD(MONTH,-1,GETDATE()))


              You're not going to be able to achieve this weekend without using at least a couple of functions, and without your explanation as to why, I can see no good reason why you should't be using them.






              share|improve this answer

























                up vote
                0
                down vote













                I have no idea what you mean by without built in functions; as you have to use them. A couple of methods though:



                DATEADD(DAY,-1,DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()),0))
                EOMONTH(DATEADD(MONTH,-1,GETDATE()))


                You're not going to be able to achieve this weekend without using at least a couple of functions, and without your explanation as to why, I can see no good reason why you should't be using them.






                share|improve this answer























                  up vote
                  0
                  down vote










                  up vote
                  0
                  down vote









                  I have no idea what you mean by without built in functions; as you have to use them. A couple of methods though:



                  DATEADD(DAY,-1,DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()),0))
                  EOMONTH(DATEADD(MONTH,-1,GETDATE()))


                  You're not going to be able to achieve this weekend without using at least a couple of functions, and without your explanation as to why, I can see no good reason why you should't be using them.






                  share|improve this answer












                  I have no idea what you mean by without built in functions; as you have to use them. A couple of methods though:



                  DATEADD(DAY,-1,DATEADD(MONTH,DATEDIFF(MONTH,0,GETDATE()),0))
                  EOMONTH(DATEADD(MONTH,-1,GETDATE()))


                  You're not going to be able to achieve this weekend without using at least a couple of functions, and without your explanation as to why, I can see no good reason why you should't be using them.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 days ago









                  Larnu

                  13.3k31329




                  13.3k31329















                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin