2013-05-09 269 views
1

我通過使用觸發器得到了從datetime中分割日期,月份年份,日期名稱的問題,插入日期時間時,則在下一列中將拆分日期,時間,月份一年的姓名和數字年(知道我的數據中的男人有多大年紀)是可能的?將日期時間拆分爲日期/年/月/日名稱和計數年份

例如,如果我插入

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

那麼會是這樣

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

爲什麼不使用ISO 8601? 「19820406」 – gbn 2013-05-09 10:17:55

+0

kk我得到了這個,但是謝謝關於歧義的記憶 – oebanez 2013-05-09 10:28:30

+0

問題:爲什麼在使用計算列時使用觸發器?佔用較少的空間,可以編入索引.... – 2013-05-09 10:53:42

回答

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小提琴Demo

+0

這個假設爲美國的mm/dd/yyy格式。我認爲這是6月4日。 – gbn 2013-05-09 10:17:25

+0

@gbn:我已將格式更改爲dmy now – praveen 2013-05-09 10:18:21

+0

雖然它可能是美國的:我們不知道。這就是爲什麼我們有ISO 8601格式來消除這種歧義 – gbn 2013-05-09 10:19:04

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 

感謝 阿倫

+0

如何創建觸發器像這樣分裂? – oebanez 2013-05-09 13:38:36

+0

您需要在插入實際值的列上創建一個after插入觸發器,從插入的表中還可以在下一列中拆分和更新 – 2013-05-14 06:49:30

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 
相關問題