+ Reply to Thread
Results 1 to 2 of 2

SELECT Issue

  1. SELECT Issue

    I'm having trouble with a select statement and wondering if some
    experts could help:)

    I have a VARCHAR field in a table called IDFormula which contains an
    INT PRIMARY KEY value from a column in the same table(with brackets
    around it). For example if the table was called Unit, and one of the
    primary key values was 341, then somewhere in the IDFormula column
    there may be a [341] in it as such: [341]+1. I need to select any row
    from the table which has a primary key that can be found in an
    IDFormula column. I hope this is clear. Here is some example code I
    threw together.

    DECLARE @Unit TABLE(
    UnitID INT PRIMARY KEY IDENTITY(1,1),
    IDFormula VARCHAR(5000)
    )

    INSERT INTO @Unit(IDFormula) VALUES(NULL);
    INSERT INTO @Unit(IDFormula) VALUES(NULL);
    INSERT INTO @Unit(IDFormula) VALUES('([1]+3)*2)');

    SELECT * FROM @Unit;

    --This is what I have tried. It never returns anything.
    SELECT * FROM @Unit UN
    WHERE UN.UnitID IN(SELECT U2.UnitID FROM @Unit U2
    WHERE CHARINDEX('[' + LTRIM(RTRIM(UN.UnitID)) + ']',
    U2.IDFormula)>0)



  2. Re: SELECT Issue

    On Dec 4, 10:40*am, Plamen Ratchev wrote:
    > Here is one method:
    >
    > SELECT UnitID, IDFormula
    > FROM @Unit AS U
    > WHERE EXISTS(SELECT *
    > * * * * * * * FROM @Unit AS U2
    > * * * * * * * WHERE U2.IDFormula LIKE '%![' + CAST(U.UnitIDAS VARCHAR(10)) + ']%' ESCAPE '!');
    >
    > --
    > Plamen Ratchevhttp://www.SQLStudio.com


    That works perfectly. Thanks very much!

+ Reply to Thread