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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:58:48+00:00 2026-06-17T16:58:48+00:00

I need to compile data from 2 tables with nothing in common. To do

  • 0

I need to compile data from 2 tables with nothing in common.

To do this, I created a view that extracts the bits that I need, which is cross-joined, so I need to strip out the unnecessary rows by filtering on date.

However, I have found a problem with the dates.

Basically, some of the users must have US format set on their systems, and that is how the dates have gone in to the database (m/d/yyyy).

The vast majority of dates are (correctly) in UK format (dd/mm/yyyy)

Obviously, I need all dates to be in the same format, but CONVERT and CAST don’t seem to work.

Therefore, I came up with the idea of using CASE to determine the string length and contents.

Basically, I look for length 10 (e.g. 15/01/2012) and assume that it’s a correct (UK) date, unless characters 4 & 5 are greater than 12.

If it’s 9 characters long, then I need to look for the position of the first ‘/’, which tells me whether it is d/mm or dd/m (year will always be 4 characters).

Lastly, if it’s 8 characters long, I assume it has to be an incorrect (US) date and reverse the numbers, entering zeroes as fillers.

However, when I try to run the script, it gives me:

Msg 102, Level 15, State 1, Line 12
Incorrect syntax near '='.
Msg 156, Level 15, State 1, Line 16
Incorrect syntax near the keyword 'THEN'.
Msg 4145, Level 15, State 1, Line 22
An expression of non-boolean type specified in a context where a condition is expected, near 'ELSE'.
Msg 102, Level 15, State 1, Line 24
Incorrect syntax near '@Day'.
Msg 4145, Level 15, State 1, Line 29
An expression of non-boolean type specified in a context where a condition is expected, near 'END'.
Msg 156, Level 15, State 1, Line 39
Incorrect syntax near the keyword 'IF'.
Msg 102, Level 15, State 1, Line 40
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 41
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 46
Incorrect syntax near ','.
Msg 156, Level 15, State 1, Line 50
Incorrect syntax near the keyword 'THEN'.
Msg 102, Level 15, State 1, Line 51
Incorrect syntax near ','.
Msg 102, Level 15, State 1, Line 52
Incorrect syntax near ','.

The part of the script that is failing is below, and I hope that someone has a suggestion on what I need to do with it (or indeed, a better way to sort out the dates).

What I’m trying to achieve is to populate the [START_DATE] field in the new table (or view) directly from the source table (mbrProject), where the ID’s match (I have already populated the ID’s in the new table by selecting distinct from mbrProject), but also trying to apply the date format change on the fly:

declare @length int,
    @Day nchar(3),
    @Month nchar(3),
    @Year nchar(5)

Update ProjectDates
SET [Start_Date] = 
(CASE
    WHEN len(mbrProject.[Start_Date]) = 8 THEN
        @Day = '0' & SUBSTRING(mbrProject.[START_DATE],3,2), @Month = '0' & SUBSTRING(mbrProject.[START_DATE],1,2), @Year = right(mbrProject.[START_DATE],5)
        @Day + @Month + @Year

    WHEN len(mbrProject.[Start_Date]) = 9 THEN
        IF charindex('/',mbrProject.[start_date]) = 2 THEN
        @Day = SUBSTRING(mbrProject.[START_DATE],3,3), @Month = '0' & SUBSTRING(mbrProject.[START_DATE],1,2), @Year = right(mbrProject.[START_DATE],5)
        ELSE @Day = '0' & SUBSTRING(mbrProject.[START_DATE],4,2), @Month = SUBSTRING(mbrProject.[START_DATE],1,3), @Year = right(mbrProject.[START_DATE],5)
        END IF
        @Day + @Month + @Year

    ELSE
        IF SUBSTRING(mbrProject.[START_DATE],4,2) > 12
            @Day = SUBSTRING(mbrProject.[START_DATE],4,3), @Month = SUBSTRING(mbrProject.[START_DATE],1,3), @Year = right(mbrProject.[START_DATE],5)
            ELSE @Day = SUBSTRING(mbrProject.[START_DATE],1,3), @Month = SUBSTRING(mbrProject.[START_DATE],4,3), @Year = right(mbrProject.[START_DATE],5)
        END IF
        @Day + @Month + @Year

    END)

Where ProjectDates.[START_DATE] = mbrProject.[START_DATE]

=======================================================================

** EDIT **

OK, big thanks to Kaf.
I used the generic script to create a new uodate script, but it is giving me one more problem.

I’ll start by showing the new script:

Update ProjectDates
SET [Start_Date] =

(
select right([Start_Date],4) +  
    case 
        when len([Start_Date])=10 then 
            substring([Start_Date],4,2) + left([Start_Date],2)

        else    right('0' + left([Start_Date],firstIndex-1),2) + 
                right('0' + substring([Start_Date],firstIndex+1,secondIndex - firstIndex-1),2) 
                end

from 
    (
    select mp.[Start_Date], charindex('/',mp.[Start_Date],1) firstIndex,
          charindex('/',mp.[Start_Date],charindex('/',mp.[Start_Date],1)+1) secondIndex

    from mbrProject mp
    join ProjectDates pd
    on mp.ID = pd.Project_ID
    )A

where ProjectDates.Project_ID = mbrProject.ID
)

What happens is that if I run ONLY the SELECT statement
e.g everything from:

select right([Start_Date],4)

down to

on mp.ID = pd.Project_ID
)A

This works.

Unfortunately, although the full script parses correctly, it returns the following error:

Msg 4104, Level 16, State 1, Line 25
The multi-part identifier "mbrProject.ID" could not be bound.

Any ideas please?

  • 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-17T16:58:49+00:00Added an answer on June 17, 2026 at 4:58 pm

    Try this generic solution to get your different formats with different lengths to ISO date format (yyyymmdd). (Assuming if you have 10 characters only when date is dd/mm/yyyy) Once you get the string date to ISO format convert it to a date/datetime type for comparisons.

    SQL Fiddle here

    --create a table for the demo.
    create table T(mydate varchar(50))
    insert into T (mydate, details)
    values ('m/d/yyyy'),('mm/d/yyyy'),('m/dd/yyyy'),('dd/mm/yyyy')
    
    
    select mydate,
           right(mydate,4) +  case when len(mydate)=10 then 
                                   substring(mydate,4,2) + left(mydate,2)
                  else right('0' + left(mydate,firstIndex-1),2) + 
           right('0' + substring(mydate,firstIndex+1,secondIndex - firstIndex-1),2) 
                            end ISO_format
    from (
    select mydate, charindex('/',mydate,1) firstIndex,
                  charindex('/',mydate,charindex('/',mydate,1)+1) secondIndex
    from T ) A
    
    --Results
    MYDATE      ISO_FORMAT
    m/d/yyyy    yyyy0m0d
    mm/d/yyyy   yyyymm0d
    m/dd/yyyy   yyyy0mdd
    dd/mm/yyyy  yyyymmdd
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a VS2010 project which is a windows application that acquires data from
I have the following query, which I designed to compile data from a number
I need to implement a daemon that needs to extract data from a database,
I need to compile the unit with /EHsc option, how could i set this
I need to compile my GWT 1.7 project from my ant build file....anyone know
I need to compile some mfc code that was written using Visual C++ 6.0
I have a program that is written in Ada, and I need to compile
Is there a way to make an app compile data from a website? I
I'm developing a system that operates on (arbitrary) data from databases. The data may
I have the following code that uses Sequence objects to read data from a

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.