使用GetDate时出现Sql错误

时间:2018-03-24 10:32:58

标签: sql sql-server tsql

  

Msg 241,Level 16,State 1,Line 38   从字符串转换日期和/或时间时转换失败。

我试过“getdate”和“getdate()没有运气。”

insert into Maintenance([MaintenanceNotes],[MaintenanceDate],[MaintenanceKey])
values ('<?xml version="1.0" encoding="utf-8"?>
<maintenancenote xmlns="http://www.metroalt.com/maintenancenote">
  <title>Wear and Tear on Hydralic units</title>
<note>
  <p>The hydralic units are showing signs of stress</p>
  <p>I recommend the replacement of the units</p>
</note>
  <followup>Schedule replacement for June 2016</followup>
</maintenancenote>', 'GETDATE', 'bus4') 

1 个答案:

答案 0 :(得分:0)

您需要删除引号,并将其用作实际功能:

    insert into Maintenance([MaintenanceNotes],[MaintenanceDate],[MaintenanceKey])
    values ('<?xml version="1.0" encoding="utf-8"?>
    <maintenancenote xmlns="http://www.metroalt.com/maintenancenote">
      <title>Wear and Tear on Hydralic units</title>
    <note>
      <p>The hydralic units are showing signs of stress</p>
      <p>I recommend the replacement of the units</p>
    </note>
      <followup>Schedule replacement for June 2016</followup>
    </maintenancenote>', GETDATE(), 'bus4') 

为了澄清,两个单引号之间的任何内容都被视为文本字符串。 GETDATE()是一个函数,因此您需要确保它不包含在引号之间。