30

有可能做这样的事情吗?本质上,我想将 int 转换为字符串并在连接中使用该字符串。注意%t1.id%

select t2.* 
from t1 
join t2 on t2.url='site.com/path/%t1.id%/more' 
where t1.id > 9000
4

5 回答 5

54

如果您有一个名为“col1”的列,它是 int,则将其转换为 String,如下所示:

CONVERT(col1,char)

例如,这允许您检查一个 int 值是否包含另一个值(此处为 9),如下所示:

CONVERT(col1,char) LIKE '%9%'
于 2014-07-13T14:25:52.287 回答
18

您可以使用CONCAT,它的数字参数将转换为其等效的二进制字符串形式。

select t2.* 
from t1 join t2 
on t2.url=CONCAT('site.com/path/%', t1.id, '%/more') where t1.id > 9000
于 2012-10-10T14:19:50.073 回答
3
select t2.* 
from t1 join t2 on t2.url='site.com/path/%' + cast(t1.id as varchar)  + '%/more' 
where t1.id > 9000

使用建议的 concat 会更好

于 2012-10-10T14:20:15.927 回答
2

尝试使用CONCAT

CONCAT('site.com/path/','%', CAST(t1.id AS CHAR(25)), '%','/more')
于 2012-10-10T14:18:06.623 回答
0

你可以这样做:

select t2.*
from t1
join t2 on t2.url = 'site.com/path/' + CAST(t1.id AS VARCHAR(10)) + '/more' 
where t1.id > 9000

注意CAST(t1.id AS VARCHAR(10))

于 2012-10-10T14:17:22.247 回答