将nvarchar数据类型转换为datetime数据类型

时间:2022-07-31 16:43:43

I have a query like below:

我有一个如下查询:

select 
  ...
 convert(VARCHAR(10), DATEADD(day,t.DAYNUMBER,t.START_DATE), 103) AS END_DATE
 where END_DATE >=  GETDATE()

It displays the following error:

它显示以下错误:

The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value.

将nvarchar数据类型转换为日期时间数据类型会导致超出范围的值。

t.DAYNUMBER comes from:

t.DAYNUMBER来自:

select max([day]) from TOUR_SPECIFIKA 

where day is int. And t.START_DATE is date. I don't know why it shows nvarchar in error.

哪天是int。并且t.START_DATE是日期。我不知道为什么它显示nvarchar错误。

1 个解决方案

#1


0  

If you have this code:

如果你有这个代码:

select ...,
       convert(VARCHAR(10), DATEADD(day, t.DAYNUMBER, t.START_DATE), 103) AS END_DATE
where END_DATE >= GETDATE()

And you are getting that particular error, then either t.START_DATE or t.END_DATE are nvarchar() rather than some date/time data type. One of these columns contains an invalid date.

并且您收到该特定错误,然后t.START_DATE或t.END_DATE是nvarchar()而不是某些日期/时间数据类型。其中一列包含无效日期。

Note that the reference in the where clause is to a column in one of the tables. It is not a reference to the alias in the SELECT.

请注意,where子句中的引用是指其中一个表中的列。它不是SELECT中别名的引用。

#1


0  

If you have this code:

如果你有这个代码:

select ...,
       convert(VARCHAR(10), DATEADD(day, t.DAYNUMBER, t.START_DATE), 103) AS END_DATE
where END_DATE >= GETDATE()

And you are getting that particular error, then either t.START_DATE or t.END_DATE are nvarchar() rather than some date/time data type. One of these columns contains an invalid date.

并且您收到该特定错误,然后t.START_DATE或t.END_DATE是nvarchar()而不是某些日期/时间数据类型。其中一列包含无效日期。

Note that the reference in the where clause is to a column in one of the tables. It is not a reference to the alias in the SELECT.

请注意,where子句中的引用是指其中一个表中的列。它不是SELECT中别名的引用。