在SQL字符串中转换&符号 [英] Escaping ampersand character in SQL string

查看:262
本文介绍了在SQL字符串中转换&符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过我的sql数据库中的名称来查询一行,它有一个&符号。我试图设置一个逃脱角色,然后逃离和符,但由于某种原因,这是不工作,我不确定我的问题是什么。

I am trying to query a certain row by name in my sql database and it has an ampersand. I tried to set an escape character and then escape the ampersand, but for some reason this isn't working and I'm uncertain as to what exactly my problem is.

Set escape '\'
    select * from V1144engine.T_nodes where node_id in(
    select node2_id from V1144engine.T_edges where node1_id in(
    select node2_id from V1144engine.T_edges where node1_id in(
    select node2_id from V1144engine.T_edges where node1_id = 
      (select node_id from V1144engine.T_nodes where node_name = 'Geometric Vectors \& Matrices')))
    and edge_type_id = 1)
    and node_type_id = 1
    and node_id in (
    select node2_id from V1144engine.T_edges where node1_id =
      (select node_id from V1144engine.T_nodes where node_name = 'Algebra II')
    and edge_type_id = 2);

虽然这有一个类似的解决方案这个问题,问题的方式差别很大。他们可能会得到相同的解决方案,但这并不意味着问题是一样的。

Although this has a similar solution to this question, the problems are posed very differently. They may end up having the same solution, but that does not mean that the questions are the same.

推荐答案

p>

Instead of

node_name = 'Geometric Vectors \& Matrices'

使用

node_name = 'Geometric Vectors ' || chr(38) || ' Matrices' 

38是和号的ascii代码,在这种形式下,它将被解释为字符串,没有别的。我试过它,它的工作。

38 is the ascii code for ampersand, and in this form it will be interpreted as a string, nothing else. I tried it and it worked.

另一种方式可能是使用LIKE和下划线代替'&'字符:

Another way could be using LIKE and an underline instead the '&' character:

node_name LIKE 'Geometric Vectors _ Matrices' 

你会发现的机会一些其他的记录,这只是这个一个字符不同,是相当低的。

The chance that you'll find some other record too, which is different in only this one character, is quite low.

这篇关于在SQL字符串中转换&符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆