Search code examples
sqlsql-server

How To CAST substring as a DATE


I have the following string:

2024-07-01T00:00:00.000+00:00

I wish to convert it to MM/dd/yyyy

I have tried the following SQL:

CAST (SUBSTRING(ESTIMATED_DISPATCH,1,4) + '/' + SUBSTRING(ESTIMATED_DISPATCH,6,2) + '/' + SUBSTRING(ESTIMATED_DISPATCH,9,2) AS date) AS estimated_dispatch

I am getting the error:

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

Can someone guide me on the correct way to convert this to a date


Solution

  •   DECLARE @InString VARCHAR(30)='2024-07-01T00:00:00.000+00:00';
      SELECT CONVERT(CHAR(10),CAST(@InString AS DATE),101);
    

    Please try this