If i am not wrong, you are using SQL Server. In your query the below part is wrong
SUBSTRING(rec,1,6) as T2.DSNumber,
should be
SUBSTRING(rec,1,6) as DSNumber,
SUBSTRING(rec,175,2) as DSRaceCode1,
SUBSTRING(rec,251,12) as DSRaceText1
Question
I am tryng to compare race codes in two different tables joining on a number id in each table, one table is in fixed positional format. I am getting an error on the '.' between T2.DSNumber on line 5
select
T1.SNumber,
T1.Racecode1,
T1.Race1 ,
SUBSTRING(rec,1,6) as T2.DSNumber,
SUBSTRING(rec,175,2) as T2.DSRaceCode1,
SUBSTRING(rec,251,12) as T2.DSRaceText1
from scerts T1
INNER JOIN DS2012 T2
on T1.SNumber = T2.DSNumber
where right(T1.SNumber,6) = T2.DSNumber and T1.Racecode1 <>T2.DSRaceCode1
and T1.FLAG = 'o'
and year(cast(T1.DDate as date)) ='2012'
order by T1.SNumber
Solution
If i am not wrong, you are using SQL Server. In your query the below part is wrong
SUBSTRING(rec,1,6) as T2.DSNumber,
should be
SUBSTRING(rec,1,6) as DSNumber,
SUBSTRING(rec,175,2) as DSRaceCode1,
SUBSTRING(rec,251,12) as DSRaceText1
OTHER TIPS
Others apparently are close with different answers, but your issue is that your second table (T2) does not actually have columns for "DSNumber" and "DSRaceCode1" as you are pulling those from the substring components. Therefore, those substring references need to be applied to your join (or WHERE). I have formatted to JOIN condition SQL format. You can't give an alias.column as a column name as you attempted to do, but enough info to provide what SHOULD be proper syntax for you to move forward.
Also, for future, and hopefully can apply to what you have via table structure changes, use id-based columns for joins and not getting into "merged" fields into a single will kill your performance, querying and support down-stream.
select
T1.SNumber,
T1.Racecode1,
T1.Race1,
SUBSTRING(T2.rec,1,6) as DSNumber,
SUBSTRING(T2.rec,175,2) as DSRaceCode1,
SUBSTRING(T2.rec,251,12) as DSRaceText1
from
scerts T1
INNER JOIN DS2012 T2
on right(T1.SNumber,6) = SUBSTRING(T2.rec,1,6)
AND T1.Racecode1 <> SUBSTRING(T2.rec,175,2)
where
T1.FLAG = 'o'
and year(cast(T1.DDate as date))
Also.. seeing sample data from each respective table would definitely have helped the many others that tried to answer your question :)
You probably should just remove the T2.
from the alias you are giving the column. If you decide you need it, then you need to escape it with quotation marks or brackets:
select field as [test.test]
select field as 'test.test'
After
as
you should define Alias Name for Fields not field itselft Your query might be some thing like this
I think your table are structure as
T1 = SNNumber, RACECODE1 and RACE1
T2 = REC
if this this true then you could used
SELECT
SUBSTRING(T2.rec,1,6) as DSNumber,
SUBSTRING(T2.rec,175,2) as DSRaceCode1,
SUBSTRING(T2.rec,251,12) as DSRaceText1
FROM scerts T1
INNER JOIN DS2012 T2 ON T1.SNNumber = RIGHT(T2.rec,6)
WHERE T1.racecode1 <> SUBSTRING(T2.rec,175,2)
AND T1.FLAG = 'o'
AND year(cast(T1.DDate as date)) ='2012'
ORDER BY T1.SNumber
Hope this work
I created the temp table for the fixed position data then did the join. This time it worked. I guess I cant pull data from fixed position and insert in columns in same select as Join statement.
Select
SUBSTRING(rec,1,6) as DSNumber,
SUBSTRING(rec,175,2) as DSRaceCode1,
SUBSTRING(rec,251,12) as DSRaceText1
into #tDSdata
from DS2012
select
T1.SNumber,
T1.RACECODE1,
T1.RACE1 ,
T2.DSNumber,
T2.DSRaceCode1,
T2.DSRaceText1
from scerts T1
INNER JOIN #tDSdata T2
on right(T1.SNumber,6) = T2.DSNumber
WHERE t1.racecode1 <> T2.DSRaceCode1
and T1.FLAG = 'o'
and year(cast(T1.DDate as date)) ='2012'
order by T1.SNumber