Convert varchar dd/mm/yyyy to dd/mm/yyyy datetime

113,286

I think you are after this:

CONVERT(datetime, date_as_string, 103)

Notice, that datetime hasn't any format. You think about its presentation. To get the data of datetime in an appropriate format you can use

CONVERT(varchar, date_as_datetime, 103)
Share:
113,286
Stew
Author by

Stew

Updated on July 09, 2022

Comments

  • Stew
    Stew almost 2 years

    I'm trying to convert a date in a varchar column in the dd/mm/yyyy format into the datetime dd/mm/yyyy format, so then I can run date range queries on the data.

    So far I have the following which is not working

    CONVERT(varchar, CAST(date_started AS datetime), 103)
    

    I have also tried

    convert(date_started as datetime, 103)
    

    I need to make sure the output is as dd/mm/yyyy as we're in the UK and not the mm/dd/yyyy format

  • Stew
    Stew over 10 years
    Didnt work im afraid, it converted but when i added the > '01/06/2013' part to it, i was seeing dates back in 2011
  • DrCopyPaste
    DrCopyPaste over 10 years
    @Stew what do you mean with the "> '01/06/2013'"-part you did not mention anything about that in your OP what did you do with it?
  • Stew
    Stew over 10 years
    Thats what i had before, sorry. my mistake That also converts, but when i do a > '01/06/2013' on the date im seeing dates in january.
  • Stew
    Stew over 10 years
    SELECT date_started FROM staff WHERE (CONVERT(datetime, date_started, 103) > '01/06/2013') When i do this im seeing dates in January, not after 1st June 2013
  • Jazzy J
    Jazzy J over 10 years
    > CONVERT(datetime, '01/06/2013', 103) - then everything is in datetime and the comparison is done on datetime datatypes.
  • senthilkumar2185
    senthilkumar2185 over 10 years
    it is automatically 01 is taking January month so that problem arise you convert 06/01/2013 is working