Need to check if today's date < 20 then set result var as 1st of next month.
If today's date > 20 then set result var as 1st of next month following the next month ?
Example:
for 24-09-2015 set result to 1st Nov 2015
for 19-09-2015 set result to 1st Oct 2015
This seems a bit tricky because of the date arithmetic:
set #var = (case when day(getdate()) < 20
then cast(dateadd(month, 1, dateadd(day, 1 - day(getdate()), getdate())) as date)
else cast(dateadd(month, 2, dateadd(day, 1 - day(getdate()), getdate())) as date)
end);
Here is a SQL Fiddle that shows the calculation.
This will give you the result:
DECLARE #d DATE = '20150924'
SELECT CASE WHEN DAY(#d) < 20
THEN DATEADD(mm, 1, DATEADD(dd, -DAY(#d) + 1, #d))
ELSE DATEADD(mm, 2, DATEADD(dd, -DAY(#d) + 1, #d)) END;
Related
First Day of financial year is April 1st.
T-SQL Query to return April 1st for the getdate()
Financial Year: April 1st to March 31st
Try this:
select DATEFROMPARTS(Yr, 4, 1) [start], DATEFROMPARTS(Yr + 1, 3, 31) [end] from
(select case when DATEPART(month, getdate()) < 4 then DATEPART(year, getdate()) - 1 else DATEPART(year, getdate()) end Yr) a
declare #today date = '2018-06-21'
select fin_year = dateadd(month, 3,
dateadd(year,
datepart(year,
dateadd(month, -3, #today)) - 1900, 0))
the expression datepart(year, dateadd(month, -3, #today)) is to get the current financial year. Since your financial year is Apr 1, for Jan 1 to Mar 31, subtracting 3 months from it, will give you the correct year (fiscal year = financial year).
After that it is just to form the date Apr 1 with that year
DECLARE #DateToUse DATETIME = GETDATE(),
#FinancialYearStart DATETIME
DECLARE #DayPart INT = DATEPART(DAY, #DateToUse),
#MonthPart INT = DATEPART(MONTH, #DateToUse),
#YearPart INT = DATEPART(YEAR, #DateToUse),
#StartMonth INT = 4, -- April
#StartDay INT = 1 -- 1st
SELECT DATETIMEFROMPARTS((
#YearPart - CASE
WHEN #MonthPart > #StartMonth
OR (
#MonthPart = #StartMonth
AND #DayPart >= #StartDay
)
THEN 0
ELSE 1
END
), #StartMonth, #StartDay, 0, 0, 0, 0)
I just knocked this up and it produces 2020-04-01 00:00:00.000 right now. I threw some other dates at it and it seemed to work nicely. Obviously the month/day can easily be changed by changing the variables at the top.
It's not the shortest block of SQL, but it's easy to use for different dates. A lot of other answers I've seen across different questions have returned different formats like a string or just the year part. Whereas this datetime will allow it to be used for datetime comparisons.
I am using SQL Server 2014 and I need to add a line of code to my SQL query that will filter the data extracted only to those records where the StayDate (a column in database) is greater than or equal to the 1st day of the current month.
In other words, the line of code I need is the following:
WHERE StayDate >= '1st Day of Current Month'
Note: StayDate is in the datetime format (eg: 2015-12-18 00:00:00.000)
Use EOMONTH to get the first day of current month
WHERE StayDate >= Dateadd(dd, 1, Eomonth(Getdate(), -1))
SQL Server 2012 and above
WHERE StayDate >= DATEADD(DAY, 1, EOMONTH(GETDATE(), -1))
Before SQL Server 2012
WHERE StayDate >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
You can try following approach:
declare #t datetime = '2016-02-28'
select DATEADD(D, -DATEPART(d, #t) + 1, #t)
So in your case:
WHERE StayDate >= DATEADD(D, -DATEPART(d, GETDATE()) + 1, GETDATE())
StayDate >= DATEADD(MONTH, DATEDIFF(MONTH, '19000101', GETDATE()), '19000101')
One other option; since staydate being on or after (greater than or equal to) the first day of the current month is equivalent to searching for staydates during the current month:
where datepart(mm, staydate) = datepart(mm, getdate())
and datepart(yyyy, staydate) = datepart(yyyy, getdate())
I want to run a query on a specific date range for my company's fiscal year, which ends in October.
The query needs to run from October 1 of the last year through the current date, but if the current year gets past October, then it needs to switch over to October of this year.
So: currently it would be October 1, 2013 to July 16, 2014.
Starting on October 2, 2014 it will be October 2, 2014 to October 2, 2014.
I could simply go in and change the SQL every October, but I figured there was an easier way to do this with a logic statement.
Thanks!
Do you want something like this:
SELECT
<your_column_list>
FROM TABLE
WHERE
date_col >= CASE
WHEN MONTH(GETDATE()) < 10 THEN CAST(('1.10.' + CAST(YEAR(GETDATE()) - 1 as CHAR(4)) AS DATETIME)
ELSE GETDATE()
END
AND
date_col < CASE
WHEN MONTH(GETDATE()) < 10 THEN GETDATE()
ELSE
CAST(('1.10.' + CAST(YEAR(GETDATE()) + 1 as CHAR(4)) AS DATETIME)
END
I'm not sure about the exact dates, i don't get the october 2nd part. You will have to adjust the date format as well. I used dd.mm.yyyy on your system it might be different.
So this should be giving the desired results.
DECLARE #startDate DATETIME,
#endDate DATETIME,
#today DATETIME;
BEGIN
SET #today = CONVERT(DATETIME, '03/1/14');
SELECT #startDate = CASE
WHEN MONTH(#today) < 10 THEN '10/01/' + LTRIM(STR(YEAR(DATEADD(YEAR, -1, #today))))
WHEN MONTH(#today) = 10
AND MONTH(#today) = 1 THEN '10/01/' + LTRIM(STR(YEAR(DATEADD(YEAR, -1, #today))))
WHEN MONTH(#today) = 10
AND MONTH(#today) > 1 THEN '10/01/' + LTRIM(STR(YEAR(#today)))
WHEN MONTH(#today) > 10 THEN '10/01/' + LTRIM(STR(YEAR(#today)))
ELSE '1/1/2000'
END,
#endDate = GETDATE();
SELECT #startDate, #endDate;
END;
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Get dates from a week number in T-SQL
How do I get the date value if I have a week number in SQL Query.
Like if I pass 26, it should give me 06/24/2012. If I pass 27, I should get 07/01/2012
Any help will be appreciated :)
Sots
SELECT DATEADD(week, n, '11/25/2011');
with n being the week number
If this doesn't work, try using WEEK() instead of WEEKOFYEAR().
CURDATE() - INTERVAL WEEKDAY(CURDATE()) DAY + INTERVAL (WEEKNO - WEEKOFYEAR(CURDATE())) WEEK
In SQL Server
DECLARE #StartDate DATE, #WeekVal INT
SET #WeekVal = 26 -- Set the week number
SET #StartDate = DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0) -- Start of current year
;WITH cte AS (
SELECT #StartDate AS DateVal, DATEPART(wk, #StartDate) AS WeekVal, 1 AS RowVal
UNION ALL
SELECT DATEADD(d, 1, DateVal), DATEPART(wk, DATEADD(d, 1, DateVal)), RowVal + 1
FROM cte WHERE RowVal < 365
)
SELECT MIN(DateVal) StartOfWeek
FROM cte
WHERE WeekVal = #WeekVal
OPTION (MAXRECURSION 365);
This gives you the start and end dates of the week. [For SQL Server]
Declare #week integer set #week = 26
Declare #Year Integer Set #Year = year(getdate())
declare #date datetime
-- ------------------------------------
Set #date = DateAdd(day, 0,
DateAdd(month, 0,
DateAdd(Year, #Year-1900, 0)))
set #date = Dateadd(week, #week-1, #date)
select #date startweek, DATEADD (D, -1 * DatePart (DW, #date) + 7, #date) endweek
This was the result from it:
startweek endweek
----------------------- -----------------------
2012-07-01 00:00:00.000 2012-07-07 00:00:00.000
(1 row(s) affected)
Need help in writing the query to get the last month data as well as month to date data.
If today's date is Mar 23 2011, I need to retrieve the data from last month and the data till todays date(means Mar 23 2011).
If date is Apr 3 2011, data should consists of March month data and the data till Apr 3rd 2011.
Thanks,
Shahsra
Today including time info : getdate()
Today without time info : DATEADD(DAY, DATEDIFF(day, 0, getdate()), 0)
Tomorrow without time info : DATEADD(DAY, DATEDIFF(day, 0, getdate()), 1)
Beginning of current month : DATEADD(month, datediff(month, 0, getdate()), 0)
Beginning of last month : DATEADD(month, datediff(month, 0, getdate())-1, 0)
so most likely
WHERE dateColumn >= DATEADD(month, datediff(month, 0, getdate())-1, 0)
AND dateColumn < DATEADD(DAY, DATEDIFF(day, 0, getdate()), 1)
Step back one month, subtract the number of days to the current date, and add one day.
WHERE
DateField <= GetDate() AND
DateField >= DateAdd(
mm,
-1,
DateAdd(dd, -1*DatePart(dd, GetDate())+1, GetDate())
)
To remove the time quickly, you can use this
Cast( Floor( Cast( GETDATE() AS FLOAT ) ) AS DATETIME )
So the second part would be (without time)
DateField >= Cast( Floor( Cast( (DateAdd(
mm,
-1,
DateAdd(dd, -1*DatePart(dd, GetDate())+1, GetDate())
)) AS FLOAT ) ) AS DATETIME )
Select Column1, Column2 From Table1
Where DateColumn <= GetDate() AND
DateColumn >= DATEADD(dd, - (DAY(DATEADD(mm, 1, GetDate())) - 1), DATEADD(mm, - 1, GetDate()))
Edit: +1 to Russel Steen. I was posting mine before I knew he had posted.
Very helpful page
declare #d datetime = '2011-04-03';
declare #startDate datetime;
select #startDate =
CAST('01 '+ RIGHT(CONVERT(CHAR(11),DATEADD(MONTH,-1,#d),113),8) AS datetime);
select #startDate;