开发者

Restrict specific date format Data into Table

I Have a table called Sur_Data and the data looks like:

ID     SV_Date
258    13/01/2010
569    15/02/2011
695    26/05/2010
745    12/06/2010

Now I want to select the ID's from that table and insert into anot开发者_运维百科her table so we are using something like:

Insert into Surdate(ID)
Select ID from Sur_Data
where ISDATE(SV_Date) = 1

Since the format in SV_Date is different it is not inserting any records into Surdate table.

So I am trying to see is there a way that we could restrict the data in Sur_Data table to have only date's that are in MM/DD/YYYY format.So whenever they try to insert records of different format it should throw an error.

Can anyone help on this?


Edit: for example 2 & 3, ANSI WARNINGS must be off.

IS_DATE function is influenced by DATEFORMAT setting for current SQL Server session/connection.

Example 1:

DECLARE @d1 VARCHAR(25) = '26/05/2010'
        ,@d2 VARCHAR(25) = '15/02/2011';

PRINT '*****Test 1*****'        
SET DATEFORMAT DMY;
SELECT  ISDATE(@d1), ISDATE(@d2);       

PRINT '*****Test 2*****'        
SET DATEFORMAT MDY;
SELECT  ISDATE(@d1), ISDATE(@d2);

Results:

*****Test 1*****

----------- -----------
1           1

(1 row(s) affected)

*****Test 2*****

----------- -----------
0           0

(1 row(s) affected)

Now, you can see how DATEFORMAT influences ISDATE function. Instead of ISDATE function you can use CONVERT function with different date/time styles. If a [n][var]char value doesn't have the selected style then CONVERT function will return NULL. For dd/mm/yyyy values (british) can be used style 103 and for mm/dd/yyyy values (U.S.) can be used style 101.

Example 2:

SET ANSI_WARNINGS OFF;
SET ARITHABORT OFF;

DECLARE @Results TABLE
(
    ID INT PRIMARY KEY
    ,SV_Date VARCHAR(20) NOT NULL
);

INSERT  @Results 
VALUES
 (258,    '13/01/2010')
,(569,    '15/02/2011')
,(695,    '26/05/2010')
,(745,    '12/06/2010');

SELECT  *
        ,ISDATE(r.SV_Date) [IS_DATETIME]
        ,CONVERT(DATETIME,r.SV_Date,103) [IS_DATETIME British/French style=dd/mm/yyyy]
        ,CONVERT(DATETIME,r.SV_Date,101) [IS_DATETIME U.S. style=mm/dd/yyyy]
        ,CASE 
            WHEN CONVERT(DATETIME,r.SV_Date,103) IS NOT NULL AND CONVERT(DATETIME,r.SV_Date,101) IS NULL THEN 'IS_DMY'
            WHEN CONVERT(DATETIME,r.SV_Date,103) IS NULL AND CONVERT(DATETIME,r.SV_Date,101) IS NOT NULL THEN 'IS_MDY'
            WHEN CONVERT(DATETIME,r.SV_Date,103) IS NOT NULL AND CONVERT(DATETIME,r.SV_Date,101) IS NOT NULL THEN 'IS_DMY_OR_MDY'
            WHEN CONVERT(DATETIME,r.SV_Date,103) IS NULL AND CONVERT(DATETIME,r.SV_Date,101) IS NULL THEN 'IS_NOT_DMY_OR_MDY'
        END
FROM    @Results r;

Results:

ID          SV_Date              IS_DATETIME IS_DATETIME British/French style=dd/mm/yyyy IS_DATETIME U.S. style=mm/dd/yyyy 
----------- -------------------- ----------- ------------------------------------------- --------------------------------- -----------------
258         13/01/2010           0           2010-01-13 00:00:00.000                     NULL                              IS_DMY
569         15/02/2011           0           2011-02-15 00:00:00.000                     NULL                              IS_DMY
695         26/05/2010           0           2010-05-26 00:00:00.000                     NULL                              IS_DMY
745         12/06/2010           1           2010-06-12 00:00:00.000                     2010-12-06 00:00:00.000           IS_DMY_OR_MDY

Now, if you want to check SV_Date values for mm/dd/yyyy format (style 101 - U.S.) then you can use a CHECK constraint like this:

Example 3:

DECLARE @Results2 TABLE
(
    ID INT PRIMARY KEY
    ,SV_Date VARCHAR(20) NOT NULL
    ,CHECK( CONVERT(DATETIME,SV_Date,101) IS NOT NULL )
);
SET ANSI_WARNINGS OFF;
INSERT  @Results2 
VALUES  (258,    '13/01/2010');
INSERT  @Results2 
VALUES  (569,    '15/02/2011');
INSERT  @Results2 
VALUES  (695,    '26/05/2010');
INSERT  @Results2 
VALUES  (745,    '12/06/2010');
SELECT  *
FROM    @Results2;

Results:

ID          SV_Date
----------- --------------------
745         12/06/2010

(1 row(s) affected)

Observations: If you want to find current DATEFORMAT setting (current session) then you can use sys.dm_exec_sessions view:

SELECT  s.date_format, s.date_first
FROM    sys.dm_exec_sessions s
WHERE   s.session_id = @@SPID 


To strictly answer the question, you could create a function (CLR or TSQL) and apply that as a column constraint/check.

But as @joe Stefanelli correctly points out, store it as a datetime data type and let the client handle the presentation format.

Edit

http://msdn.microsoft.com/en-us/library/ms190273.aspx

ALTER TABLE
    dbo.Sur_Data 
WITH CHECK 
ADD CONSTRAINT ck_dateFormatted CHECK (dbo.VerifyDateFormat(SV_Date) = 1) ;

Which assumes you've defined a function that returns 1 if the format matches the expectation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜