将字符串的各部分与分隔字符相互比较

时间:2017-01-13 10:52:55

标签: sql sql-server

我有两个带字符串的表。现在,这些字符串由不同的部分组成,由 angular.module('myapp',[]) .controller('test',function($scope){ $scope.myData = [{ "id": 1, "values": [{ "value": "" }] }, { "id": 2, "values": [{ "value": 213 }] }, { "id": 3, "values": [{ "value": "" }, { "value": "" }, { "value": "abc" }] },{ "id": 4, "values": [{ "value": "" }] },{ "id": 33, "values": [{ "value": "d" }] }]; }) .filter('filterData',function($filter){ return function(data) { var dataToBePushed = []; data.forEach(function(resultData){ var newValues=resultData; var hasData=$filter('filter')(resultData.values,{value:'!'},true); if(resultData.values && resultData.values.length>0 && hasData.length>0){ newValues.values=hasData; dataToBePushed.push(newValues); } }); debugger; return dataToBePushed; }; });分隔。例如:

表1:

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp">
    <div ng-controller="test">
        <div ng-repeat="data in myData | filterData:''">
            Id:{{ data.id }}
            </br>
            Values: {{ data.values }}
        </div>
    </div>
</div>

表2:

"

目前我收到了这样的查询:

------------------- | 1 | hello"world | ------------------- | 2 | hows"life | -------------------

此查询应显示这两行,因为如果您更改了表{1}}和------------------- | 1 | world"hello | ------------------- | 2 | hows"bro | ------------------- ,那么表2确实包含select * from table1 inner join table2 on table1.id = table2.id where table1.colum2 = table2.colum2。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:0)

如果您的column2在数据中只有一个",则以下查询将有效。

select *
from t1
join t2 
 on t1.id = t2.id
 and 
  SUBSTRING(t1.column2 ,CHARINDEX('"',t1.column2 )+1,len(t1.column2 )) = reverse(SUBSTRING(reverse(t2.column2 ),charindex('"',reverse(t2.column2 ))+1,len(t2.column2 )))
 and 
  reverse(SUBSTRING(reverse(t1.column2 ),charindex('"',reverse(t1.column2 ))+1,len(t1.column2 ))) = SUBSTRING(t2.column2 ,CHARINDEX('"',t2.column2 )+1,len(t2.column2 ));

希望这会帮助你。

答案 1 :(得分:0)

对于可重用性和干净的代码,我建议创建一个小函数:

CREATE FUNCTION [dbo].[SplitAndSwap]
(
    @input varchar(50),
    @separator char
)
RETURNS VARCHAR(50)
AS
BEGIN
    declare @separatorIndex int = CHARINDEX(@separator, @input)
    return 
        SUBSTRING(@input, @separatorIndex+1, LEN(@input) - @separatorIndex) +
        @separator + 
        SUBSTRING(@input, 0, @separatorIndex)
END

然后用作:

select * from @table1 t1 inner join @table2 t2
on t1.id = t2.id
where (t1.value = t2.value or t1.value = dbo.SplitAndSwap(t2.value, '"'))