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. DateTime Problem

DateTime Problem

Scheduled Pinned Locked Moved Database
helpquestion
7 Posts 2 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
    jonhbt
    wrote on last edited by
    #1

    Hi I have this code: Declare @v_update_sql varchar(max) Declare @d_Date datetime Declare @v_target_table_name varchar(max) Set @v_target_table_name = 'IM_DOC_TRANSFORM' Set @d_Date = '2009-03-02 12:46:00.000' Set @v_update_sql = 'Select * From HRS_STAGING.dbo.'+ @v_target_table_name + 'Where Last_Documented_Date_And_Time = Convert(datetime,'+@d_Date+')' Exec(@v_update_sql) The error that is arising is that of cannot convert to datetime from string.the variable @d_Date is a valid date but still it cannot convert it. That statement works went not in a string variable but in this format it doesn't. Can someone help me please? Thanks

    K 2 Replies Last reply
    0
    • J jonhbt

      Hi I have this code: Declare @v_update_sql varchar(max) Declare @d_Date datetime Declare @v_target_table_name varchar(max) Set @v_target_table_name = 'IM_DOC_TRANSFORM' Set @d_Date = '2009-03-02 12:46:00.000' Set @v_update_sql = 'Select * From HRS_STAGING.dbo.'+ @v_target_table_name + 'Where Last_Documented_Date_And_Time = Convert(datetime,'+@d_Date+')' Exec(@v_update_sql) The error that is arising is that of cannot convert to datetime from string.the variable @d_Date is a valid date but still it cannot convert it. That statement works went not in a string variable but in this format it doesn't. Can someone help me please? Thanks

      K Offline
      K Offline
      Kevin Horgan
      wrote on last edited by
      #2

      Hi jonhbt, Try this, you seem to be missing some qoutation makers on the CONVERT in your dynamic SQL which are necessary for a STRING to be concatenated. Set @v_update_sql = 'Select * From HRS_STAGING.dbo.'+ @v_target_table_name + 'Where Last_Documented_Date_And_Time = Convert(datetime,'''+@d_Date+''')' I hope this helps, Kevin

      J 1 Reply Last reply
      0
      • K Kevin Horgan

        Hi jonhbt, Try this, you seem to be missing some qoutation makers on the CONVERT in your dynamic SQL which are necessary for a STRING to be concatenated. Set @v_update_sql = 'Select * From HRS_STAGING.dbo.'+ @v_target_table_name + 'Where Last_Documented_Date_And_Time = Convert(datetime,'''+@d_Date+''')' I hope this helps, Kevin

        J Offline
        J Offline
        jonhbt
        wrote on last edited by
        #3

        Hi Kevin, Thanks for your help but the same happened. This is the error that I am Having. Conversion failed when converting datetime from character string.

        K 1 Reply Last reply
        0
        • J jonhbt

          Hi Kevin, Thanks for your help but the same happened. This is the error that I am Having. Conversion failed when converting datetime from character string.

          K Offline
          K Offline
          Kevin Horgan
          wrote on last edited by
          #4

          OK maybe this will help instead... Set @v_update_sql = 'Select * From HRS_STAGING.dbo.'+ @v_target_table_name + 'Where Last_Documented_Date_And_Time = CAST('''||@d_Date||''' AS DATETIME)' If it does not work, do a "SELECT @v_update_sql" immediately after the "SET @v_update_sql..." statement and see what the dynamic SQL looks like with the date string included. You might see an obvious syntax error then perhaps. Cheers, Kevin

          J 1 Reply Last reply
          0
          • K Kevin Horgan

            OK maybe this will help instead... Set @v_update_sql = 'Select * From HRS_STAGING.dbo.'+ @v_target_table_name + 'Where Last_Documented_Date_And_Time = CAST('''||@d_Date||''' AS DATETIME)' If it does not work, do a "SELECT @v_update_sql" immediately after the "SET @v_update_sql..." statement and see what the dynamic SQL looks like with the date string included. You might see an obvious syntax error then perhaps. Cheers, Kevin

            J Offline
            J Offline
            jonhbt
            wrote on last edited by
            #5

            thanks again Kevin but its not working either. Creating the select after is not showing the results since its stating an error near the |. thanks agin for your time

            1 Reply Last reply
            0
            • J jonhbt

              Hi I have this code: Declare @v_update_sql varchar(max) Declare @d_Date datetime Declare @v_target_table_name varchar(max) Set @v_target_table_name = 'IM_DOC_TRANSFORM' Set @d_Date = '2009-03-02 12:46:00.000' Set @v_update_sql = 'Select * From HRS_STAGING.dbo.'+ @v_target_table_name + 'Where Last_Documented_Date_And_Time = Convert(datetime,'+@d_Date+')' Exec(@v_update_sql) The error that is arising is that of cannot convert to datetime from string.the variable @d_Date is a valid date but still it cannot convert it. That statement works went not in a string variable but in this format it doesn't. Can someone help me please? Thanks

              K Offline
              K Offline
              Kevin Horgan
              wrote on last edited by
              #6

              Hi again, One thing I just noticed is you need a SPACE before the "WHERE clause" in the SQL example code you posted above. At the moment it dynamic SQL command looks like this. Select * From HRS_STAGING.dbo.IM_DOC_TRANSFORMWhere Last_Documented_Date_And_Time = CAST('2009-03-02 12:46:00.000' AS DATETIME) Try adding a space as follows. I have also replaced the "||" with "+" if you would prefer this method of concatenation. declare @v_update_sql varchar(1000) declare @d_date char(23) select @d_date = '2009-03-02 12:46:00.000' declare @sql varchar(1000) declare @v_target_table_name varchar(100) Set @v_target_table_name = 'IM_DOC_TRANSFORM' Set @v_update_sql = 'Select * From HRS_STAGING.dbo.'+ @v_target_table_name + ' Where Last_Documented_Date_And_Time = CAST('''+@d_Date+''' AS DATETIME)' select @v_update_sql execute (@v_update_sql) The dynamic SQL should then look like this... Select * From HRS_STAGING.dbo.IM_DOC_TRANSFORM Where Last_Documented_Date_And_Time = CAST('2009-03-02 12:46:00.000' AS DATETIME) See if that works for you. Cheers, Kevin

              J 1 Reply Last reply
              0
              • K Kevin Horgan

                Hi again, One thing I just noticed is you need a SPACE before the "WHERE clause" in the SQL example code you posted above. At the moment it dynamic SQL command looks like this. Select * From HRS_STAGING.dbo.IM_DOC_TRANSFORMWhere Last_Documented_Date_And_Time = CAST('2009-03-02 12:46:00.000' AS DATETIME) Try adding a space as follows. I have also replaced the "||" with "+" if you would prefer this method of concatenation. declare @v_update_sql varchar(1000) declare @d_date char(23) select @d_date = '2009-03-02 12:46:00.000' declare @sql varchar(1000) declare @v_target_table_name varchar(100) Set @v_target_table_name = 'IM_DOC_TRANSFORM' Set @v_update_sql = 'Select * From HRS_STAGING.dbo.'+ @v_target_table_name + ' Where Last_Documented_Date_And_Time = CAST('''+@d_Date+''' AS DATETIME)' select @v_update_sql execute (@v_update_sql) The dynamic SQL should then look like this... Select * From HRS_STAGING.dbo.IM_DOC_TRANSFORM Where Last_Documented_Date_And_Time = CAST('2009-03-02 12:46:00.000' AS DATETIME) See if that works for you. Cheers, Kevin

                J Offline
                J Offline
                jonhbt
                wrote on last edited by
                #7

                thanks for your help it worked

                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