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.
sql-server
New contributor
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.
add a comment |
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.
sql-server
New contributor
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
add a comment |
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.
sql-server
New contributor
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
sql-server
New contributor
New contributor
edited 2 days ago
marc_s
565k12610921244
565k12610921244
New contributor
asked 2 days ago
Darshan M
11
11
New contributor
New contributor
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
add a comment |
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
add a comment |
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?!??!?!
add a comment |
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.
add a comment |
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?!??!?!
add a comment |
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?!??!?!
add a comment |
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?!??!?!
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?!??!?!
answered 2 days ago
marc_s
565k12610921244
565k12610921244
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered 2 days ago
Larnu
13.3k31329
13.3k31329
add a comment |
add a comment |
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