求一条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 函数
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 函数
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
from [tb_test]
-- 要看最后的单位是不是都是一个的情况。
中 为什么 加'00'呢?
————————————————————---
select col=case when col='无' then '0' else left(col,1) end from tb_test
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 行受影响)
case
when col='无' then 0
else left(col,1)
end as col
from tb
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