从IndexDB中检索具有部分字符串匹配键的记录

Retrieve records having partial string match for a key from IndexDB

本文关键字:字符串 字符 串匹配 记录 IndexDB 检索      更新时间:2023-09-26

我有我的联系方式JSON对象存储在indexDB。有一个"birthdate"字段,以04/05/2014 (dd/mm/yyyy)格式存储。

我只想通过匹配dd/mm来检索记录。我想检索具有特定生日的所有联系人(无论年份)。

有很多方法可以做到这一点。你可以创建一个函数来解析

列中的dd/mm

然后在where子句中写入@user_entry = dbo.fnc_GetDDMM(table.jsoncol)

函数可以利用索引和子字符串来提取DOB: DD/MM之后的任何内容在您提供的示例中,它可能看起来像

if object_id('dbo.GetBirthdayFromJson', 'fn') is not null
begin
    print 'drop: dbo.GetBirthdayFromJson'
    drop function dbo.GetBirthdayFromJson
end
go
print 'create: dbo.GetBirthdayFromJson'
go
create function dbo.GetBirthdayFromJson
(
    @json varchar(500)
)  
returns date
as  
begin 
    --declare @json varchar(500) = '{name:"sac",birthdate:"04/05/2014"}',
declare @birthday date
declare @birthdaystring varchar(50) = 'birthdate:"'
declare @blen int = len(@birthdaystring)
declare @bindex int = charindex(@birthdaystring, @json)


    set @birthday= substring(@json, @bindex + @blen, 10)
    return @birthday
end

那么你的where可以像这样

select * --dont use '*' but just an example
from sometable as table
where datepart(month, dbo.GetBirthdayFromJson(table.jsoncolumn)) == @somemonth 
and datepart(year, dbo.GetBirthdayFromJson(table.jsoncolumn)) == @someyear

希望能有所帮助