将值插入其他日期时间列的datetime列

时间:2015-10-22 05:52:31

标签: sql sql-server datetime type-conversion

我遇到了一个问题,该问题应该将行插入表中。

该表具有以下属性:

CREATE TABLE [dbo].[New_Table](
    [ID] [nvarchar](32) NOT NULL,
    [Position] [smallint] NOT NULL,
    [...]
    [OrderDate] [datetime] NULL,
    [...]
 CONSTRAINT [PK_New_Table] PRIMARY KEY CLUSTERED 
(
    [ID] ASC,
    [Position] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

OrderDate列是表格中唯一的日期时间列。

从下表中检索OrderDate

CREATE TABLE [dbo].[Old_Table](
    [ID] [nvarchar](32) NOT NULL,
    [Number] [nvarchar](15) NOT NULL,
    [...]
    [Date] [datetime] NULL,
    [...]
 CONSTRAINT [PK_Old_Table] PRIMARY KEY CLUSTERED 
(
    [ID_A_Kopf] ASC,
    [Number] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

查询很简单:

INSERT INTO [dbo].[New_Table]
SELECT DISTINCT
  DataTable.ID,
  DataTable.Position,
  DataTable.System,
  b.Date
FROM
  DataTable LEFT JOIN
  (   SELECT
        OldTable.ID,
        OldTable.Date
      FROM
        OldTable
      WHERE
        OldTable.Number = <Condition>) b ON DataTable.ID = b.ID

DataTable的联接没有其他日期时间列。

当我尝试运行查询时,收到以下错误消息:

Meldung 241, Ebene 16, Status 1, Zeile 2
Fehler beim Konvertieren einer Zeichenfolge in ein Datum und/oder eine Uhrzeit.

转换为Conversion failed when converting date and/or time from character string

我试图在使用IsDate功能的时候把它放在一个案例中,但它也没有用。允许使用NULL值,如New_Table的表定义中所示。

所有这些都在:

Microsoft SQL Server Management Studio                      11.0.2100.60
Microsoft Data Access Components (MDAC)                     6.1.7601.17514
Microsoft MSXML                     2.6 3.0 4.0 6.0 
Microsoft Internet Explorer                     9.10.9200.17054
Microsoft .NET Framework                        4.0.30319.34209
Betriebssystem                      6.1.7601

1 个答案:

答案 0 :(得分:-1)

我很确定您将日期/时间列放在目标表中的错误位置。