从字符串转换日期和/或时间时转换失败-为什么?

尚塔努·迪维迪

这是一个SQL子查询,给出的错误为

“从字符串转换日期和/或时间时转换失败。”

我尝试用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
    , ''
  )
阿卡什·辛格
  1. 删除[NL],因为[NL]已多次使用。

  2. 也许OrderProductVariant.DateCreated日期格式不一样。因此也请纠正其格式。

    并强制转换(OrderProductVariant.DateCreated作为日期)> =强制转换([startdate]作为日期)

最后,您的代码将如下所示:

 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
    , ''
  )

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从托管实例中的字符串转换日期和/或时间时转换失败-为什么?

从字符串转换日期和/或时间时转换失败-但不知道为什么

为什么我的带时区的ISO日期时间字符串在POST时转换时间?

为什么字符串不能转换为日期时间?

从字符串转换日期和/或时间时转换失败?

从字符串转换日期和/或时间时转换失败?

从字符串转换日期和/或时间时转换失败?

UIColor转换为字符串失败,为什么?

在Swift中,为什么尝试从字符串转换为日期时出现错误?

为什么numpy在字符串转换时截断数字?

插入日期时间 SQL 时从字符串转换日期和/或时间时转换失败

为什么字符串不会使用强制转换或转换更改为日期?

从字符串转换日期和/或时间时,转换小时数转换失败

无法将 Varchar 转换为 DateTime - 从字符串转换日期和/或时间时转换失败

在对字符串进行操作时,为什么将字符串转换为charArray?

约会时间!从字符串转换日期和/或时间时转换失败

从字符串转换日期和/或时间时,两个日期比较使转换失败

为什么从工作线程将日期对象作为消息发送时将其转换为字符串?

为什么在 Swift 中将日期从字符串对象转换为日期对象始终为零

参数化查询时从字符串的日期和/或时间转换时,SQL Server转换失败

插入datetime时从字符串转换日期和/或时间时转换失败

从字符串转换日期和/或时间时转换失败。过滤SQL选择查询时

从字符串转换日期和/或时间时转换失败使用参数化查询时

为什么用字符串和timedelta转换DataFrame会转换dtype?

从字符串转换数据和/或时间时转换失败

从 VARCHAR(MAX) 字符串转换日期和/或时间时,T SQL 转换失败

如何避免错误消息241(从字符串转换日期和/或时间时转换失败)

从字符串转换日期和/或时间时转换失败(返回月份的最后一天)

在数据透视中从字符串转换日期和/或时间时转换失败