求一条sql语句【获取某列数字】

霜寒月冷 2012-03-14 04:23:47
求一条sql语句【获取某列数字】

if exists (select * from sysobjects where id = OBJECT_ID('[tb_test]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [tb_test]

CREATE TABLE [tb_test] (
[col] [nchar] (10) NULL)

INSERT [tb_test] ([col]) VALUES ( N'2个')
INSERT [tb_test] ([col]) VALUES ( N'2只')
INSERT [tb_test] ([col]) VALUES ( N'3块')
INSERT [tb_test] ([col]) VALUES ( N'无')
INSERT [tb_test] ([col]) VALUES ( N'4块')

select [col] from [tb_test]


要求 获取的值 为


col


2
2
3
0
4
-------------------
没有数字的 都是 0,请不要用substring 函数
...全文
108 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
霜寒月冷 2012-03-15
  • 打赏
  • 举报
回复
给最有价值的回答
霜寒月冷 2012-03-14
  • 打赏
  • 举报
回复
自己还是用标量函数解决了啊。
create function dbo.F_Get_No
(
@No varchar(100)
)
RETURNS bigint
AS
BEGIN
WHILE PATINDEX('%[^0-9]%',@No)>0
BEGIN
SET @No=STUFF(@No,PATINDEX('%[^0-9]%',@No),1,'') --删掉一个非数字的字符,循环结束,剩余的为数字部分
END
RETURN CONVERT(bigint,@No)
END
霜寒月冷 2012-03-14
  • 打赏
  • 举报
回复
当数值大于10 的时候 ,或者数字后面单位超过两个中文字符,上面的写法就不灵了啊
勿勿 2012-03-14
  • 打赏
  • 举报
回复
select 
case
when col='无' then 0
else left(col,1)
end as col
from tb
99归一 2012-03-14
  • 打赏
  • 举报
回复

if exists (select * from sysobjects where id = OBJECT_ID('[tb_test]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [tb_test]

CREATE TABLE [tb_test] (
[col] [nchar] (10) NULL)

INSERT [tb_test] ([col]) VALUES ( N'2个')
INSERT [tb_test] ([col]) VALUES ( N'2只')
INSERT [tb_test] ([col]) VALUES ( N'3块')
INSERT [tb_test] ([col]) VALUES ( N'无')
INSERT [tb_test] ([col]) VALUES ( N'4块')
select
case
when col='无' then 0
else left(col,1)
end as col
from tb_test

结果:
--------
col
-----------
2
2
3
0
4

(5 行受影响)
super007007007 2012-03-14
  • 打赏
  • 举报
回复
select case col when '无' then 0 else left(col,len(col)-1) end as col from tb_test
idealy 2012-03-14
  • 打赏
  • 举报
回复

select col=case when col='无' then '0' else left(col,1) end from tb_test
gal1024 2012-03-14
  • 打赏
  • 举报
回复
left(right('00'+[col],2),1)
中 为什么 加'00'呢?
————————————————————---
[Quote=引用 3 楼 jinfengyiye 的回复:]
select left(right('00'+[col],2),1)
from [tb_test]
-- 要看最后的单位是不是都是一个的情况。
[/Quote]
PL5240 2012-03-14
  • 打赏
  • 举报
回复
select (case col when '无' then 0 else left(col,1) end) as col from [tb_test]
PL5240 2012-03-14
  • 打赏
  • 举报
回复
select (case col when '无' then 0 else left(col,1) end) from [tb_test]
gw6328 2012-03-14
  • 打赏
  • 举报
回复
select left(right('00'+[col],2),1)
from [tb_test]
-- 要看最后的单位是不是都是一个的情况。
  • 打赏
  • 举报
回复
[Quote=引用楼主 chz415767975 的回复:]
求一条sql语句【获取某列数字】

if exists (select * from sysobjects where id = OBJECT_ID('[tb_test]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [tb_test]

CREATE TABLE [tb_test] (
[col] [nchar] ……
[/Quote]

楼主的数字都只是0-9吗?
  • 打赏
  • 举报
回复

if exists (select * from sysobjects where id = OBJECT_ID('[tb_test]') and OBJECTPROPERTY(id, 'IsUserTable') = 1)
DROP TABLE [tb_test]

CREATE TABLE [tb_test] (
[col] [nchar] (10) NULL)

INSERT [tb_test] ([col]) VALUES ( N'2个')
INSERT [tb_test] ([col]) VALUES ( N'2只')
INSERT [tb_test] ([col]) VALUES ( N'3块')
INSERT [tb_test] ([col]) VALUES ( N'无')
INSERT [tb_test] ([col]) VALUES ( N'4块')

select case when isnumeric(LEFT([col],1))=1 then LEFT([col],1) else 0 end as [col]
from [tb_test]

col
2
2
3
0
4

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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