Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Help on Date Time Validation Expand / Collapse
Author
Message
Posted Wednesday, July 10, 2013 1:39 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, April 11, 2014 5:57 PM
Points: 104, Visits: 314
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
Post #1472327
Posted Wednesday, July 10, 2013 1:54 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:31 PM
Points: 11,949, Visits: 10,982
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1472336
Posted Wednesday, July 10, 2013 2:03 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, April 11, 2014 5:57 PM
Points: 104, Visits: 314
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)....
Post #1472341
Posted Wednesday, July 10, 2013 2:11 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 3:31 PM
Points: 11,949, Visits: 10,982
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1472344
Posted Wednesday, July 10, 2013 3:28 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
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


 
Post #1472379
Posted Wednesday, July 10, 2013 4:04 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, April 11, 2014 5:57 PM
Points: 104, Visits: 314
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
Post #1472387
Posted Wednesday, July 10, 2013 4:07 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Friday, April 11, 2014 5:57 PM
Points: 104, Visits: 314
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
Post #1472389
Posted Wednesday, July 10, 2013 7:32 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
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.

 
Post #1472409
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse