我有报告,它将YEAR
作为一个参数,我想计算财政年度的开始和结束。以下是我的尝试方式:
CREATE PROCEDURE [dbo].[sp_name]
@StartDate as datetime,
@Enddate as datetime,
@year as varchar(10)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
@StartDate = year(dateadd(q, -1, cast(cast(@year AS char) + '/01/' + cast(@year AS char) AS datetime))) = @year
这是正确的方法吗?
我需要财务开始日期为2014年7月1日至2015年6月30日,如果年份输入2015年。请注意,我需要在内部以脚本计算。如果我做错了什么,我怎么能纠正这个以获得理想的结果?
答案 0 :(得分:4)
使用DATEADD
和DATEDIFF
,您可以为您的会计年度计算机:
DECLARE @year INT = 2015
SELECT
start_date = DATEADD(MONTH, 6, DATEADD(YEAR, DATEDIFF(YEAR, 0, DATEADD(YEAR, @year - 1900, 0)) - 1, 0)),
end_date = DATEADD(DAY, -1, DATEADD(MONTH, 6, DATEADD(YEAR, DATEDIFF(YEAR, 0, DATEADD(YEAR, @year - 1900, 0)), 0)))
阅读here了解更常见的日期例程。
要在存储过程中使用它:
CREATE PROCEDURE procedure_name
@year AS INT
AS
BEGIN
SET NOCOUNT ON
SELECT
start_date = DATEADD(MONTH, 6, DATEADD(YEAR, DATEDIFF(YEAR, 0, DATEADD(YEAR, @year - 1900, 0)) - 1, 0)),
end_date = DATEADD(DAY, -1, DATEADD(MONTH, 6, DATEADD(YEAR, DATEDIFF(YEAR, 0, DATEADD(YEAR, @year - 1900, 0)), 0)))
END
答案 1 :(得分:0)
对于SQL Server 2012+版本,您可以使用DATEFROMPARTS
https://msdn.microsoft.com/en-IN/library/hh213228.aspx
CREATE PROCEDURE [dbo].[usp_name]
@StartDate as datetime,
@Enddate as datetime,
@year as int
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT @StartDate = DATEFROMPARTS(@year-1,7,1), @EndDate=DATEFROMPARTS(@year,6,30)
END
答案 2 :(得分:0)
也许这会有所帮助,也可以在财政年度发生变化或搬到新公司时起作用。
CREATE PROCEDURE [dbo].[usp_yeardates] /* or your sp name */
@year AS SMALLINT,
@monthoffset AS TINYINT = 0, /* if you wish your year to start at a month other than jan then set number of months to offset, e.g. to start April, move three forward @monthoffset = 3 */
@startdate AS SMALLDATETIME OUTPUT, /* NB 2008r2+ use DATE instead of SMALLDATETIME */
@enddate AS SMALLDATETIME OUTPUT
AS
/* Created by Darren Edward Comeau - 26/08/2015 */
BEGIN
/* check inputs */
IF @year < 1900 or @year > 2078
RAISERROR ('year out of bounds',16,1)
ELSE IF @monthoffset > 11
RAISERROR ('monthoffset out of bounds',16,2)
ELSE
SELECT
/* logic to establish start / end date */
@startdate =
dateadd(month,@monthoffset,
dateadd(year,@year-1900,'19000101')
),
@enddate =
dateadd(day,-1,
dateadd(month,@monthoffset,
dateadd(year,@year-1899,'19000101')
)
);
END;
GO
您将使用以下程序;
/* usage */
DECLARE
@startdate SMALLDATETIME,
@enddate SMALLDATETIME,
@year SMALLINT,
@monthoffset TINYINT,
@rc INT;
EXEC @rc = usp_yeardates
@year = 2011,
@monthoffset = 6, /* 6 months offset equalls July - June year */
@startdate = @startdate OUTPUT,
@enddate = @enddate OUTPUT;
SELECT
@rc AS [ReturnCode],
@startdate AS [StartDate],
@enddate AS [EndDate];
答案 3 :(得分:0)
这将为您提供财政年度的开始和结束日期
DECLARE @DateFrom datetime, @DateTo datetime
SET @DateFrom = '2012-03-30'
SET @DateTo = '2021-03-31'
DECLARE @Output TABLE ( Item NVARCHAR(1000),startdate Datetime,enddate Datetime )
DECLARE @Year INT
DECLARE @EndYear INT
DECLARE @Month INT
DECLARE @FinacialYearValue INT
SET @Month = CAST(MONTH(@DateFrom) AS INT)
SET @Year = CAST(YEAR(@DateFrom) AS INT)
SET @EndYear= CAST(YEAR(@DateTo) AS INT)
SET @FinacialYearValue = (CASE WHEN @Month <=3 THEN @Year - 1 ELSE @Year END)
WHILE @EndYear >= @FinacialYearValue
BEGIN
INSERT INTO @Output (Item,startdate,enddate )
SELECT CAST(@FinacialYearValue AS varchar(4)) + '-' + CAST((@FinacialYearValue +1 ) AS varchar(4)) ,
start_date = DATEADD(MONTH, 3, DATEADD(YEAR, DATEDIFF(YEAR, 0, DATEADD(YEAR, @FinacialYearValue+1 - 1900, 0)) - 1, 0)),
end_date = DATEADD(DAY, -1, DATEADD(MONTH, 3, DATEADD(YEAR, DATEDIFF(YEAR, 0, DATEADD(YEAR, @FinacialYearValue+1 - 1900, 0)), 0)))
SET @FinacialYearValue += 1
END
SELECT * FROM @Output
答案 4 :(得分:0)
Declare @BillDate smalldatetime, @FYStartDate smalldatetime, @FYEndDate smalldatetime
Select @BillDate ='10-JAN-2019'
Select @FYStartDate = case when MONTH(@BillDate) <=3 Then '01-Apr-' + CAST(YEAR(@BillDate) - 1 AS varchar(4)) Else '01-Apr-' + CAST(YEAR(@BillDate) AS varchar(4)) End,
@FYEndDate = case when MONTH(@BillDate) <=3 Then '31-Mar-' + CAST(YEAR(@BillDate) AS varchar(4)) Else '31-Mar-' + CAST(YEAR(@BillDate) + 1 AS varchar(4)) End
答案 5 :(得分:0)
import React, { Component } from "react";
import Form from "./form";
import joi from "joi-browser";
import {registeruser} from "../http/registeruser";
class Register extends Form {
state = {
data:{
name:"", password:"", email:""},
errors:{}
}
schema={
name: joi.string().required().label("name"),
password: joi.string().required().min(5).label("Password"),
email: joi.string().required().label("Email"),
}
doSubmit = async () =>{
await registeruser(this.state.data);
}
render() {
return ( <div>
<h1>Register</h1>
<form onSubmit={this.handleSubmit}>
{this.renderInput("name","name","string")}
{this.renderInput("password","Password","password")}
{this.renderInput("email","Email","string")}
{this.renderButton("Register")}
</form>
</div>
);
}
}
export default Register;
答案 6 :(得分:0)
这将提供印度财政年度的开始日期。例如四月至三月。
SELECT CAST(DATEFROMPARTS(YEAR(DATEADD(M, (MONTH(DATEADD(MONTH, -4, GETDATE()) - 1) * -1), GETDATE())), MONTH('04-01-2020'), DAY('04-01-2020')) AS date)
答案 7 :(得分:0)
Declare @Date date = Getdate() ---Paste your date here
Declare @Fyear varchar(4)
Declare @FyearStartDate Date
Declare @FyearEnd varchar(4)
Declare @FyearEndDate Date
If Month(@Date) >= 4
Set @Fyear = year(@Date)
Else
Set @Fyear = year(@Date)-1
Set @FyearStartDate = '04' +'-'+ '01-' + @Fyear
Set @FyearEnd = @Fyear+1
Set @FyearEndDate = '03' +'-'+ '31-' + @FyearEnd
Select @FyearStartDate FYSTARTDT, @FyearEndDate FYENDDT