Join SQL Server tables on a like statement

后端 未结 4 2109
清酒与你
清酒与你 2021-02-19 18:37

I am hoping this isn\'t a repeat. I\'ve checked the searches and I can\'t seem to find a clear answer to this.

I have a table that has it\'s primary key set to be a

4条回答
  •  感动是毒
    2021-02-19 19:20

    Cast StateID to a compatible type, e.g.

    WHERE URL LIKE '%' + CONVERT(varchar(50), StateID) + '%'
    

    or

    WHERE URL LIKE N'%' + CONVERT(nvarchar(50), StateID) + N'%'
    

    if URL is nvarchar(...)

    EDIT

    As pointed out in another answer, this could result in poor performance on large tables. The LIKE combined with a CONVERT will result in a table scan. This may not be a problem for small tables, but you should consider splitting the URL into two columns if performance becomes a problem. One column would contain 'page.aspx?id=' and the other the UNIQUEIDENTIFIER. Your query could then be optimized much more easily.

提交回复
热议问题