Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3320988
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:00:32+00:00 2026-05-17T23:00:32+00:00

I’m running into an issue casting a VARCHAR in one table into a DATETIME

  • 0

I’m running into an issue casting a VARCHAR in one table into a DATETIME and then using it in a comparison. The format (‘2010-09-02 10:38:31.000’) seems to be correct on all of the records, and I can select the data without any problems. However, if I try to use the new datetime in the WHERE clause, I get the error: Conversion failed when converting date and/or time from character string.

So just to be clear, this works:

SELECT Field1, Field2, CAST(Field3 AS DATETIME) AS DateTaken FROM MyTable;

That returns all of the data as expected, and there are no null dates. The following fails with the previously mentioned error.

SELECT Field1, Field2 FROM MyTable WHERE CAST(Field3 AS DATETIME) > @startDate;

I tried using a view to retrieve the data, but it had the same error. Next I tried creating a temporary table for the data like so…

SELECT Field1, Field2, CAST(Field3 AS DATETIME) AS Field3 INTO _MyTable;
SELECT Field1, Field2 FROM _MyTable WHERE Field3 > @startDate;

Which works successfully.

All of my searching just leads to incorrectly formatted dates and / or regional settings causing havok which does not appear to be the cause in my case. I’d rather not create a table every time I need to use a DATETIME cast, so what am I doing wrong here? Help is much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:00:33+00:00Added an answer on May 17, 2026 at 11:00 pm

    edit # 3…………..

      select * from test 
      where case isdate(field3) when 1 
            then CAST(field3 as datetime) else null end > @startdate
    

    edit # 2…………..

    I did a test on my db…

    declare @startdate datetime
    
    select @startdate = '09/1/2010'
    
    Create table test (field1 varchar(50), field2 varchar(50), field3 varchar(50))
    
    
    insert into test
    select 'test' as field1, 'test' as field2, '2010-09-02 10:38:31.000' as field3
    
    insert into test
    select 'test2' as field1, 'test2' as field2, '2010-09-02 10:38:31.000' as field3
    
    insert into test
    select 'test3' as field1, 'test3' as field2, '2010-10-02 10:38:31.000' as field3
    
    select * from test where CAST(field3 as datetime) > @startdate
    
    select * from test where Convert(datetime, field3) > @startdate
    
    drop table test
    

    Results:

    test    test    2010-09-02 10:38:31.000
    test2   test2   2010-09-02 10:38:31.000
    test3   test3   2010-10-02 10:38:31.000
    
    
    test    test    2010-09-02 10:38:31.000
    test2   test2   2010-09-02 10:38:31.000
    test3   test3   2010-10-02 10:38:31.000
    

    changing the startdate param to 10/1/2010
    Results:

    test3   test3   2010-10-02 10:38:31.000
    
    test3   test3   2010-10-02 10:38:31.000
    

    1st response……………..
    Did you try Convert?

    SELECT Field1, Field2 FROM MyTable WHERE CONVERT(DateTime, Field3) > @startdate;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.