Help with an Inner join query

  • Please help me out with one of my queries using an inner join statement.

    What I am trying to achieve is to pull out a column from one of my tables based on the following criterias:

    1.)The column value for a field in

    2.) The record should not have been processed before, which is identified by the column flag in

    3.)The record should be created in 2008

    I tried to establish the same by writing the following query:

    select t1.id1

    from table1 t1 inner join table2 t2

    on t1.id1 = cast(t2.id2 as varchar)

    where t2.flag = 'false'

    and datepart(yy,t2.timestampvalue) = '2008'

    But my result also returned records which were created in 2006.

    How do I eliminate that my recordset.

  • Using some sample data, I haven't been able to re-create your problem. Here's my test:

    create table #T1 (

    ID int identity primary key);

    create table #T2 (

    ID int identity primary key,

    T1ID int,

    Created datetime);

    insert into #T1

    default values;

    insert into #T2 (T1ID, created)

    select 1, getdate() union all

    select 1, '1/1/2006' union all

    select 1, '1/1/2008'

    select *

    from #T1

    inner join #T2

    on #T1.ID = #T2.T1ID

    where datepart(yy, created) = '2008'

    The select gets the record from 2008 every time, and does not get any of the other dates.

    Is it possible there is more to your query? Or a typo in it?

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply