简体   繁体   English

转换输入的时间时从字符串转换日期和/或时间时转换失败| SQL,SQLSRV

[英]Conversion failed when converting date and/or time from character string when converting inputted time | SQL, SQLSRV

I have a problem with this query. 我对此查询有疑问。 I'm receiving the following error below. 我在下面收到以下错误。

What I'm trying to do is to convert the 10:00 PM to 22:00, basically from 12 hour format to 24 hour format. 我正在尝试将10:00 PM转换为22:00,基本上从12小时格式转换为24小时格式。

Msg 241, Level 16, State 1, Line 2 Conversion failed when converting date and/or time from character string. 消息241,级别16,状态1,行2从字符串转换日期和/或时间时转换失败。

The value of the schedulename column is schedulename列的值为

10:00 PM - 06:00 AM 下午10:00-上午06:00

10:00 PM - 06:00 AM 下午10:00-上午06:00

and one row called REST 一行称为REST

I set it to LEFT(schedulename,8) so that I can get the schedule on the left, and then RIGHT(schedulename,8) so that I can get the schedule on the right. 我将其设置为LEFT(schedulename,8)这样我可以在左侧获取时间表,然后将其设置为RIGHT(schedulename,8)这样我就可以在右侧获取时间表。

I'm using SQLSRV, SQLSERVER 2012, and XAMPP. 我正在使用SQLSRV,SQLSERVER 2012和XAMPP。

SELECT 
Format(cast(LEFT(schedulename,8) as datetime),'HH:mm:ss') AS login,
Format(cast(RIGHT(schedulename,8) as datetime),'HH:mm:ss') AS logout 
FROM
employeesschedulelist 
WHERE 
employeeidno='D0150000005'

I also tried this, but no luck. 我也尝试过,但是没有运气。

SELECT 
CONVERT(VARCHAR, LEFT(schedulename,8), 108) as login,
 CONVERT(VARCHAR, RIGHT(schedulename,8), 108) as logout 
from employeesschedulelist 
where employeeidno='D0150000005'

Is there another solution to this without changing my column? 在不更改我的专栏的情况下,还有其他解决方案吗?

You can use TRY_CAST() instead of CAST() . 您可以使用TRY_CAST()而不是CAST() If the conversion fails, it will return NULL . 如果转换失败,它将返回NULL

SELECT FORMAT(TRY_CAST(LEFT(schedulename ,8) AS DATETIME),'HH:mm:ss') AS login,
       FORMAT(TRY_CAST(RIGHT(schedulename, 8) AS DATETIME),'HH:mm:ss') AS logout 
FROM employeesschedulelist 
WHERE employeeidno = 'D0150000005'

Try with this. 试试这个。

SELECT 
CONVERT(TIME,(LEFT(schedulename,8))) AS login,
CONVERT(TIME,(RIGHT(schedulename,8))) AS logout 
FROM employeesschedulelist 
WHERE employeeidno='D0150000005'

暂无
暂无

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

相关问题 无法更正我的代码:从字符串转换日期和/或时间时转换失败 - Failed to correct my codes: Conversion failed when converting date and/or time from character string 从字符串错误转换日期和/或时间时,MSSQL Server 2008转换失败 - MSSQL Server 2008 Conversion failed when converting date and/or time from character string Error 如何修复“未捕获的 PDOException:...从字符串转换日期和/或时间时转换失败”? - How to fix "Uncaught PDOException: ...Conversion failed when converting date and/or time from character string"? 从字符串转换日期和/或时间时转换失败 - 但不明白为什么 - Conversion failed when converting date and/or time from character string - but no idea why 使用 PHP 和 PDO 与 MS SQL 从字符串转换日期时间时转换失败 - Conversion failed when converting datetime from character string using PHP and PDO with MS SQL 用Php和SQL Server将日期时间转换为字符串时出现问题 - Problem when converting a date time to string with Php and SQL Server 从字符串转换日期和/或时间时,PHP mssql_query错误 - PHP mssql_query errors when converting date and/or time from character string 将字符串转换为时间和日期 - Converting string into time and date 将带有日期和时间的php字符串转换为sql时间戳 - converting php string with date and time to sql timestamp 将时间转换为字符串时秒丢失 - Second being lost when converting time to string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM