Get current school year from the current date in SQL Server - sql-server

So there is a very similar forum post to this one but I cannot run those commands. I am using SQL Server 2008 R2 and need help getting the current school year.
Basically I need a way to generate the current school year by using the current date. From Aug. of the current year until Aug. of the next year it will be 1 school year.
Ex. Aug. 2015 - July 2016 will need to pull up the year 2015 and Starting Aug. 2016 the year 2016 will need to pull up.
I am still fairly new to SQL so any help would be greatly appreciated.

Sounds like you're looking for this:
IF Month(GetDate()) >=8
BEGIN
SELECT Year(GetDate())
END
ELSE
BEGIN
SELECT Year(GetDate())-1
END
EDIT:
To use this in a select statement's where clause:
DECLARE #currentSchoolYear INT
IF Month(GetDate()) >=8
BEGIN
SELECT #currentSchoolYear = Year(GetDate())
END
ELSE
BEGIN
SELECT #currentSchoolYear = Year(GetDate())-1
END
SELECT * FROM dbo.dates
WHERE CASE WHEN Month(datevalue) >=8 THEN Year(datevalue)
ELSE Year(datevalue)-1
END = #currentSchoolYear
obviously replace "dbo.dates" with your table name, and "datevalue" with whatevercolumn you're comparing. This will return the rows from the current school year

SELECT value1, value,
(CASE WHEN GETDATE() BETWEEN '20150801' AND '20160731' THEN 2015
WHEN GETDATE() BETWEEN '20160801' AND '20170731' THEN 2016
ELSE 2014) AS schoolYear
FROM dbTable
WHERE ...

Related

sql code to get week number for FiscalYear

Need sql code to get week number for FiscalYear –
Date table ranges from 1975 to 2024
Already have weekno (WeekOfYear) for CalendarYear
-week count start from 1st of April and end on 31st march
-Week start from Sunday to Saturday
-On Fiscalyear end, week also ended
eg.
1st apr 2016 -Week 1
2nd apr 2016 - week 1
3rd apr 2016 to 9th apr 2016 -week 2
10th apr 2016 to 16th apr 2016 -week 3 ... and so on
assuming DATEFIRST is 7, I got this function - actually you can default datefirst = 7 for the lifetime of your query only - this seems to work according to by 1980 calendar
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date, ,>
-- Description: <Description, ,>
-- =============================================
CREATE FUNCTION FiscalYearMonth
(
#DAT datetime
)
RETURNS int
AS
BEGIN
DECLARE #TAXY as int;
DECLARE #R as int;
SET #TAXY = YEAR(dateadd(month,-3,#dat));
DECLARE #TDAT as varchar(8)
SET #TDAT = CAST(#TAXY AS varchar(4)) + '0401';
SELECT #R = datediff(day, DATEADD(day , (8 - DATEPART(DW,#TDAT)) % 7,#TDAT), dateadd(day,14,#DAT)) / 7;
RETURN #R;
END
GO
e.g.
SET datefirst 7;
select dbo.FiscalYearMonth('19800413')
Writing a query to do this, seems to me very complicated. I advise you to do it either by using a table (Choice1) or by creating an EXCEL file (Choice2).
Choice1: Create a WeekCalendarForFiscalYear table with 4 fields [DateOfToday, weekNumber, FiscalYear].
In DateOfToday, you put the date of the day.
In WeekNumber, you put the number of the fiscal week.
In FiscalYear, you are in the fiscal year (2016, 2017, 2018, etc.).
You have to think about indexing these fields.
/!\ : You can also create your table like this: WeekCalendarForFiscalYear with 4 fields [WeekNumber, FiscalYear, StartDate, EndDate]. StartDate would correspond to the beginning of the fiscal week and EndDate at the end of the fiscal week.
Choix2: You create an EXCEL file in which you enter the data either as in Choix1, or as it suits you.
The file must be physically on the server of the database.
You access the file via OPENROWSET. You will find on the web a wide
range of sites that will help you to use OPENROWSET well.
Hope this can help.

How to get 3 letter abbreviation for month in SQL

How to get month in 3 letters in SQL.
In SQL Table data is inserted:
2016-01-07 09:38:58.310
I need only month result in 3 letters like below:
Jan
Assuming you're using SQL Server 2012 or newer, you can use the FORMAT function:
SELECT FORMAT([Date], 'MMM', 'en-US')
Adapt the locale as needed.
Since you're on SQL Server 2008, I'd use
SELECT LEFT(DATENAME(MONTH, [Date]), 3)
Try this (I am assuming you are using Sql Server).
Select Convert(char(3), GetDate(), 0)
If you need full name of month, try
Select Datename(month, GetDate())
Or you could just do:
LEFT(GETDATE(), 3)
For instance you could declare a variable:
Declare #MONTH VARCHAR(3)
Set #MONTH = LEFT(GETDATE(), 3)

need compare the days between sql and System.datetime

in sql server 2012 i cant not give data type for days like sunday monday , and my project need compare the days between sql and System which contains days like sunday, monday... , please help me how can i proceed ahead?
Try the following query:
SELECT CASE
WHEN CAST(dateX AS DATE) != CAST(dateY AS DATE)
then 'NOT EQUEL'
else 'EQUEL'

DATEPART(ww,Date) with SET DATEFIRST

I´m using SQL Server 2005
I´m trying to get the week with DatePart(ww,date) function
My code
SELECT datepart(ww,'2012-01-08 00:00:00')
Return 2
But I want ...
Return 1
According with IS0-8601 and this table from this website
YEAR 2012
Week-01 From 2012-1-2 to 2012-1-8
...
Am I wrong?
There is any trick with SET DATEFIRST 1, I´m trying without success.
Thanks for your time
I can´t use ISO_WEEK, because SQL Server 2005 don´t work
Use ISO_WEEK:
SELECT datepart(ISO_WEEK,'2012-01-08 00:00:00')
You can read more about it on MSDN.
Edit:
I didn't realize ISO_WEEK was not available in SQL Server 2005. Since it's based on the Thursday of the week, the problem now shifts to finding the Thursday from the given day:
DECLARE #Date date = '2012-01-08'
DECLARE #Thursday date = DATEADD(DAY, 3-(DATEPART(WEEKDAY, #Date) + ##DATEFIRST - 2) % 7, #Date)
SELECT (DATEPART(DAYOFYEAR,#Thursday) - 1) / 7 +1

SQL Server date calculations in stored procedures

I wanted to ask 3 questions about below code (please excuse the long code listing, I am including these lines in the hopes that it provides enough context).
Note that the code here depends on the date when it is executed. For this reason my questions refer to a hypothetical situation with two different execution dates:
March 1st 2014
January 1st 2014
My questions are on whether my understanding on some parts of this is correct, i.e:
A. that the SELECT DATEADD expression (on line 1) would:
On March 1st 2014 create the datetime 2014-02-31 23:59:59
B. that the code on lines 6-9 would:
On March 1st 2014 create the datetime 2014-02-01 00:00:00
On January 1st 2014 create the datetime 2013-02-01 00:00:00
and
C. that the code on lines 11-14 would:
On March 1st 2014 create the datetime 2015-01-30 00:00:00
On January 1st 2014 create the datetime 2014-01-30 00:00:00
Is this understanding correct?
1. SET #ldpmth = (SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,GETDATE()),0)) )
2. SET #yr = (SELECT YEAR(#ldpmth))
3. SET #mth = 0
4. SET #dy = 0
5.
6. SET #fysdate = (SELECT CASE WHEN MONTH(#ldpmth) >= 2 THEN
7. DATEADD(MM, 1,CAST(CAST(#yr+#mth+#dy AS NVARCHAR(50)) AS DATETIME))
8. ELSE DATEADD(MM, 1, CAST(CAST((#yr-1)+#mth+#dy AS NVARCHAR(50))
9. AS DATETIME)) END )
10.
11. SET #fyedate = (SELECT CASE WHEN MONTH(#ldpmth) >= 2 THEN
12. DATEADD(YY, 1, CAST(CAST(#yr+#mth+#dy AS NVARCHAR(50)) AS DATETIME)) + 30
13. ELSE DATEADD(YY, 1, CAST(CAST((#yr-1)+#mth+#dy AS NVARCHAR(50))
14. AS DATETIME)) + 30 END )
(Thank you for all who answered so far. This is actually code that was developed (but not documented) at my place of employment some years ago and I have been tasked with converting it to a form that works client-side with Crystal Reports.)
On March 1st 2014 create the datetime 2014-02-31 23:59:59
No. AFAIK, there isn't any dbms that's so dumb it thinks Feb 31 is an actual date. The SQL statement on line 1 will return the value 'Feb 28 2014 23:59:59'.
You can test all your statements by substituting actual dates for GETDATE() and guesswork. In the first query, for example, use
SELECT DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,'2014-03-01'),0));
^^^^^^^^^^
You can probably run a version of SQL Server locally. SQL Server 2008 even installs on Windows XP. If you can't bear that, though, there's always sqlfiddle.com.
This could be easily tested in SQL Server Management Studio (SSMS). I'll answer the first, and you can test the next two yourself.
Question:
A. that the DATEADD (on line 1) would on March 1st 2014 create the datetime 2014-02-31 23:59:59
Answer: No, because February can't possibly have 31 days, so it can't return '2014-02-31`
Proof:
select DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,'2014-03-01'),0))
Results
(No column name)
2014-02-28 23:59:59.000
For (B) and (C), if you'd stated your question, instead, as "My financial years run from 1st February until 30th(sic) January - how do I get the start and end dates for the current financial year, I'd have offered:
SET #fysdate = DATEADD(year,DATEDIFF(month,'19000201',GETDATE())/12,'19000201')
and:
SET #fyedate = DATEADD(year,DATEDIFF(month,'19000201',GETDATE())/12,'19010131')
(Note that this second finds the 31st Jan, not the 30th. I assume that was carelessness in your original question).
But, as to (A), as I indicated, I thought your question is again poorly phrased - if you're trying to find the end point of a period and that period is defined on datetimes (as opposed to just dates), it's far easier to construct an exclusive upper bound:
SET #ldpmth = DATEADD(month, DATEDIFF(month,0,GETDATE()),0)
This bound (to be used with a < comparison rather than <= or BETWEEN) doesn't artificially exclude any values with a time such as 23:59:59.527. Note also, that if the financial year end date calculation is going to be used against datetime values which include times, I'd again counsel computing an exclusive upper bound, and substitute 19010201 instead of 19010131.
So, in total, I don't think I've actually answered your questions as posed, but I think I've provided the answers you should have been seeking.

Resources