同音查询
select f1,f2,f3 from tb where fc like '%@fc%' or hz2py(fc) like '%'+hz2py(@fc)+'%'
fc字段是关键字所在字段,如果整列用函数转换,查询效率怎么控制?
相似查询的情况就更不明白了...
...全文
49119打赏收藏
请教在sql中实现同音及相似查询
搜索了很多文章/贴子,发现类似问题一直没有得到解答,特发贴请教: 现在已经有了取得字符串首字母的函数, 但是取得字符串全拼的函数只有oracle的,sql里面不知道怎么实现? 同音查询 select f1,f2,f3 from tb where fc like '%@fc%' or hz2py(fc) like '%'+hz2py(@fc)+'%' fc字段是关键字所在字段,如果整列用函数转换,查询效率怎么控制? 相似查询的情况就更不明白了...
根据大力的贴子改成.将大力的两个函数合并成了一个函数.
可以应用于助记码的查询
--*/
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[fGetPy]') and xtype in (N'FN', N'IF', N'TF'))
drop function [dbo].[fGetPy]
GO
--创建取拼音函数
create function fGetPy(@Str varchar(500)='')
returns varchar(500)
as
begin
declare @strlen int,@return varchar(500),@ii int
declare @n int,@c char(1),@chn nchar(1)
select @strlen=len(@str),@return='',@ii=0
set @ii=0
while @ii<@strlen
begin
select @ii=@ii+1,@n=63,@chn=substring(@str,@ii,1)
if @chn>'z'
select @n = @n +1
,@c = case chn when @chn then char(@n) else @c end
from(
select top 27 * from (
select chn = '吖'
union all select '八'
union all select '嚓'
union all select '咑'
union all select '妸'
union all select '发'
union all select '旮'
union all select '铪'
union all select '丌' --because have no 'i'
union all select '丌'
union all select '咔'
union all select '垃'
union all select '嘸'
union all select '拏'
union all select '噢'
union all select '妑'
union all select '七'
union all select '呥'
union all select '仨'
union all select '他'
union all select '屲' --no 'u'
union all select '屲' --no 'v'
union all select '屲'
union all select '夕'
union all select '丫'
union all select '帀'
union all select @chn) as a
order by chn COLLATE Chinese_PRC_CI_AS
) as b
else set @c='a'
set @return=@return+@c
end
return(@return)
end
go
--测试
select dbo.fgetpy('青藏高原') as 东莞市,dbo.fgetpy('ab中c国人') as 中国人
VB.NET
的代码
''' <summary>
''' 日付 :2005/06/13
''' 作成者 :薛
''' </summary>Convert 濁音 to ArrayList
''' <param>ByRef arrayList As ArrayList, ByVal position As Integer</param>
''' <returns></returns>
Private Shared Sub ConvertToReturnArray(ByRef arrayList As ArrayList, ByVal position As Integer)
Dim i%
For i = position To arrayList.Count - 1
If hashTable_O.ContainsKey(arrayList.Item(i)) Then
' get new Arraylist
Dim relatedArray As arrayList = GetNewArrayList(arrayList)
' It change the value according to Key(Value) of HashTable
relatedArray.Item(i) = hashTable_O.Item(relatedArray.Item(i))
' add to final ArrayList
AddReturnString(relatedArray)
' The loop call for new
ConvertToReturnArray(arrayList, i + 1)
' The loop call for origion ArrayList
ConvertToReturnArray(relatedArray, i + 1)
Exit For
End If
Next
End Sub
''' <summary>
''' 日付 :2005/06/13
''' 作成者 :薛
''' </summary>It Get sequential Sql Condition and Add to final ArrayList
''' <param>ByVal stringArrayList As ArrayList</param>
''' <returns></returns>
Private Shared Sub AddReturnString(ByVal stringArrayList As ArrayList)
Dim strSQlCondition As String = ""
Dim i%
For i = 0 To stringArrayList.Count - 1
strSQlCondition += stringArrayList.Item(i).ToString
Next
returnList.Add(strSQlCondition)
End Sub
''' <summary>
''' 日付 :2005/06/13
''' 作成者 :薛
''' </summary> It will colne a New arrayList from origion ArrayList
''' <param>ByRef sourceArrayList As ArrayList</param>
''' <returns></returns>
Private Shared Function GetNewArrayList(ByRef sourceArrayList As ArrayList) As ArrayList
Dim newArrayList As ArrayList = New ArrayList
Dim i%
For i = 0 To sourceArrayList.Count - 1
newArrayList.Add(sourceArrayList.Item(i).ToString)
Next
Return newArrayList
End Function
''' <summary>
''' 日付 :2005/06/13
''' 作成者 :薛
''' </summary> Check the char whether exist hashTable
''' </summary>if exist will retun char of HashTable,else return origion char
''' <param>ByVal str As Strin</param>
''' <returns></returns>
Private Shared Function GetReplaceStr(ByVal str As String) As String
If hashTable_.ContainsKey(str) Then
Return hashTable_.Item(str).ToString
End If
Return str
End Function
''' <summary>
''' 日付 :2005/06/13
''' 作成者 :薛
''' </summary> It will replace the char with fining char of hashTable
''' <param>ByRef arrayList As ArrayList</param>
''' <returns></returns>
Private Shared Sub ReplaceWithHashTable(ByRef arrayList As ArrayList)
Dim i%
For i = 0 To arrayList.Count - 1
arrayList.Item(i) = GetReplaceStr(arrayList.Item(i).ToString)
Next
End Sub
''' <summary>
''' 日付 :2005/06/13
''' 作成者 :薛
''' </summary> It split source String and then add into arrayList
''' <param>ByVal strGetValue As String</param>
''' <returns></returns>
Private Shared Function ConvertToArrayList(ByVal strGetValue As String) As ArrayList
Dim arrayList As arrayList = New arrayList
Dim i%
Dim iLen%
For i = 0 To strGetValue.Length - 1
iLen = 2
If i + 2 > strGetValue.Length Then ' Checking the length whether it is beyond the boundary
iLen = strGetValue.Length - i
End If
If CheckStringState(strGetValue.Substring(i, iLen)) Then ' Get two char
arrayList.Add(strGetValue.Substring(i, iLen))
i = i + 1
Else
arrayList.Add(strGetValue.Substring(i, 1)) ' Get one Char
End If
Next
Return arrayList
End Function
''' <summary>
''' 日付 :2005/06/13
''' 作成者 :薛
''' </summary> Check the key whether it exist in HashTable,
''' </summary> it Will return true if it exist in,else return false
''' <param>ByVal strKey As String</param>
''' <returns></returns>
Private Shared Function CheckStringState(ByVal strKey As String) As Boolean
If hashTable_.ContainsKey(strKey) Or hashTable_O.ContainsKey(strKey) Then
Return True
End If
Return False
End Function
''' <summary>
''' 日付 :2005/06/13
''' 作成者 :薛
''' </summary> 拗音,促音,濁音 Init
''' <param>ByVal strGetValue As String</param>
''' <returns></returns>
Public Shared Function GetSqlConditionArrayList(ByVal strGetValue As String) As ArrayList
returnList.Clear()
' Set a ArrayList that can save the changing String
Dim strAfterAplitArray As ArrayList = New ArrayList
hashTable_.Clear()
hashTable_O.Clear()