Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. avoiding 1/1/1900 in SQL SERVER?

avoiding 1/1/1900 in SQL SERVER?

Scheduled Pinned Locked Moved Database
csharpdatabaseasp-netsql-serversysadmin
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John Sundar
    wrote on last edited by
    #1

    while updating or inserting a record in SQL SERVER, by default the DATETIME variable getting 1/1/1900. i need a trigger, so that whenever UPDATE or INSERT is made in a particular table, and if datetime consist of 1/1/1900, those values should replace as NULL.... can anyone provide the trigger for this? or is there anyother solution? Currently i m using ASP.NET with C# (.net 2003, 1.1 framework) - KARAN

    R M 2 Replies Last reply
    0
    • J John Sundar

      while updating or inserting a record in SQL SERVER, by default the DATETIME variable getting 1/1/1900. i need a trigger, so that whenever UPDATE or INSERT is made in a particular table, and if datetime consist of 1/1/1900, those values should replace as NULL.... can anyone provide the trigger for this? or is there anyother solution? Currently i m using ASP.NET with C# (.net 2003, 1.1 framework) - KARAN

      R Offline
      R Offline
      Rocky
      wrote on last edited by
      #2

      I'm sorry I can write the whole Trigger for u but I'm gonna give u a few clues to start with . First you need to make an INSTEAD OF trigger for insert and updates in that you will use the updated and inserted values. if the date in the inserted table matches 1/1/1900 dont issue an insert/update statement. Otherwise execute the insert statement. I hope this will help you

      Rocky Success is a ladder which you can't climb with your hands in your pockets.

      1 Reply Last reply
      0
      • J John Sundar

        while updating or inserting a record in SQL SERVER, by default the DATETIME variable getting 1/1/1900. i need a trigger, so that whenever UPDATE or INSERT is made in a particular table, and if datetime consist of 1/1/1900, those values should replace as NULL.... can anyone provide the trigger for this? or is there anyother solution? Currently i m using ASP.NET with C# (.net 2003, 1.1 framework) - KARAN

        M Offline
        M Offline
        Mark J Miller
        wrote on last edited by
        #3

        If you have full control of the application's source code, a better solution than a Trigger would be to check the value in the application code for 1/1/1900. If it matches 1/1/1900, then set the parameter value for your SqlParameter object to DbNull.Value instead of the actual datatime value. If you aren't using SqlParameter objects (which you should be) then set the value of the string to "NULL". Example 1: using(SqlCommand cmd = new SqlCommand(.. set ctor arguments ..)){ // setup cmd object cmd.Properties.Add("@date", SqlDbType.DateTime); if(myDateVariable.ToShortDateString() == "1/1/1900") cmd.Properties["@date"] = DbNull.Value; else cmd.Properties["@date"] = myDateVariable; } Example 2: StringBuilder sql = new StringBuilder("INSERT INTO myTable(dateColumn) VALUES("); if(myDateVariable.ToShortDateString() == "1/1/1900") sql.Append("NULL"); else sql.Append(String.Format("'{0}'", myDateVariable)); NOTE: The second example will expose you to Sql Injection attacks if the value you are concatenating to your SQL statement is a String variable, so I recommend the first method for all SQL statements.

        Mark's blog: developMENTALmadness.blogspot.com

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups