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. nvarchar to datetime conversion plz!!!!!!!!!!!!!!!!!!!!!!!!!!

nvarchar to datetime conversion plz!!!!!!!!!!!!!!!!!!!!!!!!!!

Scheduled Pinned Locked Moved Database
helptutorialquestion
6 Posts 4 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.
  • L Offline
    L Offline
    Learner520
    wrote on last edited by
    #1

    hi everybody i have two tables table1 with column name SubmissionDate nvarchar(11) table2 with column name submissiondate2 (datetime) and i insert data in table1 like 17/05/2009 but still in nvarchar format cos this is my requirement (i mean nvarchar) my question is i want to insert same date from table1's column to table2's column but dont know how to convert datatype nvarchar to datetime. when i try to cast i got this error msg Arithmetic overflow error converting expression to data type datetime. any help would be really appricated

    M B M 3 Replies Last reply
    0
    • L Learner520

      hi everybody i have two tables table1 with column name SubmissionDate nvarchar(11) table2 with column name submissiondate2 (datetime) and i insert data in table1 like 17/05/2009 but still in nvarchar format cos this is my requirement (i mean nvarchar) my question is i want to insert same date from table1's column to table2's column but dont know how to convert datatype nvarchar to datetime. when i try to cast i got this error msg Arithmetic overflow error converting expression to data type datetime. any help would be really appricated

      M Offline
      M Offline
      Manas Bhardwaj
      wrote on last edited by
      #2

      http://msdn.microsoft.com/en-us/library/ms187928.aspx[^]

      Please remember to rate helpful or unhelpful answers, it lets us and people reading the forums know if our answers are any good.

      1 Reply Last reply
      0
      • L Learner520

        hi everybody i have two tables table1 with column name SubmissionDate nvarchar(11) table2 with column name submissiondate2 (datetime) and i insert data in table1 like 17/05/2009 but still in nvarchar format cos this is my requirement (i mean nvarchar) my question is i want to insert same date from table1's column to table2's column but dont know how to convert datatype nvarchar to datetime. when i try to cast i got this error msg Arithmetic overflow error converting expression to data type datetime. any help would be really appricated

        B Offline
        B Offline
        Blue_Boy
        wrote on last edited by
        #3

        insert into table2
        select cast(submissiondate2 as datetime) from table1


        I Love T-SQL "Don't torture yourself,let the life to do it for you." If my post helps you kindly save my time by voting my post.

        1 Reply Last reply
        0
        • L Learner520

          hi everybody i have two tables table1 with column name SubmissionDate nvarchar(11) table2 with column name submissiondate2 (datetime) and i insert data in table1 like 17/05/2009 but still in nvarchar format cos this is my requirement (i mean nvarchar) my question is i want to insert same date from table1's column to table2's column but dont know how to convert datatype nvarchar to datetime. when i try to cast i got this error msg Arithmetic overflow error converting expression to data type datetime. any help would be really appricated

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          This is a fundamental error that a lot of newbies make - storing dates as strings. Your dates are stored in a format other than yyyy-mm-dd, probably dd/mm/yyyy and SQL cannot decide between the dd and mm structures, therefore the error. Try using Convert(datetime,datefield,106) or one of the other formatters. Under convert/cast in BOL.

          Never underestimate the power of human stupidity RAH

          L 1 Reply Last reply
          0
          • M Mycroft Holmes

            This is a fundamental error that a lot of newbies make - storing dates as strings. Your dates are stored in a format other than yyyy-mm-dd, probably dd/mm/yyyy and SQL cannot decide between the dd and mm structures, therefore the error. Try using Convert(datetime,datefield,106) or one of the other formatters. Under convert/cast in BOL.

            Never underestimate the power of human stupidity RAH

            L Offline
            L Offline
            Learner520
            wrote on last edited by
            #5

            thanks holmes for your kind answer my Problem; the following code is working perfectly except date. every thing is working perfectly pleas let me know how to add Date into table through following query, and dont worry about other variables these are working perfectly because i have changed them into varchar like this SELECT @str_Session_Id=CONVERT(nvarchar(10), CONVERT(int, @SessionId)) but i dont want to add date like varchar because i have to compare date with other dates therefore i dont want to convert into varchar. select @date =(select DATEADD(dd, 1, GETDATE())) Begin set @txtSql = case when (@Std_Count < 3) then 'insert into ProjectRequests values('+ @str_stu_Id+','+ @str_Priority_Id +','+ @str_Project_Id +' , '+ @str_Session_Id +', '+ @date +' , NULL)' Msg 241, Level 16, State 1, Procedure FUALLY_QUALIFIED_STUDENT_BULK, Line 48 Conversion failed when converting datetime from character string. any help would be appreciated.

            M 1 Reply Last reply
            0
            • L Learner520

              thanks holmes for your kind answer my Problem; the following code is working perfectly except date. every thing is working perfectly pleas let me know how to add Date into table through following query, and dont worry about other variables these are working perfectly because i have changed them into varchar like this SELECT @str_Session_Id=CONVERT(nvarchar(10), CONVERT(int, @SessionId)) but i dont want to add date like varchar because i have to compare date with other dates therefore i dont want to convert into varchar. select @date =(select DATEADD(dd, 1, GETDATE())) Begin set @txtSql = case when (@Std_Count < 3) then 'insert into ProjectRequests values('+ @str_stu_Id+','+ @str_Priority_Id +','+ @str_Project_Id +' , '+ @str_Session_Id +', '+ @date +' , NULL)' Msg 241, Level 16, State 1, Procedure FUALLY_QUALIFIED_STUDENT_BULK, Line 48 Conversion failed when converting datetime from character string. any help would be appreciated.

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              Do some research into parameterised queries, they will make your life so much easier. Inserting date using a string var is Ok, you just need to format it correctly, the following are valid formats for SQL Server. The important thing is to STORE it as a datetime data type in the database. yyyy/mm/dd dd/MMM/yyyy mm/dd/yyyy If you try to use a date in dd/mm/yyyy format it will create the error you are getting. MS is a US company so SQL Server used the stupid freakin US date format internally. Also you need to add additional ' around your date value.

              Never underestimate the power of human stupidity RAH

              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