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?
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