---------------------------------------------------------------- -- Author :DBA_HuangZJ(发粪涂墙) -- Date :2014-03-21 10:30:19 -- Version: -- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64) -- Apr 2 2010 15:48:46 -- Copyright (c) Microsoft Corporation -- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor) -- ---------------------------------------------------------------- --> 测试数据[A] if object_id('[A]') is not null drop table [A] go create table [A]([b] nvarchar(18)) insert [A] select N'中国万岁1245' union all select N'中国挺住5678' union all select N'中国好样的7890' --------------生成数据--------------------------select substring(b,1,len(b)-4) from [A] ----------------结果---------------------------- /* ------------------ 中国万岁 中国挺住 中国好样的 */
if object_id('[A]') is not null drop table [A] go create table [A]([b] nvarchar(18)) insert [A] select N'中国万岁1245' union all select N'中国挺住5678' union all select N'中国好样的7890' select left(b,len(b)-4) a from A /* a 中国万岁 中国挺住 中国好样的 */
这个也可以:if object_id('[A]') is not null drop table [A] go create table [A]([b] nvarchar(18)) insert [A] select N'中国万岁1245' union all select N'中国挺住5678' union all select N'中国好样的7890' select left(b,patindex('%[0-9]%',b)-1) a from A /* a 中国万岁 中国挺住 中国好样的 */
select substring(b,1,len(b)-4) 这里的1代表什么意思呢?
代表,从b这个字段的第1个字符开始取,长度为len(b)-4的子串
IF OBJECT_ID('DBO.CHINA_STR') IS NOT NULL DROP FUNCTION DBO.CHINA_STR GO CREATE FUNCTION DBO.CHINA_STR(@S NVARCHAR(100)) RETURNS VARCHAR(100) AS BEGIN WHILE PATINDEX('%[^吖-座]%',@S) > 0 SET @S = STUFF(@S,PATINDEX('%[^吖-座]%',@S),1,N'') RETURN @S END GO SELECT DBO.CHINA_STR(b) from dbo.A GO
select 字段名=substring(字段名,1,len(字段名)-4) from 表名达到你这个效果用了两个函数,Substring和LenSubstring函数是抓出一个列中的其中一部分 Len函数是取得列的字符宽度
-- Author :DBA_HuangZJ(发粪涂墙)
-- Date :2014-03-21 10:30:19
-- Version:
-- Microsoft SQL Server 2008 R2 (RTM) - 10.50.1600.1 (X64)
-- Apr 2 2010 15:48:46
-- Copyright (c) Microsoft Corporation
-- Enterprise Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: ) (Hypervisor)
--
----------------------------------------------------------------
--> 测试数据[A]
if object_id('[A]') is not null drop table [A]
go
create table [A]([b] nvarchar(18))
insert [A]
select N'中国万岁1245' union all
select N'中国挺住5678' union all
select N'中国好样的7890'
--------------生成数据--------------------------select substring(b,1,len(b)-4) from [A]
----------------结果----------------------------
/*
------------------
中国万岁
中国挺住
中国好样的
*/
go
create table [A]([b] nvarchar(18))
insert [A]
select N'中国万岁1245' union all
select N'中国挺住5678' union all
select N'中国好样的7890'
select left(b,len(b)-4) a
from A
/*
a
中国万岁
中国挺住
中国好样的
*/
go
create table [A]([b] nvarchar(18))
insert [A]
select N'中国万岁1245' union all
select N'中国挺住5678' union all
select N'中国好样的7890'
select left(b,patindex('%[0-9]%',b)-1) a
from A
/*
a
中国万岁
中国挺住
中国好样的
*/
这里的1代表什么意思呢?
代表,从b这个字段的第1个字符开始取,长度为len(b)-4的子串
IF OBJECT_ID('DBO.CHINA_STR') IS NOT NULL
DROP FUNCTION DBO.CHINA_STR
GO
CREATE FUNCTION DBO.CHINA_STR(@S NVARCHAR(100))
RETURNS VARCHAR(100)
AS
BEGIN
WHILE PATINDEX('%[^吖-座]%',@S) > 0
SET @S = STUFF(@S,PATINDEX('%[^吖-座]%',@S),1,N'')
RETURN @S
END
GO
SELECT DBO.CHINA_STR(b) from dbo.A
GO
Len函数是取得列的字符宽度