SQL Serverver--Create fiscal year table (constraint)

Source: Internet
Author: User

--Example 1: Create financial year table (constraint)

--New table:

Create Table Fiscalyears
(
Fiscal_Year INTEGER not NULL PRIMARY KEY,
Start_date date not NULL,
Constraint valid_start_date
CHECK (
Year (start_date) = Fiscal_year-1
and MONTH (start_date) =10
And Day (start_date) =01
),

End_date date not NULL,
Constraint valid_end_date
CHECK (
Year (end_date) = Fiscal_Year
and MONTH (end_date) =10
And Day (end_date) =01
)
);

--Delete constraint valid_end_date

Alter Table Fiscalyears
Drop constraint Valid_end_date

--New constraint
Alter Table Fiscalyears
Add Constraint Valid_end_date
CHECK (
Year (end_date) = Fiscal_Year
and MONTH (end_date) =09
And Day (end_date) =30
)

-OR:

ALTER TABLE [dbo]. [Fiscalyears]
With CHECK
ADD CONSTRAINT [Valid_end_date]
CHECK (
(
DATEPART (Year,[end_date]) =[fiscal_year]
and DATEPART (month,[end_date]) = (9)
and DATEPART (day,[end_date]) = (30)
)
)
GO

--Insert data into table Fiscalyears
INSERT INTO dbo. Fiscalyears
Values
' 1990 ',
' 1989-10-01 ',
' 1990-09-30 '
)

--Reference: http://www.xuebuyuan.com/53328.html;

--http://m.blog.csdn.net/blog/zhaoyh0530/4535987


This article comes from the "Ricky's blog" blog, please be sure to keep this source http://57388.blog.51cto.com/47388/1699518

SQL Serverver--Create fiscal year table (constraint)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.