简体   繁体   English

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

[英]Conversion failed when converting date and/or time from character string

I'm getting this error: Conversion failed when converting date and/or time from character string when I try to run the following query in SQL server 2008 R2:我收到此错误:当我尝试在 SQL 服务器 2008 R2 中运行以下查询Conversion failed when converting date and/or time from character string

    DECLARE @Time datetime = N'7/13/2011'
    DECLARE @str as varchar(100) = '2011_07_13_DM_VT_I2_Data'
    DECLARE @TestTime datetime =  cast(replace (left (left(@str, len(@str) - len('_data')), 10), '_', '') as datetime)
    DECLARE @r int = datediff(d, @TestTime, @Time)

    SELECT t.name FROM
    (
    SELECT 
         name
    FROM 
        sysobjects 
    WHERE 
        (type = 'U') AND ((name LIKE '%[_]I2[_]Data') or (name LIKE '%[_]R4[_]Data') or (name LIKE '%[_]R8[_]Data'))
    ) t

    WHERE
        datediff(DAY, cast(replace (left (left(t.name, len(t.name) - len('_data')), 10), '_', '') as datetime), @Time) <= 1

    ORDER BY 
        cast(replace (left (left(t.name, len(t.name) - len('_data')), 10), '_', '') as datetime) desc

This query runs fine if I comment out WHERE datediff(DAY, cast(replace (left (left(t.name, len(t.name) - len('_data')), 10), '_', '') as datetime), @Time) <= 1 , then I wonder if it is the problem of the cast function.如果我注释掉WHERE datediff(DAY, cast(replace (left (left(t.name, len(t.name) - len('_data')), 10), '_', '') as datetime), @Time) <= 1这个查询运行良好WHERE datediff(DAY, cast(replace (left (left(t.name, len(t.name) - len('_data')), 10), '_', '') as datetime), @Time) <= 1 ,那么我想知道这是否是cast function 的问题。 But the t table will have the table names like '2011_07_13_DM_VT_I2_Data' you can see in the @TestTime the cast function works fine with this format.但是t表将具有像'2011_07_13_DM_VT_I2_Data'这样的表名,您可以在@TestTime中看到强制cast function 可以正常使用这种格式。 I don't know what is the wrong with the WHERE condition.我不知道WHERE条件有什么问题。

Thank you for any help.感谢您的任何帮助。

You need to split your query into two because it looks like the query optimizer has decided to apply the where clause from the outer query before it has filtered down the rows from sysobjects in the sub query.您需要将查询分成两部分,因为看起来查询优化器已决定应用外部查询中的 where 子句,然后才从子查询中的 sysobjects 中过滤掉行。

You could try something like this.你可以试试这样的。

DECLARE @Time datetime = N'7/13/2011'
DECLARE @str as varchar(100) = '2011_07_13_DM_VT_I2_Data'
DECLARE @TestTime datetime =  cast(replace (left (left(@str, len(@str) - len('_data')), 10), '_', '') as datetime)
DECLARE @r int = datediff(d, @TestTime, @Time)

SELECT name INTO #TMP
FROM 
    sysobjects 
WHERE 
    (type = 'U') AND ((name LIKE '%[_]I2[_]Data') or (name LIKE '%[_]R4[_]Data') or (name LIKE '%[_]R8[_]Data'))

SELECT *
FROM #TMP as t
WHERE
    datediff(DAY, cast(replace (left (left(t.name, len(t.name) - len('_data')), 10), '_', '') as datetime), @Time) <= 1

ORDER BY 
    cast(replace (left (left(t.name, len(t.name) - len('_data')), 10), '_', '') as datetime) desc

DROP TABLE #TMP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从字符串转换日期和/或时间时转换失败? - Conversion failed when converting date and / or time from character string? 约会时间! 从字符串转换日期和/或时间时转换失败 - Datetime! Conversion failed when converting date and/or time from character string 从字符串转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string 从字符串SQL转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string SQL 从字符串转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string 从字符串转换日期和/或时间时,获取转换失败 - Getting Conversion failed when converting date and/or time from character string 错误“从字符串转换日期和/或时间时转换失败” - Error “Conversion failed when converting date and/or time from character string” 从字符串转换日期和/或时间时,DateTime转换失败 - DateTime conversion failed when converting date and/or time from character string 从字符串转换日期和/或时间时转换失败? - Conversion failed when converting date and/or time from character string? 从字符串转换日期和/或时间时转换失败 - - Conversion failed when converting date and/or time from character string -
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM