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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T02:26:45+00:00 2026-05-25T02:26:45+00:00

Consider a varchar field ( ShipDate ) that gets date-like strings written to it.

  • 0

Consider a varchar field (ShipDate) that gets date-like strings written to it. These strings come from multiple third-party systems in various formats (over which I, apparently, have no control =/).

I decided to create a view that converts this varchar field to DATE so that I can query it easily (and filter out some other records / fields that I don’t care about).

So far I see two formats coming in: YYYYMMDD (which is fine, I can just a a straight CONVERT) and MM/DD/YYYY, which causes an error:

Conversion failed when converting date and/or time from character string.

This changes my conversion from a simple CONVERT(DATE, ShipDate, 1) to:

CONVERT (DATE,
(CASE 
    WHEN ShipDate LIKE '_/__/____' THEN SUBSTRING(ShipDate, 6, 4) + '0' + SUBSTRING(ShipDate, 1, 1) + SUBSTRING(ShipDate, 3, 2)--M/DD/YYYY
    WHEN ShipDate LIKE '__/_/____' THEN SUBSTRING(ShipDate, 6, 4) + SUBSTRING(ShipDate, 1, 2) + '0' + SUBSTRING(ShipDate, 4, 1)--MM/D/YYYY
    WHEN ShipDate LIKE '_/_/____' THEN SUBSTRING(ShipDate, 5, 4) +  '0' + SUBSTRING(ShipDate, 1, 1) + '0' + SUBSTRING(ShipDate, 3, 1)--M/D/YYYY
    ELSE ShipDate --For the YYYYMMDD dates
END), 1) --End of CONVERT

Is there a better way to do the above SQL statement? I could potentially get even more date-like string formats as time goes on, so the above example could get pretty awful (I tagged this question with regex in case that could reduce the size of the case statement).

Or, is there a way to handle this problem as the records come in, avoiding the view altogether? I’m not too familiar with Triggers / SP’s, but if that’s a good option I’m willing to go that route =)

Or, some other method that is commonly used to solve this problem? Just curious at this point. I’m a .NET programmer, but end up helping out with SQL work because I have some experience, so I’m pretty new to anything even kind of advanced in SQL.

  • 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-25T02:26:46+00:00Added an answer on May 25, 2026 at 2:26 am

    If the known formats are always M then D, and the separators are always /, why not just parse for the slashes? Also, why are you using ,1) in your CONVERT? All of the above formats seemed to convert fine for me without it:

    WITH x(ShipDate) AS
    (
        SELECT '5/12/2011'
        UNION ALL SELECT '05/5/2012'
        UNION ALL SELECT '05/05/2012'
    )
    SELECT CONVERT (DATE, ShipDate) FROM x;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a table that has a varchar field. If I do
I have a table Login(id(int),EmailId(varchar(35),connType(varchar)) where conntype has values like pop3 or imap. consider
Please consider the following: declare @abbrev varchar(20); set @abbrev=''; select pk_term into #t2 from
Consider a table like this: folders_table ----------------------- INT id_folder INT id_folder_parent VARCHAR folder_name Which
Consider the following code. DECLARE @sql VARCHAR(MAX) SELECT @sql = 'SELECT * FROM caseinformation'
Consider a SQL Server table defined with a varchar(1) NULL field. It's being used
I have a varchar column in Postgres 8.3 that holds values like: '0100011101111000' I
Consider the following Transact sql. DECLARE @table TABLE(val VARCHAR(255) NULL) INSERT INTO @table (val)
Consider these two function definitions: void foo() { } void foo(void) { } Is
Consider the Oracle emp table. I'd like to get the employees with the top

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.