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

pjustindaryll

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.

Msg 241, Level 16, State 1, Line 2 Conversion failed when converting date and/or time from character string.

The value of the schedulename column is

10:00 PM - 06:00 AM

10:00 PM - 06:00 AM

and one row called 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.

I'm using SQLSRV, SQLSERVER 2012, and 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?

Qirel

You can use TRY_CAST() instead of CAST(). If the conversion fails, it will return 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'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

SQL - Conversion failed when converting date and/or time from character string

SQL Error "Conversion failed when converting date and/or time from character string" whereas not converting

conversion failed when converting date and/or time from character string in xml

Datetime! Conversion failed when converting date and/or time from character string

Conversion failed when converting date and / or time from character string?

Error "Conversion failed when converting date and/or time from character string"

tsql - Conversion failed when converting date and/or time from character string

Conversion failed when converting date and/or time from character string - why?

Conversion failed when converting date and/or time from character string?

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 with DateDiff

Problem: Conversion failed when converting date and/or time from character string

"Conversion failed when converting date and/or time from character string"

SQL Server: Conversion failed when converting time from character string

SQL Server: passing string from variable into date - Conversion failed when converting date and/or time from character string

SQL Server Conversion failed when converting from date and/or time from character string when query is parameterized

Conversion failed when converting date and/or time from character string. when filtering sql select query

T SQL Conversion failed when converting date and/or time from character string from VARCHAR(MAX)

Conversion failed when converting date and/or time from character string while inserting datetime SQL

datetime conversion failed when converting date or time from character string SQL (c#)

How to solve "Conversion failed when converting date and/or time from character string" in my case in SQL Server

Conversion failed when converting date and/or time from character string in SQL SERVER 2008

SQL error, Conversion failed when converting date and/or time from character string

SQL Server : conversion failed when converting date and/or time from character with string past 12pm

SQL Server: Conversion failed when converting date and/or time from character string

SQL: How to resolve "Conversion failed when converting date and/or time from character string"?

Conversion failed when converting date and/or time from character string - SQL Server error

Sql Server 2008: Conversion failed when converting date and/or time from character string