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?
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.
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.
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.
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.
What about SqlDateTime.MinValue and SqlDateTime.MaxValue?
Note: these are SQL type min/max not the .net types like the previous 2 answers :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With