• TJT (5/15/2013)


    I am trying to create a variable like the following in a WHERE clause

    DECLARE @test-2 AS VARCHAR(90)

    SET @test-2 = '2003 OR Cars.year = 2004 OR Cars.year = 2004'

    SELECT Cars.year

    FROM CARS

    WHERE Cars.year = @test-2

    Is this possible to set a variable like this? I need to escape the "OR Cars.year =" part it seems

    No, it's invalid T-SQL construction.

    You should use the following:

    SELECT Cars.year

    FROM CARS

    WHERE Cars.year IN (2003,2004,2005)

    or you can build the whole statement as dynamic SQL and execute it, however I don't think that is really applicable for your case.

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]