SQL - Select 来自 table 的最高值,其中值与格式匹配?
SQL - Select highest value from table where the value matches a format?
我有一个 variable
returns 一个 format
类似 variable = '10100001001'
我想 select 来自 column
(char 20) 的最高值 (max),其中该值与上述变量的格式匹配。
匹配项 -- 可能是 length
,等等,
我见过类似 select 最大值但不匹配格式的问题。
所以像 Select value from Table1 where value like ('%variable%')
我想知道有没有更明显和优雅的方法来实现这个。
示例数据:
id value
-- -- -- -- --
1 101000019
2 abcd123gh
3 101000026
4 abcd
5 23433
6 10100gh34
想要的结果:
101000026
谢谢
注意:不确定 "Matches -- might be the length
" 是什么意思,所以暂时忽略该花絮...
如果您所做的只是 like
模式匹配,您可以通过更新变量 (select @variable = '%' + @variable + '%'
) 或在 @variable 的末尾附加 %
或在 WHERE 子句中(见下文)。
这是一个简单的例子...
设置:
create table Table1 (id int, value varchar(20))
go
insert Table1 values (1,'101000019')
insert Table1 values (2,'abcd123gh')
insert Table1 values (3,'101000026')
insert Table1 values (4,'abcd')
insert Table1 values (5,' 23433')
insert Table1 values (6,'10100gh34')
go
select * from Table1
go
id value
----------- --------------------
1 101000019
2 abcd123gh
3 101000026
4 abcd
5 23433
6 10100gh34
示例搜索:
declare @variable varchar(20)
select @variable = '10100001001'
print "++++++++++++ pattern : %1!", @variable
select max(value) from Table1 where value like '%' + @variable + '%'
select @variable = '101000'
print "++++++++++++ pattern : %1!", @variable
select max(value) from Table1 where value like '%' + @variable + '%'
select @variable = '10100'
print "++++++++++++ pattern : %1!", @variable
select max(value) from Table1 where value like '%' + @variable + '%'
go
++++++++++++ pattern : 10100001001
max(Table1.value)
--------------------
NULL
++++++++++++ pattern : 101000
max(Table1.value)
--------------------
101000026
++++++++++++ pattern : 10100
max(Table1.value)
--------------------
10100gh34
我有一个 variable
returns 一个 format
类似 variable = '10100001001'
我想 select 来自 column
(char 20) 的最高值 (max),其中该值与上述变量的格式匹配。
匹配项 -- 可能是 length
,等等,
我见过类似 select 最大值但不匹配格式的问题。
所以像 Select value from Table1 where value like ('%variable%')
我想知道有没有更明显和优雅的方法来实现这个。
示例数据:
id value
-- -- -- -- --
1 101000019
2 abcd123gh
3 101000026
4 abcd
5 23433
6 10100gh34
想要的结果:
101000026
谢谢
注意:不确定 "Matches -- might be the length
" 是什么意思,所以暂时忽略该花絮...
如果您所做的只是 like
模式匹配,您可以通过更新变量 (select @variable = '%' + @variable + '%'
) 或在 @variable 的末尾附加 %
或在 WHERE 子句中(见下文)。
这是一个简单的例子...
设置:
create table Table1 (id int, value varchar(20))
go
insert Table1 values (1,'101000019')
insert Table1 values (2,'abcd123gh')
insert Table1 values (3,'101000026')
insert Table1 values (4,'abcd')
insert Table1 values (5,' 23433')
insert Table1 values (6,'10100gh34')
go
select * from Table1
go
id value
----------- --------------------
1 101000019
2 abcd123gh
3 101000026
4 abcd
5 23433
6 10100gh34
示例搜索:
declare @variable varchar(20)
select @variable = '10100001001'
print "++++++++++++ pattern : %1!", @variable
select max(value) from Table1 where value like '%' + @variable + '%'
select @variable = '101000'
print "++++++++++++ pattern : %1!", @variable
select max(value) from Table1 where value like '%' + @variable + '%'
select @variable = '10100'
print "++++++++++++ pattern : %1!", @variable
select max(value) from Table1 where value like '%' + @variable + '%'
go
++++++++++++ pattern : 10100001001
max(Table1.value)
--------------------
NULL
++++++++++++ pattern : 101000
max(Table1.value)
--------------------
101000026
++++++++++++ pattern : 10100
max(Table1.value)
--------------------
10100gh34