从字符串转换日期 and/or 时间时转换失败 - 为什么?
Conversion failed when converting date and/or time from character string - why?
这是一个 SQL 子查询,给出的错误是
"Conversion failed when converting date and/or time from character string.".
我试过用 datetime2 替换 datetime 但它不起作用。
declare @cols as NVARCHAR(max)
, @cols2 as NVARCHAR(max)
, @query as NVARCHAR(max) select
@cols = STUFF(
(
select
',' + QUOTENAME(ProductOptionName)
from
OrderProductVariantOption [NL]
inner join OrderProductVariant [NL] on
OrderProductVariant.Id = OrderProductVariantOption.OrderProductVariantId
where
(
OrderProductVariant.AcceptedById = '[Business|0]'
or OrderProductVariant.MRefId = '[Business|0]'
)
and [OrderProductVariant.MarketplaceGroupId=Marketplace]
and OrderProductVariant.DateCreated >= cast(cast('[startdate]' as date) as datetime2)
and OrderProductVariant.DateCreated < cast(cast('[enddate]' as date) as datetime2)
group by
ProductOptionName
order by
ProductOptionName for XML path('')
, TYPE
)
. value('.', 'NVARCHAR(MAX)')
, 1
, 1
, ''
)
您引用了 [startdate]
和 [enddate]
。因此,它们是常量字符串值 [startdate] 和 [enddate],不再是字段名称。
尝试
and OrderProductVariant.DateCreated >= cast([startdate] as date)
and OrderProductVariant.DateCreated < cast([enddate] as date)
并且您应该使用 CONVERT
函数而不是 CAST
,因为您可以将日期格式设置为参数。
'[startdate]'
和 '[enddate]'
是文字字符串值 - 您不会尝试将名为 startdate
(或 enddate
)的列中的值转换为日期,但实际的字符串值:[startDate]
(或[enddate]
)。
当然,这些字符串不能转换为日期值。
此外,您的查询还有另一个问题 - 您正试图对连接中的两个表使用相同的别名 - 这将导致以下错误:
The correlation name 'NL' is specified multiple times in a FROM clause.
您需要更新您的查询,因为您的查询中存在一些语法问题。在 sql server
中使用 []
声明您的列名。 ''
用于声明字符串值。因此,从您的日期列中删除 '
。
还需要删除 table 别名 [NL]
,因为您为两个 table 声明了相同的别名并在查询中直接使用 table 名称。
最重要的是,您的 select 查询从 declare 语句开始,这进一步给出了错误。从下一行写下您的查询。
declare @cols as NVARCHAR(max)
, @cols2 as NVARCHAR(max)
, @query as NVARCHAR(max)
select
@cols = STUFF(
(
select
',' + QUOTENAME(ProductOptionName)
from
OrderProductVariantOption ---- remove [NL] since you are using table name directly in your query
inner join OrderProductVariant on
OrderProductVariant.Id = OrderProductVariantOption.OrderProductVariantId
where
(
OrderProductVariant.AcceptedById = '[Business|0]' --- check over there too
or OrderProductVariant.MRefId = '[Business|0]'
)
and [OrderProductVariant.MarketplaceGroupId=Marketplace]
and OrderProductVariant.DateCreated >= cast([startdate] as date)
and OrderProductVariant.DateCreated < cast([enddate] as date)
group by
ProductOptionName
order by
ProductOptionName for XML path('')
, TYPE
)
. value('.', 'NVARCHAR(MAX)')
, 1
, 1
, ''
)
使用这个:
and OrderProductVariant.DateCreated >= Convert(datetime,[startdate])
and OrderProductVariant.DateCreated < Convert(datetime,[enddate])
删除[NL],因为[NL]被多次使用。
可能 OrderProductVariant.DateCreated 日期格式不同。所以也要更正它的格式。
and cast(OrderProductVariant.DateCreated as date) >= cast([startdate] as date)
最后,您的代码将如下所示:
declare @cols as NVARCHAR(max)
, @cols2 as NVARCHAR(max)
, @query as NVARCHAR(max) select
@cols = STUFF(
(
select
',' + QUOTENAME(ProductOptionName)
from
OrderProductVariantOption
inner join OrderProductVariant on
OrderProductVariant.Id = OrderProductVariantOption.OrderProductVariantId
where
(
OrderProductVariant.AcceptedById = '[Business|0]'
or OrderProductVariant.MRefId = '[Business|0]'
)
and [OrderProductVariant.MarketplaceGroupId=Marketplace]
and cast(OrderProductVariant.DateCreated as date) >= cast([startdate] as date)
and cast(OrderProductVariant.DateCreated as date) < cast([enddate] as date)
group by
ProductOptionName
order by
ProductOptionName for XML path('')
, TYPE
)
. value('.', 'NVARCHAR(MAX)')
, 1
, 1
, ''
)
这是一个 SQL 子查询,给出的错误是
"Conversion failed when converting date and/or time from character string.".
我试过用 datetime2 替换 datetime 但它不起作用。
declare @cols as NVARCHAR(max)
, @cols2 as NVARCHAR(max)
, @query as NVARCHAR(max) select
@cols = STUFF(
(
select
',' + QUOTENAME(ProductOptionName)
from
OrderProductVariantOption [NL]
inner join OrderProductVariant [NL] on
OrderProductVariant.Id = OrderProductVariantOption.OrderProductVariantId
where
(
OrderProductVariant.AcceptedById = '[Business|0]'
or OrderProductVariant.MRefId = '[Business|0]'
)
and [OrderProductVariant.MarketplaceGroupId=Marketplace]
and OrderProductVariant.DateCreated >= cast(cast('[startdate]' as date) as datetime2)
and OrderProductVariant.DateCreated < cast(cast('[enddate]' as date) as datetime2)
group by
ProductOptionName
order by
ProductOptionName for XML path('')
, TYPE
)
. value('.', 'NVARCHAR(MAX)')
, 1
, 1
, ''
)
您引用了 [startdate]
和 [enddate]
。因此,它们是常量字符串值 [startdate] 和 [enddate],不再是字段名称。
尝试
and OrderProductVariant.DateCreated >= cast([startdate] as date)
and OrderProductVariant.DateCreated < cast([enddate] as date)
并且您应该使用 CONVERT
函数而不是 CAST
,因为您可以将日期格式设置为参数。
'[startdate]'
和 '[enddate]'
是文字字符串值 - 您不会尝试将名为 startdate
(或 enddate
)的列中的值转换为日期,但实际的字符串值:[startDate]
(或[enddate]
)。
当然,这些字符串不能转换为日期值。
此外,您的查询还有另一个问题 - 您正试图对连接中的两个表使用相同的别名 - 这将导致以下错误:
The correlation name 'NL' is specified multiple times in a FROM clause.
您需要更新您的查询,因为您的查询中存在一些语法问题。在 sql server
中使用 []
声明您的列名。 ''
用于声明字符串值。因此,从您的日期列中删除 '
。
还需要删除 table 别名 [NL]
,因为您为两个 table 声明了相同的别名并在查询中直接使用 table 名称。
最重要的是,您的 select 查询从 declare 语句开始,这进一步给出了错误。从下一行写下您的查询。
declare @cols as NVARCHAR(max)
, @cols2 as NVARCHAR(max)
, @query as NVARCHAR(max)
select
@cols = STUFF(
(
select
',' + QUOTENAME(ProductOptionName)
from
OrderProductVariantOption ---- remove [NL] since you are using table name directly in your query
inner join OrderProductVariant on
OrderProductVariant.Id = OrderProductVariantOption.OrderProductVariantId
where
(
OrderProductVariant.AcceptedById = '[Business|0]' --- check over there too
or OrderProductVariant.MRefId = '[Business|0]'
)
and [OrderProductVariant.MarketplaceGroupId=Marketplace]
and OrderProductVariant.DateCreated >= cast([startdate] as date)
and OrderProductVariant.DateCreated < cast([enddate] as date)
group by
ProductOptionName
order by
ProductOptionName for XML path('')
, TYPE
)
. value('.', 'NVARCHAR(MAX)')
, 1
, 1
, ''
)
使用这个:
and OrderProductVariant.DateCreated >= Convert(datetime,[startdate])
and OrderProductVariant.DateCreated < Convert(datetime,[enddate])
删除[NL],因为[NL]被多次使用。
可能 OrderProductVariant.DateCreated 日期格式不同。所以也要更正它的格式。
and cast(OrderProductVariant.DateCreated as date) >= cast([startdate] as date)
最后,您的代码将如下所示:
declare @cols as NVARCHAR(max)
, @cols2 as NVARCHAR(max)
, @query as NVARCHAR(max) select
@cols = STUFF(
(
select
',' + QUOTENAME(ProductOptionName)
from
OrderProductVariantOption
inner join OrderProductVariant on
OrderProductVariant.Id = OrderProductVariantOption.OrderProductVariantId
where
(
OrderProductVariant.AcceptedById = '[Business|0]'
or OrderProductVariant.MRefId = '[Business|0]'
)
and [OrderProductVariant.MarketplaceGroupId=Marketplace]
and cast(OrderProductVariant.DateCreated as date) >= cast([startdate] as date)
and cast(OrderProductVariant.DateCreated as date) < cast([enddate] as date)
group by
ProductOptionName
order by
ProductOptionName for XML path('')
, TYPE
)
. value('.', 'NVARCHAR(MAX)')
, 1
, 1
, ''
)