Saturday, 17 November 2018

MSSQL - Fix Error - Conversion failed when converting date and/or time from character string.

watch this example on YouTube


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


To fix it replace

Select Convert(date, '07122018')

with

Select Convert(date, Right('07122018', 4) + LEFT('07122018', 2) + SUBSTRING('07122018', 3, 2))

1 comment:

  1. Hello sir
    first of all I hope you are fine
    I'm having Excel sheet with date Cloumn and its Custom saved as xlsx from csv, but in sql its showing just digits I tried all of this to convert it but I couldnt:
    SELECT CAST(date AS DATETIME)
    FROM PortfolioProject..covidDeaths;

    SELECT CONVERT(DATETIME, date)
    FROM PortfolioProject..covidDeaths;

    SELECT location, Convert(date, Right('07122018', 4) + LEFT('07122018', 2) + SUBSTRING('07122018', 3, 2)), total_cases, total_deaths, population
    FROM PortfolioProject..covidDeaths;

    last, one is your method its worked but the problem showing all of rows one date, I made a ton of search

    Thank you

    ReplyDelete