Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validating a date to be within the SqlDbType.DateTime range

I want to validate a System.DateTime value before I add it as a parameter to my SqlCommand instance.

The MSDN documentation for the SqlDbType enumeration says:

Date and time data ranging in value from January 1, 1753 to December 31, 9999 to an accuracy of 3.33 milliseconds.

To validate the value, I'm using

public readonly DateTime SqlDateTimeMin = new DateTime(1753, 1, 1);
public readonly DateTime SqlDateTimeMax = new DateTime(9999, 12, 31);

if (value < SqlDateTimeMin || value > SqlDateTimeMax)
    // Validation check fails
else
    // Validation check succeeds

Is this the best way? Is there an alternative to hard coding these min and max values?

like image 815
Ed I Avatar asked Nov 02 '10 18:11

Ed I


People also ask

How to create a date range in data validation using and function?

Using AND Function to Create a Date Range in Data Validation. The same thing can also be done by using a custom formula based on AND function. First of all, select cell A1. From data validation dialog box, select “Custom” from “Allow” drop down. Now, in the formula input bar enter below formula and click OK.

How to validate data with two dates in Excel?

From here in data validation dialog box, select “Date” from “Allow” drop down. After that, select between from the data drop down. Next, you need to enter two dates in “Start Date” and “End Date” input boxes.

Where are the date and time stored in a MySQL database?

The date and time are collectively stored in a column using the datatype DATETIME2. SELECT * FROM TABLE_NAME WHERE DATE_TIME_COLUMN BETWEEN 'STARTING_DATE_TIME' AND 'ENDING_DATE_TIME'; Step 1: Create a Database.

How to use data validation in Excel 2016?

Now, go to Data Tab ⇢ Data Validation ⇢ Data Validation. From here in data validation dialog box, select “Date” from “Allow” drop down. After that, select between from the data drop down. Next, you need to enter two dates in “Start Date” and “End Date” input boxes.


1 Answers

What about SqlDateTime.MinValue and SqlDateTime.MaxValue?

Note: these are SQL type min/max not the .net types like the previous 2 answers :-)

like image 182
gbn Avatar answered Oct 19 '22 06:10

gbn