需要一种使用SQL查询在数据中查找特殊字符的方法

时间:2017-05-02 17:23:17

标签: sql sql-server regex data-cleaning

我试图在我的任何字段中找到不在a-zA-Z0-9范围内的特殊字符。但是,如果我尝试这个查询:

select Name from table where Name like '%[?]%'

我得到两条记录:

  • ???? ?????
  • 固定?????

这就是我想要的。但是,由于我不知道特殊字符是什么,我需要使用排除混合字符的数据:

select Name from table where Name NOT like '%[a-zA-Z0-9]%'

由于这排除了所有带有a-zA-Z0-9的记录,我只得到:

  • ???? ?????

但我还需要得到'固定?????'结果。我需要将具有特殊字符的数据合并到其中。

我有点不知道如何做到这一点。我已经看到这是用shell脚本或'vi'(LIST)完成的,但在SQL中并不那么容易。

有没有人解决这个问题?

3 个答案:

答案 0 :(得分:0)

LIKE'%[^ 0-9a-zA-Z]%'

数字(0-9),小写字母alphas(a-z),大写字母alphas(A-Z)。 “^”使其成为“NOT”中的一个

答案 1 :(得分:0)

试试这段代码:

select Name from table where Name like '%[^0-9a-zA-Z ]%'

答案 2 :(得分:0)

感谢您的回复。我曾尝试过你的建议,但我仍然得到了更多的结果。但是,看起来您可以通过排除获得非常具体的信息。最终我最终添加了我得到的数据的结果。

像这样:

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
		<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<div class="container">

</div>

<button class="btn btn-info" id="createData">Create divs</button>
<button type="button" class="btn btn-info" data-toggle="modal" data-target="#addEmpForm">Add Employee</button>
<!-- Modal -->

<div class="modal fade" id="addEmpForm" role="dialog">
  <div class="modal-dialog">
  <div class="modal-content">
    <!-- Modal Header -->
    <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">
      <span aria-hidden="true">&times;</span>
      <span class="sr-only">Close</span>
    </button>
    <h4 class="modal-title" id="myModalLabel">Add Employee</h4>
    </div>
    <form id="updateForm">
    <!-- Modal Body -->
    <div class="modal-body">
      <div class="form-group">
      <label class="col-sm-4 control-label" for="empName">Employee Name</label>
      <div class="col-sm-8">
        <input type="text" class="form-control" id="empName" required/>
      </div>
      </div>
      <div class="form-group">
      <label class="col-sm-4 control-label" for="empProfile">Profile</label>
      <div class="col-sm-8">
        <input type="url" class="form-control" id="empProfile" required/>
      </div>
      </div>
      <div class="form-group">
      <label class="col-sm-4 control-label" for="comp">Company</label>
      <div class="col-sm-8">
        <input type="text" class="form-control" id="comp" required/>
      </div>
      </div>
    </div>
    <!-- Modal Footer -->
    <div class="modal-footer">
      <button type="button" class="btn btn-default" data-dismiss="modal">
      Close
      </button>
      <button class="btn btn-primary">
      Save changes
      </button>
    </div>
    </form>
  </div>
  </div>
</div>

这最终给了我正在寻找的东西。虽然我现在有的新问题是如何抑制显然也在数据中找到的[]括号:

  • ???? ?????
  • HP PCI 10 / 100Base-TX核心[100BASE-TX,FD,AUTO,TT = 1500]
  • 固定?????

将这些括号添加到查询中会破坏数组边界:

select Name from table where Name LIKE '%[^0-9a-zA-Z() -._/\:=,]%'

然而,这是我可以处理的事情。只要我没有得到“全部”的数据。