Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Need help - Error - The conversion of a char data type to a datetime data type

  • 24-04-2009 3:12pm
    #1
    Registered Users, Registered Users 2 Posts: 224 ✭✭


    Hi,

    Im trying to convert a varchar to a datetime and am getting the following error.

    The conversion of a char data type to a datetime data type resulted in an out-of-range datetime value.

    One of the problems is that the data in the fields are in the following formats

    Dec 31 2008 12:00AM
    02/17/2009
    20/02/2009
    <null>

    I need to somehow convert all these to
    20/02/2009 format

    Im using MSSQL 2000 btw

    Any help will be most appreciated.

    Thanks


Comments

  • Registered Users, Registered Users 2 Posts: 2,931 ✭✭✭Ginger


    You can use an ISNULL statement to get you out of the whole NULL issue.

    Your best bet it is run it through an ASPX file and adjust the logic, otherwse run a stored proc with a cursor and see what happens


  • Registered Users, Registered Users 2 Posts: 1,916 ✭✭✭ronivek


    You can also use isdate() to check if a varchar is suitable for conversion in the first place.


  • Moderators, Computer Games Moderators Posts: 10,669 Mod ✭✭✭✭Andrew76


    Hi there,

    To add to the other replies, the following code converts your sample values to DATETIME:
    DECLARE @Str VARCHAR(100)
    -- Comment/uncomment to test diff values.
    SET @Str = 'Dec 31 2008 12:00AM'
    --SET @Str = '02/17/2009'
    --SET @Str = '20/02/2009'
    --SET @Str = NULL
    
    SELECT @Str [Var], 
    CASE WHEN ISDATE(@Str) = 1 THEN CAST(@Str AS DATETIME)
    ELSE CONVERT(DATETIME,@Str,103) 
    END [Date]
    

    I needed the convert 103 for the 20/02/2009 value (probably due to my sql language settings). Hope this is of some help.

    Andrew.


Advertisement