How do you add a CONSTRAINT for a VARCHAR(10) column to checks the Date format as yyyy/mm/dd

I have tried:
ALTER TABLE dbo.tablename
ADD CONSTRAINT DF_Date_format
check (SDate LIKE '%[1-2][0-9][0-9][0-9]/[0-1][0-9]/[0-3][0-9]%')
******But this does not work for VARCHAR data type ********
and
I also tried creating a function as shown below but I got an error when trying to add a constraint with this function.
(error is:
'The ALTER TABLE statement conflicted with the CHECK constraint "DF_Date_format". The conflict occurred in database " databasename ", table "dbo.tablename", column 'Date'.)
Create FUNCTION [dbo].[CheckDateFormat]
(@Date varchar(10))
Returns BIT
AS
BEGIN
Declare @RETURN BIT
SELECT @Return =
Case when (substring (@Date, 5, 1) + substring (@Date, 8, 1)) = '//'
then 0
else 1
end
Return @Return
END
ALTER TABLE dbo.tablename
ADD CONSTRAINT DF_Date_format
check
( dbo.CheckDateFormat(SDate) = 0 )

As noted above, be mindful of the performance implications of UDF CHECK constraints:
http://www.sqlusa.com/bestpractices/udf-check-constraint/
The following logic works:
Create FUNCTION [dbo].[CheckDateFormat]
(@Date varchar(10))
Returns BIT
AS
BEGIN
Declare @RETURN BIT
SELECT @Return =
Case when (substring (@Date, 5, 1) + substring (@Date, 8, 1)) = '//'
then 0
else 1
end
Return @Return
END
GO
CREATE TABLE Orders (ID INT IDENTITY(1,1),
OrderDate varchar(10),
Amount money
GO
ALTER TABLE Orders
ADD CONSTRAINT DF_Date_format
check
( dbo.CheckDateFormat(OrderDate) = 0 )
GO
As noted above, DATE is a better choice for column data type.
Kalman Toth Database & OLAP Architect
SELECT Video Tutorials 4 Hours
New Book / Kindle: Exam 70-461 Bootcamp: Querying Microsoft SQL Server 2012

Similar Messages

  • How do you add a task for disconnected hardware?

    I'm have a Labview 2009 project that contains an existing data acquisition task that includes several analog inputs from a cDAQ1 device.  I'm working on the Labview software, but don't currently have the hardware available.   I need to add an addition channel to this task, but without having the physical hardware connected. 
    When I attempt to do this in Labview 2009, using DAQ Assistant, the analog channel that I want to add is not displayed as an option to select.  The missing hardware shows (cDAQ1), up in the existing task, and is listed in MAX with a red x next to it because its not connected.    I have a separate task that used simulated channels, so I'm not asking about simulated hardware.
    I attempted to export the projects DAQ configuration file, edit it, then import it back into the project, but I get an error dialog box when I attempt to do this, Error -201064 occurred at _DAQmxImportConfig.vi", Configuration file is missing the required header fields".    I didn't remove any header fields, but I've had problems with DAQ Import before because of the different file formats for exported files, (see forums under Error -201064).  Unfortunately, the Labview Projects differ from MAX in that it only provides a "text, .txt" export format and that's a problem for importing.
    Nevermind.  I figure out if I manually select the ".ini" file time under Export, (.txt is the default), THEN Export will write to a different format and as long as I edit it correctly, I can then import it back into the project.
    Message Edited by PointOnePa on 04-25-2010 04:45 PM

    Go to Settings > Phone > My Number and add your number there if it's not already.
    Then go to Settings > Messages > Send & Receive > Apple ID and enter your data there.
    That should work.

  • How do you get a refund for an app that was purchased through the new App Catalog for a WebOS app!

    Hi,
    I just purchased a Dictionary app from Palm's App Catalog, the product is listed as: English Dictionary and Thesarus by Ultralingua, Inc. and it was not something that was advertised correctly and hence is not something that I want.
    It costs a whopping $9.99 and if it was as advertised, it would have been worth the money but it is not.
    I've had Dictionaries on my Palm Devices so when I saw the first directory for the WebOs for use on Pre, I immediately got it.
    However, unlike my old Palm dictionaries, this dictionary requires you to have a network connection.  Something that is not useful for me.  I sometimes like to look up word while on the subway to work (I live in NYC) where this is no network connection.
    There was nothing in the description that said it 'required' a network connection, and considering that all of the dictionaries I have gotten in the past did not require a network connection, I assumed (since it didn't state anything) that I could use it off-line.
    Heck, since you are required to use this dictionary online, I might as well use google to lookup a word and that is free!
    So I believe I was tricked into getting this app, and I want a refund!
    And there doesn't seem to be a way to get a refund for an app that did not disclose it's requirements before I made my purchase.
    Thanks for you help in finding out how to get a refund when its needed!
    Rob
    Post relates to: Pre p100eww (Sprint)

    Palm does have the ability to remove apps remotely, that was added in the last update.
    I knew that would be Palm's response, but I earn you now, Palm better make sure they put a better description on these apps.  And in this case its Palm's fault for not disclosing that this app required network support in order to work.
    They should at least make that part of a feature description.
    This is the second app that I purchased that was not advertised correctly.  So I far I have spent good money on crap only because the descriptions did not contain the right info.
    I see the only solution is to make all apps have at least a one hour free trail, or whatever free trial you want.
    I and I'm sure I'm not alone can't keep purchasing stuff that either doesn't work as advertised or is misleading in the description.
    I mean really Palm, you approved a dictionary app that just does a google in the background and displays the definition.
    I can do that same thing by typing it on my Pre and hitting Google?
    I still feel cheated and ripped off.
    I signed up for Palm and WebOS because I wanted to give you a second chance but that love only goes so far.  I think Andriod will be in my life soon.  I can't stand by and let myself be taken advantage of.
    I just can't believe how angry and upset I AM over this.
    Rob

  • HT2534 how do you create an AppleID for a student who does not meet the minimum age requirements?

    How do you create an Apple ID for a student who does not meet the "minimum age" requirements?

    A parent/guardian has to create it and supervise its use.

  • How do you  add new even to iCal? (and my spell check)

    ok i am ********
    in my last post i spelled mail as male....
    Here is my question .. each time i try to add a new event to iCal i get a pop up window that says "You can not add events to MAIL calendar. "
    I searched the helps menu and i did exactly what it says but each time i try to add an even i get the same pop up. And also on the pull down the add even is shaded out.

    Dean,
    Here is what iCal Help says about being unable to change an event or calendar.
    If you can’t change an event or calendar, check the following:
    If it’s a calendar to which you’ve subscribed, you can change the calendar name but you can’t change its events or to-do items.
    If you were invited to an event, you can change only your status for the event, the calendar where the event appears, and the event alarm.
    You can’t change or delete events on the Birthdays calendar. (To show the Birthdays calendar, select “Show Birthdays calendar” in iCal preferences.) To change the birthdays that appear, open Address Book (in your Applications folder) and make your changes there. For more information, in Address Book choose Help > Address Book Help.
    If you can’t change a single event’s time zone or the iCal time zone, time zone support may be turned off. Choose iCal > Preferences, click Advanced, and then make sure the “Turn on time zone support” checkbox is selected.
    If you want to change events on a calendar that you published on the Internet, but you’ve deleted the calendar from your Calendars list, you need to re-create the calendar with all its events, give it the same name as the published calendar, and then publish the new calendar. The new calendar will replace the previously published one.
    If you can’t change an event for which you are the organizer, or you can’t change the attendee settings for an event you’ve been invited to, make sure all your email addresses are listed on your Address Book card (the card marked “me”).
    1. Do any of those recommendations help you further define your problem.
    2. Is there any reason you have not yet upgraded to OS X 10.5.3?
    ;~)

  • How Do You Add An Extension For A Phone Number?

    I wanted to add an extension after the phone number for a contact, how do I do that?

    Hello:
    AS indicated, you would just add it after the last part of the number (in that box).
    (800) 123 4567 x 9999
    Barry

  • HT5312 Please help me.  How do you add a rescue e mail address to your account  ?  The article didn't help.

    I cant change my security questions because I do not have a rescue e mail address.  I can't add the email. What are the steps to do so..

    You need to contact Apple. Click here, phone them, and ask for the Account Security team.
    (87031)

  • How do you add a class in fluid grid navigation menu to change the background and link color when page is active?

    When i try to add a class to the active page to have it distinguished when a visitor is on, in the class section it only has other options and not one for "active"

    See this link:
    Making a persistent page indicator on site wide CSS Menus
    Nancy O.

  • How do you add files to a target of an Xcode project from the terminal?

    Hi friends.
    I am building a tool to build projects in an automated way. So far it is working great but I can't find a command to add files to a target from the command line.
    Basically what I want to do is the following:
    I have a directory with the XCode project, something like:
    - Classes
    - Images
    - Configuration
    - Etc
    I want to add some files to the folder Images that will be included in the build output.
    Is this possible?
    Thanks!!

    Is this helpful?
    http://www.askdavetaylor.com/fancymac_os_x_command_linetricks.html

  • Itunes wi-fi sync; how do I add a name for my new computer on the ipad 4?

    RE: itunes wi-fi sync;
    How do I add a name for my new macbook pro computer on the ipad 4?

    You cannot add a name for Mac on the iPad. I assume that you mean in the WiFi sync setting - not possible.

  • How do you schedule weekdays only for iCal app on iPhone?

    how do you schedule weekdays only for iCal app on iPhone?

    aprishwrs,
    The scheduling options on the iPhone are limited to non-existant. You will have to use your Desktop iCal to make any desired repeat options.

  • How to check the date in the date format

    hi all,
    i am using db10g
    i have to read the data column from some source.
    I should accept as it is and validate whether that date is in the date format how can i validate this condition?
    Thanks..

    user13329002 wrote:
    hi all,
    i am using db10g
    i have to read the data column from some source.
    I should accept as it is and validate whether that date is in the date format how can i validate this condition?
    Thanks..Ask your source what the date format is. If it is constantly changing then the format needs to be transmitted also.
    Imagine a string (that could represent a date) like this:
    10-01-11
    it could mean:
    10th of January 2011
    1st of October 2011
    11th of January 2010
    1st of November 2011
    11th of October 2001 (highly unlikly but still possible).
    How should an automated system decide which is the correct date? You need to establish a rule that includes a date format and therefore returns the correct value. Or even better: Deal with dates, not with strings. This is especially for those Java/VB programmers who have no real clue what a data type is ment for.

  • How to change the date format?

    Hi,
    I need to display the data format as(YYYY-MM-DD). But now it displays(2009-1-9)
    Here is my code snippet which i used to display the data format as(2009-1-9)
    *<INPUT TYPE=TEXT NAME="date_submitted" MAXLENGTH=20 SIZE=10 VALUE="" onBlur= "return dateSubmitted()">  (YYYY-MM-DD)*
    *<SCRIPT LANGUAGE="javascript">*
    dateSubmitted()
    *</SCRIPT>*
    function dateSubmitted()
                        if (document.pgUpdate.date_submitted.value == "")
                             date = new Date();     
                             month = date.getMonth() + 1     
                             document.pgUpdate.date_submitted.value =
                                            date.getYear() + "-" + month + "-" + date.getDate();
                        return true;
    Can anybody help me how to change the date format?
    Thanks in advance!

    prit123 wrote:
    use SimpleDateFormat class. The code is :He posted a Javascript related question, not a Java related question.
    Please use forums devoted to Javascript. You're here at a Java/JSP forum.
    There are JS forums at webdeveloper.com and dynamicdrive.com. Good luck.
    String formatPattern = "yyyy-mm-dd";
    SimpleDateFormat sdf = new SimpleDateFormat(formatPattern);
    sdf.format(yourdate);yyyy-mm-dd denotes year-minutes-days. Please go read the SimpleDateFormat API as well.

  • How do you add the language of the dictionary in pages for the whole document?

    How do you add the language of the dictionary in pages for the whole document. When I add the british language it does not stay ON for the whole document. It goes back to "none"???? I am using pages 09 version 4.1 (923).

    This is a feature of Numbers.   If you repost on the iWork forum you may get more information.

  • How do you add a Comment when editing document in Word for ipad?

    How do you add a Comment when editing a document in Word for ipad. I've subscribed to Office, so have full editing functions. Can Track changes etc in Review, but can't see how to add a marginal Comment.

    Comments, like they are in Numbers for OS X, are not really supported in iOS. Normally, they rely on a mouse-over to read them. Since there is no mouse on the iPad, it stands to reason they are not there.
    I believe Eric Ross's comments is more geared towards used said Numbers docs on iWork.com.

Maybe you are looking for