Share via

Best and most efficient way to convert a VARCHAR(10) to a Date for filtering

Bobby P 271 Reputation points
2026-03-09T16:08:28.7233333+00:00

We have a "Blob" Table with a Date and a Data Type of VARCHAR(10).

What is the best and most efficient way to convert that to a Date so that we can look back and filter based on the last 3-years?

Thanks in advance for your review and am hopeful for a solid solution.

SQL Server | SQL Server Transact-SQL

Answer accepted by question author
  1. Q&A Assist
    2026-03-09T16:08:47.29+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    Use an unambiguous, set-based conversion and avoid changing server/session settings. Assuming the VARCHAR(10) column always contains a valid, consistent date format, the pattern is:

    -- Example: column stores dates as 'YYYYMMDD'
    SELECT  ...
    FROM    dbo.YourBlobTable AS b
    WHERE   CONVERT(date, b.YourVarcharDateColumn, 112) >= DATEADD(YEAR, -3, CAST(GETDATE() AS date));
    

    Key points:

    1. Use an unambiguous style code
      • If the data is YYYYMMDD, use style 112 (ISO, no delimiters) as above.
      • If the data is YYYY-MM-DD, date/datetime2 will interpret it correctly without a style for modern types, but using a style or normalizing to YYYYMMDD is safer and clearer.
      • Avoid ambiguous formats like MM/DD/YYYY or DD/MM/YYYY for storage and conversion.
    2. Filter on the converted date
      • Convert the VARCHAR to date (or datetime2) in the predicate and compare to a date boundary:
    WHERE CONVERT(date, b.YourVarcharDateColumn, 112)
          >= DATEADD(YEAR, -3, CAST(GETDATE() AS date));
    
    1. For performance and indexing
      • If this table is large and the column is indexed as VARCHAR, converting in the WHERE clause prevents index seek usage.
      • For best performance, add a persisted computed column of type date and index that:
    ALTER TABLE dbo.YourBlobTable
    ADD YourDateColumn AS CONVERT(date, YourVarcharDateColumn, 112) PERSISTED;
    
    CREATE INDEX IX_YourBlobTable_YourDateColumn
    ON dbo.YourBlobTable(YourDateColumn);
    
    SELECT ...
    FROM   dbo.YourBlobTable
    WHERE  YourDateColumn >= DATEADD(YEAR, -3, CAST(GETDATE() AS date));
    
    1. Long-term recommendation
      • Store dates in a proper date/datetime data type going forward (e.g., date or datetime2) and only format as text in the presentation layer. This avoids reliance on DATEFORMAT or language settings and uses the unambiguous YYYYMMDD/YYYY-MM-DD formats internally.

    References:

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.