format datetime to varchar

  • Hello,
    I would like to convert a datetime column with values such as '2017-09-19 08:01:55.780' to  varchar like '2017-09-19T08:01:55.780'

    So I try the following sql but I get an error
    select FORMAT('2017-09-19 08:01:55.780','yyyy-MM-ddThh:mm:ss.fff')

    Any thoughts please?

    Thank you

  • The error message you get tells you exactly what the problem is

    Argument data type varchar is invalid for argument 1 of format function.

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • I would also avoid using FORMAT as well. CONVERT and the style code 126 returns the display format you are after.

    If, however, you are storing your datetimes as varchar, then don't use FORMAT, use REPLACE (and replace the space with a 'T'); or better yet don't store them as varchars. 😉

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply