代码我希望数据在两个字段之前和两个字段之后给出id [英] code for i want data giving id with before two fields and after two fields

查看:76
本文介绍了代码我希望数据在两个字段之前和两个字段之后给出id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要sql查询来读取数据



1080200032060120121019130214这是一个id,就像在我的表中有13个ID。只是为了理解我这样给予



 108020003 20601 20121019130214 
108020003 20602 20121019130734
108020003 20603 20121019131157
108020003 20604 20121019131714
108020003 20605 20121019132137
108020003 20606 20121019141036
108020003 20607 20121019141451
108020003 20608 20121019141957
108020003 20609 20121019142735
108020003 20610 20121019144434
108020003 20611 20121019145003
108020003 20612 20121019145450
108020003 20613 20121019150619



id数据类型是nvarchar



i就像这种格式一样。基于20601到20613读取数据。



i给出id为10802000320607,我希望数据在两个字段之前和两个字段之后给出id

 108020003 20605 20121019132137 
108020003 20606 20121019141036
108020003 20607 20121019141451
108020003 20608 20121019141957
108020003 20609 20121019142735



i我正在使用bellow查询,它显示错误。

 选择  top   3  来自 表格 
其中 id> @ givenId
order by id

union

选择 top 2 来自
其中​​ id< @ givenid
order by id desc



plz帮帮我。

解决方案

< blockquote>这将得到所需的结果

 声明  @ givenId  数字 18  0 
set @ givenId = 10802000320607
选择 id 来自 table
其中​​ convert numeric 18 0 ), left (id, 14 ))介于 @ givenId-2 @ givenId + 2
< span class =code-keyword> order by id



Happy Coding!

:)


i want sql query for reading data

1080200032060120121019130214 this is one id like in my table having 13 ids. just for understanding i am giving like this

108020003 20601 20121019130214
108020003 20602 20121019130734
108020003 20603 20121019131157
108020003 20604 20121019131714
108020003 20605 20121019132137
108020003 20606 20121019141036
108020003 20607 20121019141451
108020003 20608 20121019141957
108020003 20609 20121019142735
108020003 20610 20121019144434
108020003 20611 20121019145003
108020003 20612 20121019145450
108020003 20613 20121019150619


id datatype is nvarchar

i am giving like this format. based on 20601 to 20613 read data.

i am giving id as 10802000320607 , i want data giving id with before two fields and after two fields

108020003 20605 20121019132137
108020003 20606 20121019141036
108020003 20607 20121019141451
108020003 20608 20121019141957
108020003 20609 20121019142735


i am using bellow query, it displays errors.

Select top 3 from table
where id > @givenId
order by id
 
union
 
select top 2 from table
where id < @givenid
order by id desc


plz help me.

解决方案

This will give desired result

Declare @givenId numeric(18,0)
set @givenId=10802000320607
Select id from table
where convert(numeric(18,0),left(id,14)) between @givenId-2 and @givenId+2
order by id


Happy Coding!
:)


这篇关于代码我希望数据在两个字段之前和两个字段之后给出id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆