string ( dd/mm/yyyy) to date sql server

11,633

have you tried using DATETIME instead of VARCHAR(10)

WHERE inputDate > CONVERT(DATETIME, '24/01/2013', 103) 
Share:
11,633
Larry
Author by

Larry

I learnt a lot from SO, I'm trying to give something back. Interested in:C#, Javascript Using: VB(A/Script), SQL

Updated on July 18, 2022

Comments

  • Larry
    Larry almost 2 years

    EDIT: The solution is in the reference post's solution .I was careless to overlook DATETIME--> Varchar(10)

    `Syntax for CONVERT:

    CONVERT ( data_type [ ( length ) ] , expression [ , style ] )`
    

    I am aware of this post

    SQL Server (2005, 2000, 7.0) does not have any flexible, or even non-flexible, way of taking an arbitrarily structured datetime in string format and converting it to the datetime data type.

    So I am looking for a solution that solves this particular String format only.

    Let's say I have a table in sql server with field :inputDate in datetime format

    The following code works without convert/cast

    SELECT inputDate
    FROM   some_table
    WHERE  inputDate > '01/24/2013' 
    

    But it won't work for

    SELECT inputDate
    FROM   some_table
    WHERE  inputDate > '24/01/2013'
    

    Throwing an The conversion of a varchar data type to a datetime data type resulted in an out-of-range value

    Then I tried

    SELECT inputDate
    FROM   some_table
    WHERE  inputDate > CONVERT(VARCHAR(10), '24/01/2013', 103)
    

    Throwing the same error

    Is there a way to convert string in dd/mm/yyyy to be recognize as datetime format in SQL SERVER? Or the only way, and the proper way is doing santization elsewhere?