1

我有问题通过使用触发器从日期时间拆分日期、月份年份、日期名称,当我插入日期时间时,然后在下一列中将拆分日期、时间、月份年份日期名称和计数年份(知道我数据中的人多大)那可能吗 ?

例如,如果我插入

INSERT INTO MAN VALUES ('04/06/1982')

然后会是这样

DATETIME                  DATE         MONTH     YEAR      DAYNAME     AGE
04/06/1982 00:00:00       04           06        1982      friday      27
4

3 回答 3

1

尝试这个 :-

Declare @myDate datetime 
set @myDate='19820604'    --YYYYMMDD


Select @myDate as DateTime,
       datename(day,@myDate) as Date,
       month(@myDate) as Month,
       datename(year,@myDate) as Year,
       Datename(weekday,@myDate) as DayName,
       DATEDIFF ( year , @myDate , getdate() ) as Age

结果

  ╔══════════════════════════════╦══════╦═══════╦══════╦═════════╦══════════╗
  ║           DateTime           ║ DATE ║ MONTH ║ YEAR ║ DAYNAME ║ Age      ║
  ╠══════════════════════════════╬══════╬═══════╬══════╬═════════╬══════════╣
  ║ April, 06 1982 00:00:00+0000 ║    4 ║     6 ║ 1982 ║ Friday  ║       31 ║
  ╚══════════════════════════════╩══════╩═══════╩══════╩═════════╩══════════╝

SQL 小提琴演示

于 2013-05-09T10:13:22.210 回答
0

代码已稍作改动以正确给出年龄。

    Select myDate,myDateDate,myDateMonth,myDateYear,myDateDayName, Convert(varchar(50),Age)+ ' Years and '+Convert(varchar(50),nodays) +'days' {Age] from
        (
        Select @myDate as myDate,
               datename(day,@myDate) as myDateDate,
               month(@myDate) as myDateMonth,
               datename(year,@myDate) as myDateYear,
               Datename(weekday,@myDate) as myDateDayName,
               DATEDIFF ( year , @myDate , getdate() ) Age  , 
               DATEDIFF       ( dd , @myDate , getdate() ) -365* DATEDIFF ( year , @myDate , getdate() )   as nodays
        ) As a

谢谢阿伦

于 2013-05-09T11:32:14.883 回答
0

这里有两种解决问题的方法:

- 方法 1:
您可以在表中添加一些计算列,因此当您检索表内容时,将在该时间计算其他剩余字段。

-- 1.1) Create the base of table 'MAN'
CREATE TABLE [dbo].[MAN](
    [DATETIME] [datetime] NOT NULL
) ON [PRIMARY]
GO

-- 1.2) Insert a record in it
INSERT INTO MAN VALUES ('04/06/1982')
GO

-- 1.3) Add some computed columns
ALTER TABLE MAN
ADD
    [DAY]       AS DATENAME(DAY, [DATETIME]),
    [MONTH]     AS MONTH([DATETIME]),
    [YEAR]      AS DATENAME(YEAR, [DATETIME]),
    [DAYNAME]   AS DATENAME(WEEKDAY, [DATETIME]),
    [AGE]       AS DATEDIFF(YEAR, [DATETIME], GETDATE())
GO

-- 1.4) See the result
SELECT * FROM MAN


- 方法 2:
在创建表时,添加所需的剩余字段,以便在下一阶段添加插入/更新触发器以计算剩余字段并插入/更新它们。

-- 2.1) Create the table 'MAN' with all needed columns
CREATE TABLE [dbo].[MAN](
    [DATETIME]  [datetime]      NOT NULL,
    [DAY]       [int]           NULL,
    [MONTH]     [int]           NULL,
    [YEAR]      [int]           NULL,
    [DAYNAME]   [nvarchar](10)  NULL,
    [AGE]       [int]           NULL
) ON [PRIMARY]
GO

-- 2.2) Create Insert and update triggers in order to calculate the values of the rest fields while inserting/updating
CREATE TRIGGER [dbo].[trCalculateRemainingDateFields] ON MAN
AFTER INSERT, UPDATE
AS
BEGIN   
    SET NOCOUNT ON;

    UPDATE  MAN
    SET
        [DAY]       = DATENAME(DAY, MAN.[DATETIME]),
        [MONTH]     = MONTH(MAN.[DATETIME]),
        [YEAR]      = DATENAME(YEAR, MAN.[DATETIME]),
        [DAYNAME]   = DATENAME(WEEKDAY, MAN.[DATETIME]),
        [AGE]       = DATEDIFF(YEAR, MAN.[DATETIME], GETDATE())

    FROM    Inserted i
    WHERE   i.[DATETIME] = MAN.[DATETIME]
END
GO


-- 2.3) Insert a record in it
INSERT INTO MAN ([DATETIME]) VALUES ('04/06/1985')
GO

-- 2.4) See the result
SELECT * FROM MAN


- 方法 3:
创建计算剩余字段的主表视图可能是一个不错的选择。

-- 3.1) Create the base of table 'MAN'
CREATE TABLE [dbo].[MAN](
    [DATETIME] [datetime] NOT NULL
) ON [PRIMARY]
GO

-- 3.2) Insert a record in it
INSERT INTO MAN VALUES ('04/06/1982')
GO

-- 3.3) Create a view which is contained the remaining fields
CREATE VIEW dbo.vMAN
AS
    SELECT  [DATETIME],
            DATENAME(DAY, [DATETIME])               AS [DAY],
            MONTH([DATETIME])                       AS [MONTH],
            DATENAME(YEAR, [DATETIME])              AS [YEAR],
            DATENAME(WEEKDAY, [DATETIME])           AS [DAYNAME],
            DATEDIFF(YEAR, [DATETIME], GETDATE())   AS [AGE]    
    FROM    MAN
GO

-- 3.4) See the result
SELECT * FROM vMAN
于 2017-03-26T21:02:03.720 回答