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 9166389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:08:33+00:00 2026-06-17T15:08:33+00:00

I am looking for what I think to be a very useful information regarding

  • 0

I am looking for what I think to be a very useful information regarding SQL Server Management Studio.

I have a table with a column of type varchar that stores dates, numbers, and strings.

Those dates are stored with the following format:

dd/mm/aaaa

I have a query that searchs form matching rows and one requirement is that the user must be able to seacrh between dates (period of time).

There is no mistery if I had just dates, I could use a query:

where convert(datetime,a.valor,103) between '01/01/2013' and '03/01/2013'

The problem is that this query fails when reach a row that the value is not a date.

What would be a efficient way to perform that query since there could be thousand of rows to search?

  • 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-06-17T15:08:35+00:00Added an answer on June 17, 2026 at 3:08 pm

    The typical answer is to add a WHERE clause:

    WHERE ISDATE(a.valor) = 1
    

    However this is problematic in your situation for a couple of reasons:

    1. ISDATE() won’t necessarily match the way you want depending on regional settings of the server, the user’s language or dateformat options, etc. For example:

      SET DATEFORMAT dmy;
      SELECT ISDATE('13/01/2012'); -- 1
      
      SET DATEFORMAT mdy;
      SELECT ISDATE('13/01/2012'); -- 0
      
    2. You can’t really control that SQL Server will try and perform the CONVERT after the filter.

    You can’t even use subqueries or CTEs to try and separate the filter from the CONVERT because SQL Server can optimize the operations in the query in whatever order it deems more efficient.

    For example, with a limited sample, you will probably find that this works okay:

    SET DATEFORMAT dmy;
    
    SELECT valor, valor_date FROM (
      SELECT valor, valor_date = CONVERT(DATE, 
        CASE WHEN ISDATE(valor) = 1 THEN valor ELSE NULL END, 103)
      FROM dbo.mytable
      WHERE ISDATE(valor) = 1
    ) AS sub WHERE valor_date BETWEEN '01/01/2012' AND '01/03/2012';
    

    But I have seen cases with even this construct where SQL Server has tried to evaluate the filter first, leading to the same error you’re currently getting.


    A couple of safer workarounds:


    Add a computed column, e.g.

    ALTER TABLE dbo.mytable ADD valor_date
      AS CONVERT(DATE, CASE WHEN ISDATE(valor) = 1 THEN valor 
        ELSE NULL END, 103);
    

    To protect yourself from possible misinterpretations at runtime, you should specify dateformat before issuing a query that references the computed column, e.g.

    SET DATEFORMAT dmy;
    SELECT valor, valor_date FROM dbo.mytable WHERE ...;
    

    Create a view:

    CREATE VIEW dbo.myview
    AS
      SELECT valor, valor_date = CONVERT(DATE, 
        CASE WHEN ISDATE(valor) = 1 THEN valor ELSE NULL END, 103)
      FROM dbo.mytable
      WHERE ISDATE(valor) = 1;
    

    Again, you’ll want to issue a SET DATEFORMAT when querying the view.


    Use a temp table:

    SELECT <cols>
    INTO #foo
    FROM dbo.mytable
    WHERE ISDATE(valor) = 1;
    
    SELECT <cols>, CONVERT(DATE, valor) FROM #foo WHERE ...;
    

    You may still want to use DATEFORMAT to protect yourself from conflicts between ISDATE and user settings.


    And no, you should not try to validate your strings as dates using string pattern matching as was suggested in another (now deleted) answer:

    like '%__/%' or like '%/%'
    

    You will have to have some pretty complex and heavy-handed validation there to handle all valid dates including leap years.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking to create a read-only table, and MYSQL permissions, I think, are the
Really quick here... I think I have the answer, but just looking for some
I think what I'm looking for is something very simple, yet I am unable
I think that looking at others' code is a good way to learn. I'm
I think this is easily explained by looking at code, so I'm posting a
Looking to do a security audit of all user permissions. I think I can
I think the title is self explanatory. What I'm looking for is material so
I think everything is in the question. I'm looking for the Lazyboy equivalent for
After looking at several questions/answers here, I'm not seeing what I think I need.
All, I THINK that I'm looking for a function for Trilinear interpolation. Here's the

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.