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. General Programming
  3. C#
  4. Change SQL Server dateformat?

Change SQL Server dateformat?

Scheduled Pinned Locked Moved C#
databasesql-serversysadminhelpquestion
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.
  • D Offline
    D Offline
    Dotnetkanna
    wrote on last edited by
    #1

    Hi, By default sql server will accept date format of mdy.is it possible to change the format to dmy. SET DATEFORMAT dmy GO INSERT tDateOrder (d) VALUES ('31/1/04') GO INSERT tDateOrder (d) VALUES ('2/28/04') by executing the above statement im getting the below error. (1 row(s) affected) Msg 242, Level 16, State 3, Line 1 The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. The statement has been terminated. is it possible to change the database format as dmy permanently? and also it should change only for this particular database. Regards Kanna

    L T 2 Replies Last reply
    0
    • D Dotnetkanna

      Hi, By default sql server will accept date format of mdy.is it possible to change the format to dmy. SET DATEFORMAT dmy GO INSERT tDateOrder (d) VALUES ('31/1/04') GO INSERT tDateOrder (d) VALUES ('2/28/04') by executing the above statement im getting the below error. (1 row(s) affected) Msg 242, Level 16, State 3, Line 1 The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. The statement has been terminated. is it possible to change the database format as dmy permanently? and also it should change only for this particular database. Regards Kanna

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Wrong forum!You should better post your question to General database forum. :)

      Dotnetkanna wrote:

      INSERT tDateOrder (d) VALUES ('2/28/04')

      It isn't valid date,[EDIT]if the date format is dmy which means day/month/year. [/EDIT] An year contains 12 months not 28.You may consider change to query to: INSERT tDateOrder (d) VALUES ('28/2/04')

      Life is a stage and we are all actors!

      1 Reply Last reply
      0
      • D Dotnetkanna

        Hi, By default sql server will accept date format of mdy.is it possible to change the format to dmy. SET DATEFORMAT dmy GO INSERT tDateOrder (d) VALUES ('31/1/04') GO INSERT tDateOrder (d) VALUES ('2/28/04') by executing the above statement im getting the below error. (1 row(s) affected) Msg 242, Level 16, State 3, Line 1 The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value. The statement has been terminated. is it possible to change the database format as dmy permanently? and also it should change only for this particular database. Regards Kanna

        T Offline
        T Offline
        T M Gray
        wrote on last edited by
        #3

        You shouldn't be passing strings to the database for dates. If you use parameterized queries the conversion happens for you automatically. Parameterized Queries[^] This example only shows text, but if you use the following syntax it will do all the type checking before it gets to the database.

        cmd.Parameters.Add("@MyDate", SqlDbType.DateTime);
        cmd.Parameters["@MyDate"].Value = yourDateTimeVariable;

        This is also the recommended way to do queries to prevent SQL injection attacks becuase it also handles things like single quotes and other things you would have to escape manually.

        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