select query with criterial in multi column

  • select [ALName] +'--' +[AFName] +'--' +[APhone] +'--' +[***] +'--' +[AAddress] as [Name] ,[***] from UserDataEntry.GeneralInfo where UserDataEntry.GeneralInfo.[Name] like '%abc%' ORDER BY ALName

    Invalid column name.

    and I tried this one too, it error out.

    select [ALName] +'--' +[AFName] +'--' +[APhone] +'--' +[***] +'--' +[AAddress] as [Name] ,[***] from UserDataEntry.GeneralInfo where [ALName]++[AFName] as [Name] like '%abc%' ORDER BY ALName

    Msg 156, Level 15, State 1, Line 1

    Incorrect syntax near the keyword 'as'.

    The first query work in sql 2000.

    I can do this

    select [ALName] +'--' +[AFName] +'--' +[APhone] +'--' +[***] +'--' +[AAddress] as [Name] ,[***] from UserDataEntry.GeneralInfo where [ALName] like '%abc%' or [ALName] like '%abc%' ORDER BY ALName

    it is cumbersome. Thx.

  • Frances

    If you take the second "as [Name]" out of your second query, it should work.

    John

  • Thx.

  • Frances L (10/4/2007)


    select [ALName] +'--' +[AFName] +'--' +[APhone] +'--' +[***] +'--' +[AAddress] as [Name] ,[***] from UserDataEntry.GeneralInfo where UserDataEntry.GeneralInfo.[Name] like '%abc%' ORDER BY ALName

    Invalid column name.

    and I tried this one too, it error out.

    select [ALName] +'--' +[AFName] +'--' +[APhone] +'--' +[***] +'--' +[AAddress] as [Name] ,[***] from UserDataEntry.GeneralInfo where [ALName]++[AFName] as [Name] like '%abc%' ORDER BY ALName

    Msg 156, Level 15, State 1, Line 1

    Incorrect syntax near the keyword 'as'.

    The first query work in sql 2000.

    I can do this

    select [ALName] +'--' +[AFName] +'--' +[APhone] +'--' +[***] +'--' +[AAddress] as [Name] ,[***] from UserDataEntry.GeneralInfo where [ALName] like '%abc%' or [ALName] like '%abc%' ORDER BY ALName

    it is cumbersome. Thx.

    I don't know how the first query worked in SQL Server 2000 since Name is an alias created in the query and you cannot reference aliases in Where or group by clauses.

    Your second query is generating an error because you cannot create an alias using "AS" anywhere but the select list. YOur query should look like:

    select [ALName] +'--' +[AFName] +'--' +[APhone] +'--' +[***] +'--' +[AAddress] as [Name] ,[***] from UserDataEntry.GeneralInfo where [ALName]+[AFName] like '%abc%' ORDER BY ALName

    Notice that all I changed was the "++" to "+" and removed the "AS [Name]" from the where clause.

Viewing 4 posts - 1 through 3 (of 3 total)

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