简体   繁体   English

t-sql将日期分配到学年

[英]t-sql assign dates to academic year

I am trying to assign absence dates to an academic year, the academic year being 1st August to the 31st July. 我正在尝试将缺席日期分配到学年,学年是8月1日到7月31日。

So what I would want would be: 所以我想要的是:
31/07/2007 = 2006/2007 31/07/2007 = 2006/2007
02/10/2007 = 2007/2008 02/10/2007 = 2007/2008
08/01/2008 = 2007/2008 08/01/2008 = 2007/2008

Is there an easy way to do this in sql 2000 server. 有没有一种简单的方法在sql 2000服务器中执行此操作。

A variant with less string handling 具有较少字符串处理的变体

SELECT
  AbsenceDate,
  CASE WHEN MONTH(AbsenceDate) <= 7 
    THEN 
      CONVERT(VARCHAR(4), YEAR(AbsenceDate) - 1) + '/' + 
      CONVERT(VARCHAR(4), YEAR(AbsenceDate))
    ELSE 
      CONVERT(VARCHAR(4), YEAR(AbsenceDate)) + '/' + 
      CONVERT(VARCHAR(4), YEAR(AbsenceDate) + 1)
  END AcademicYear
FROM
  AbsenceTable

Result: 结果:

2007-07-31 => '2006/2007' 
2007-10-02 => '2007/2008' 
2008-01-08 => '2007/2008'

Should work this way: 应该这样工作:

select case 
    when month(AbsenceDate) <= 7 then 
        ltrim(str(year(AbsenceDate) - 1)) + '/' 
               + ltrim(str(year(AbsenceDate)))
        else 
        ltrim(str(year(AbsenceDate))) + '/' 
               + ltrim(str(year(AbsenceDate) + 1))
        end

Example: 例:

set dateformat ymd
declare @AbsenceDate datetime
set @AbsenceDate = '2008-03-01'
select case 
    when month(@AbsenceDate) <= 7 then 
        ltrim(str(year(@AbsenceDate) - 1)) + '/' 
               + ltrim(str(year(@AbsenceDate)))
        else 
        ltrim(str(year(@AbsenceDate))) + '/' 
               + ltrim(str(year(@AbsenceDate) + 1))
        end

You could create a function as follows: 您可以创建一个函数,如下所示:

CREATE FUNCTION dbo.GetAcademicYear(@in DATETIME)
RETURNS VARCHAR(10) AS
BEGIN
    DECLARE @out VARCHAR(10)

    IF (MONTH(@in) > 7)
        SET @out = CAST(YEAR(@in) AS VARCHAR) + '/' + CAST((YEAR(@in) + 1) AS VARCHAR)
    ELSE
        SET @out = CAST((YEAR(@in) - 1) AS VARCHAR) + '/' + CAST(YEAR(@in) AS VARCHAR)

    RETURN(@out)
END

And then just call it with: 然后只需调用它:

SELECT dbo.GetAcademicYear('31 July 2007')

or 要么

SELECT col1, col2, dbo.GetAcademicYear(date_col) AS academic_year, col3, etc
FROM my_table

etc 等等

Maybe you could consider creating an AcademicYear table, something like this: 也许您可以考虑创建一个AcademicYear表,如下所示:

CREATE TABLE AcademicYear (
    AcYear   VARCHAR(9)
,   FromDate DATE,
,   ToDate   DATE)

and populating it accordingly. 并相应地填充它。 Then your query might become 然后您的查询可能会成为

SELECT
  AbsenceDate,
  AcYear
FROM
  AbsenceTable JOIN AcademicYear ON AbsenceDate BETWEEN FromDate AND ToDate

As a fringe benefit, if the start and end of the year should change, it's a data change, not some horrible extended UDF. 作为边缘效益,如果一年的开始和结束都应该改变,那就是数据变化,而不是一些可怕的扩展UDF。

I would suggest creating a calendar table. 我建议创建一个日历表。 It would include all dates for the foreseeable future (and past) with any information for those dates that you need - such as the academic year to which it belongs. 它将包括可预见的未来(和过去)的所有日期,以及您需要的那些日期的任何信息 - 例如它所属的学年。 You can then also add in things like whether or not it's a holiday, a weekend, etc. 然后,您还可以添加诸如假期,周末等内容。

Whenever you need any kind of query like this it's then a simple matter of joining to your calendar table. 无论何时您需要这样的任何类型的查询,这都是加入您的日历表的简单问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM