遇到一个棘手的问题(字符查询问题,在线等)

shan__le 2010-08-04 10:35:06
我数据库有一个字段是用字符型来存储一个数字的,带小数点的。
现在客户要求能够从这里查询出来数值区间,怎么写这个sql能够完成?
...全文
71 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
oO寒枫Oo 2010-08-27
  • 打赏
  • 举报
回复
select Services from dbo.Contract where ISNUMERIC(Services)=1 and (Services not like '%[^0-9]%' or charindex('.',Services)>1 ) and charindex('.',‘0’+Services)<>1
cast(Services as as numeric(12,2)) between 0 and 100
oO寒枫Oo 2010-08-27
  • 打赏
  • 举报
回复
考虑要周全 '1,2' '1%2' '.23' 都视为数字 这样写估计好一点

select Services from dbo.Contract where ISNUMERIC(Services)=1 and (Services not like '%[^0-9]%' or charindex('.',Services)>1 ) and charindex('.',Services)<>0
cast(Services as as numeric(12,2)) between 0 and 100
ACMAIN_CHM 2010-08-04
  • 打赏
  • 举报
回复
其中是不是有非数字的字符串啊?
wwwwb 2010-08-04
  • 打赏
  • 举报
回复
如果有NULL值,要事先判断一下
case when Services is null then 0
else
cast(Services as as numeric(12,2))
end
shan__le 2010-08-04
  • 打赏
  • 举报
回复
多的as去掉了,就是报上面的错误
shan__le 2010-08-04
  • 打赏
  • 举报
回复
先吃饭去了,回来在研究吧,估计换数据类型了。
wwwwb 2010-08-04
  • 打赏
  • 举报
回复
多了一个AS,去掉
shan__le 2010-08-04
  • 打赏
  • 举报
回复
Services
--------------------------------------------------
13.6422
18.612
11.589554
15.5

服务器: 消息 8114,级别 16,状态 5,行 1
将数据类型 varchar 转换为 numeric 时出错。
多了两个as,运行以后出现了一个错误,但是结果又出来了,很纳闷了
wwwwb 2010-08-04
  • 打赏
  • 举报
回复
select Services from dbo.Contract where cast(Services as as numeric(12,2))>10 and
cast(Services as as numeric(12,2))<30
shan__le 2010-08-04
  • 打赏
  • 举报
回复
13.00
34.20
4.82
37.00
12.62
35.60
108.90
16.80
141.60
例如这几个,我查询一个区间,那么就应该是
select Services from dbo.Contract where Services>'10' and Services<'30'
那么出来的应该是这几个 13.00 12.62 16.80
shan__le 2010-08-04
  • 打赏
  • 举报
回复
123.2009
176.7991
423.0
13.6422
4.059
18.612
5.0
192.0
165.0
11.589554
2008.49
235.62
390.896
194.8930
15.5
4.18
30.8
112.69
34.6761
20.00
20.72
5.136
374.70
18.0
47.2
220.4
34.26
250.88
11.73
27.3420
46.3780
40.0000
45.0000
123.0000
2.3893
399.8000
140.0000
36.0000
432.0000
36.0000
206.0000
34.3000
37.0000
35.0000
43.2800
174.8800
147.2500
30
39.00
390.896
137.00
48.50
30.33
5.78
13.08
30.00
147.50
16.50
35.00
45.98
4.16
3.17
218.00
450.00
136.00
136.00
2642.76
25.00
119.80
11.22
123.08
385.32
278.00
270.60
1085.00
20.35
25.00
45.00
175.00
15.00
121.50
15.00
18.00
163.00
141.56
4.76
22.09
128.00
129.10
3.82
5.00
1181.46
110.00
2.08
1432.24
15.00
159.80
3.80
12.00
120.01
3.55
4.06
26.73
43.55
41.50
48.00
5.50
36.79
428.06
39.20
28.00
172.90
278.01
240.00
11.00
28.38
448.00
141.40
44.40
22.69
3.00
50.00
16.20
371.60
160.00
43.28
112.12
5.00
12.96
3.54
2.50
24.35
155.72
228.00
20.00
151.06
13.50
28.25
16.93
32.00
40.00
40.30
471.42
45.00
2.25
43.50
35.00
1193.67
258.00
13.02
3.00
147.00
330.00
1600.00
18.00
25.00
26.58
185.28
384.00
30.00
15.60
366.00
304.71
37.70
5.50
251.10
126.00
34.00
197.00
130.00
356.27
131.00
4.00
13.00
230.00
190.00
2.80
260.00
14.90
200.00
404.44
151.00
108.00
16.37
20.00
203.00
230.00
40.00
210.00
40.30
147.86
35.72
115.00
441.00
4.50
37.00
140.00
210.00
113.80
2.80
209.84
16.77
30.00
120.00
166.52
18.40
5.00
132.00
5.00
104.00
13.00
44.00
2.20
11.56
30.25
108.53
377.68
325.50
34.00
3.50
281.00
28.20
207.76
163.76
24.00
13.00
18.20
3.90
47.40
231.00
13.00
40.00
323.00
14.40
103.00
258.74
266.40
274.40
43.20
21.23
3.00
227.36
256.75
40.70
39.90
28.70
272.06
49.40
200.00
3.80
30.00
4.90
22.21
363.00
16.00
5.00
12.00
167.34
41.54
2.70
319.00
232.88
2.08
269.00
150.00
150.00
47.00
191.90
162.37
38.00
176.95
24.00
27.00
150.23
3.89
155.67
200.00
17.30
37.80
277.00
145.34
31.50
5.59
179.36
188.00
2.00
2.00
5.25
33.48
2.52
11.60
135.00
47.40
20.00
4.00
5.20
178.20
25.00
26.00
34.80
2060.00
45.00
344.00
100.50
28.00
28.00
36.00
165.70
258.91
29.60
200.00
190.00
190.00
21.70
103.07
399.00
2.00
4.50
20.00
158.00
283.00
20.40
3.14
3.00
32.47
18.00
196.50
5.00
4.80
234.00
197.00
19.79
5.56
112.00
280.00
4.80
162.00
15.00
33.00
23.00
220.00
31.00
180.55
275.00
30.00
120.90
1270.00
46.00
31.50
32.50
3.75
2.46
3.50
397.00
35.60
49.30
2.40
13.80
2.00
120.15
246.10
34.00
3.00
32.92
30.00
215.97
15.00
180.01
12.13
148.00
12.60
493.80
180.18
4.00
28.20
342.27
16.80
178.36
13.60
13.61
23.83
3.48
17.10
3.00
42.00
5.00
4.10
20.55
3.00
18.00
30000.00
126.00
136.00
31.20
100.88
25.90
33.00
50.49
39.00
146.24
129.87
15.12
26.33
29.00
18.00
276.00
180.18
18.00
230.00
20.00
37.00
110.00
100.00
25.50
15.97
24.80
121.00
22.00
50.10
39.60
13.00
34.20
4.82
37.00
12.62
35.60
108.90
16.80
141.60
这些是字符型的
就是可以按照数字进行区间查询就行了,其他没要求。
WWWWA 2010-08-04
  • 打赏
  • 举报
回复
贴记录及要求结果出来看看
shan__le 2010-08-04
  • 打赏
  • 举报
回复
嗯嗯,很有可能,我需要排查一下数据,好多。

2,596

社区成员

发帖
与我相关
我的任务
社区描述
Sybase相关技术讨论区
社区管理员
  • Sybase社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧