Help on Date Time Validation

  • Hello

    Can someone please share if there are any functions or code to validate the Date in different format settings?

    Ex:

    ----

    If I enter the value "9999", the function should validate against the Date Formats ("MM/DD/YYYY", "DD-MMM-YYYY") and return 0, because 9999 is not a valid Date

    If I enter the value "01-Jan-9999", the function should validate against the Date Formats ("MM/DD/YYYY", "DD-MMM-YYYY") and return 1.

    Also is there any common function available to use for similar kind of different requirements?

    Thanks

    Shuaib

  • ShuaibV (7/10/2013)


    Hello

    Can someone please share if there are any functions or code to validate the Date in different format settings?

    Ex:

    ----

    If I enter the value "9999", the function should validate against the Date Formats ("MM/DD/YYYY", "DD-MMM-YYYY") and return 0, because 9999 is not a valid Date

    If I enter the value "01-Jan-9999", the function should validate against the Date Formats ("MM/DD/YYYY", "DD-MMM-YYYY") and return 1.

    Also is there any common function available to use for similar kind of different requirements?

    Thanks

    Shuaib

    I think you should leave the validation to the front end. If you store your data as a datetime datatype this is not an issue.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • We have the Data stored in NVarchar data type. Also, the data should exported to file based on the Data validation (Like Bad Data, Valid Data)....

  • ShuaibV (7/10/2013)


    We have the Data stored in NVarchar data type.

    UGH!!! Any chance of changing this? Storing "dates" as nvarchar is a really bad practice. It makes for very challenging validation and performance killing calculations. And why Nvarchar? Not many unicode characters are required to make a valid date. 😉

    Also, the data should exported to file based on the Data validation (Like Bad Data, Valid Data)....

    Any other requirements?

    In order to help we will need a few things:

    1. Sample DDL in the form of CREATE TABLE statements

    2. Sample data in the form of INSERT INTO statements

    3. Expected results based on the sample data

    Please take a few minutes and read the first article in my signature for best practices when posting questions.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • ShuaibV (7/10/2013)


    Hello

    Can someone please share if there are any functions or code to validate the Date in different format settings?

    Ex:

    ----

    If I enter the value "9999", the function should validate against the Date Formats ("MM/DD/YYYY", "DD-MMM-YYYY") and return 0, because 9999 is not a valid Date

    If I enter the value "01-Jan-9999", the function should validate against the Date Formats ("MM/DD/YYYY", "DD-MMM-YYYY") and return 1.

    Also is there any common function available to use for similar kind of different requirements?

    Thanks

    Shuaib

    Here's a procedure I use with example usage. More reliable than ISDATE and allows different DATEFORMAT settings. The first procedure calls the second, which could also be used standalone if only checking UMC dates.

    CREATE PROCEDURE [dbo].[IsValidDate]

    @sDate VARCHAR(50)

    ,@sDateFormat CHAR(3) = 'YMD' -- MDY, DMY, YMD, YDM, MYD, DYM

    AS

    BEGIN

    /*

    EXAMPLES:

    EXEC dbo.IsValidDate '01-07-2001' -- Valid date

    EXEC dbo.IsValidDate '1/7/2001' -- Valid date

    EXEC dbo.IsValidDate '07-01-2001' -- Valid date

    EXEC dbo.IsValidDate '7/1/2001' -- Valid date

    EXEC dbo.IsValidDate '29-12-2013','DMY' -- Valid date in DMY format

    EXEC dbo.IsValidDate '29-12-2013','MDY' -- Invalid date in MDY format

    EXEC dbo.IsValidDate '32-12-2013' -- ERROR: Date out of range

    EXEC dbo.IsValidDate '29-02-2013' -- ERROR: Not a leap year

    EXEC dbo.IsValidDate 'xyz' -- ERROR: Invalid date

    EXEC dbo.IsValidDate '1234' -- Invalid std date/valid umc date

    EXEC dbo.IsValidDate '2013-01-08 15:44:12' -- Valid date

    EXEC dbo.IsValidDate '2013-01-08 15:44:12.000' -- Valid date

    EXEC dbo.IsValidDate '2013-01-08 15:44:12.208' -- Valid date

    EXEC dbo.IsValidDate '2013-01-08 15:44:12.2081606' -- Invalid std date/valid umc date

    EXEC dbo.IsValidDate '2013-01-08 15:44:12.2081606 +05:30' -- Invalid std date/valid umc date

    */

    SET NOCOUNT ON

    SET DATEFORMAT @sDateFormat

    DECLARE

    @dStdDate SMALLDATETIME

    ,@dUMCDate DATETIMEOFFSET

    ,@bIsValidUMCDate BIT

    ,@bIsValidDate BIT

    SET @dStdDate = NULL

    SET @dUMCDate = NULL

    /* Check to see if this is a valid UMC date */

    IF OBJECT_ID('tempdb..#CheckUMCDate') IS NOT NULL

    DROP TABLE #CheckUMCDate

    CREATE TABLE #CheckUMCDate (

    [ID] INT IDENTITY(1,1) NOT NULL,

    [InputDate] VARCHAR(50) NULL,

    [ConvertedDate] VARCHAR(50) NULL,

    [IsValidUMCDate] BIT NULL

    PRIMARY KEY (ID))

    BEGIN TRY

    INSERT INTO #CheckUMCDate

    EXEC dbo.IsValidUMCDate @sDate

    END TRY

    BEGIN CATCH

    INSERT INTO #CheckUMCDate

    SELECT

    @sDate AS InputDate

    ,NULL ConvertedDate

    ,0 AS IsValidUMCDate

    END CATCH

    SELECT

    @dUMCDate = ConvertedDate

    ,@bIsValidUMCDate = IsValidUMCDate

    FROM

    #CheckUMCDate

    /* Check date by converting into other date datatypes. */

    /* The date datatypes to use can (should) be changed */

    /* depending on your requirements. Different date */

    /* datatypes will give different results! */

    BEGIN TRY

    SET @dStdDate = CONVERT(SMALLDATETIME,@sDate)

    SET @bIsValidDate = 1

    END TRY

    BEGIN CATCH

    SET @bIsValidDate = 0

    SET @dStdDate = NULL

    END CATCH

    SELECT

    @sDate AS InputDate

    ,@dStdDate AS StdDate

    ,@bIsValidDate AS IsValidDate

    ,@dUMCDate AS UMCDate

    ,@bIsValidUMCDate AS IsValidUMCDate

    END

    GO

    CREATE PROCEDURE [dbo].[IsValidUMCDate]

    @sDate VARCHAR(50)

    ,@sDateFormat CHAR(3) = 'YMD' -- MDY, DMY, YMD, YDM, MYD, DYM

    AS

    BEGIN

    SET NOCOUNT ON

    SET DATEFORMAT @sDateFormat

    DECLARE

    @dUMCDate DATETIMEOFFSET

    ,@bIsValidUMCDate BIT

    ,@ERROR BIT

    SET @dUMCDate = NULL

    BEGIN TRY

    SET @dUMCDate = CONVERT(DATETIMEOFFSET,@sDate)

    SET @bIsValidUMCDate = 1

    END TRY

    BEGIN CATCH

    SET @bIsValidUMCDate = 0

    SET @dUMCDate = NULL

    END CATCH

    SET @ERROR = @bIsValidUMCDate

    SELECT

    @sDate AS InputDate

    ,@dUMCDate AS ConvertedDate

    ,@ERROR AS IsValidUMCDate

    END

    GO

     

  • Thank you. I will post the sample data at the earliest. We using NVARCHAR is to store the generic data and not specific to Date/Int etc.,

    Thanks

  • Thank you Steven. This will be really helpful for me, i want to convert this to SQL FUNCTION and there is restriction we cant SET DATEFORMAT in the function. I have the view and wanted to use against a column in the view.

    Thanks

  • ShuaibV (7/10/2013)


    Thank you Steven. This will be really helpful for me, i want to convert this to SQL FUNCTION and there is restriction we cant SET DATEFORMAT in the function. I have the view and wanted to use against a column in the view.

    Thanks

    I've tried to make a function out of this (preferably an inline tvf), but you can neither issue SET commands in a function nor use TRY/CATCH. If the DATEFORMAT is not an issue then that could be left out, but I haven't been able to figure out a way to do the necessary error-trapping within a function. Many methods for doing so have been suggested but I just haven't been able to get them to work in this case.

    This is simply a "trial-and-error" validation where the CONVERT is attempted and if it converts it's good and if not it fails, but there needs to be some mechanism for detecting and reporting a failure. If you or anyone else can turn this into an actual function I would be pleased. 🙂

     

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply