mysql substring

来源:互联网 发布:江苏微盛网络是传销吗 编辑:程序博客网 时间:2024/06/06 01:51


 

 

substring:

 

语法
SUBSTRING  expression  ,  start  ,  length    
参数

expression      

是字符串、二进制字符串、text、image、列或包含列的表达式。不要使用包含聚合函数的表达式。  

start

是一个整数,指定子串的开始位置。

  

length

  

是一个整数,指定子串的长度(要返回的字符数或字节数)。     

说明   由于在 text  数据上使用 SUBSTRING 时 start  和 length  指定字节数,因此 DBCS 数据(如日本汉字)可能导致在结果的开始或结束位置拆分字符。此行为与 READTEXT 处理 DBCS 的方式一致。然而,由于偶而会出现奇怪的结果,建议对 DBCS 字符使用 ntext  而非 text 。   

返回类型

如果 expression  是支持的字符数据类型,则返回字符数据。如果 expression  是支持的 binary  数据类型,则返回二进制数据。   

返回字符串的类型与给定表达式的类型相同(表中显示的除外)。      

给定的表达式返回类型text varchar image varbinary ntext nvarchar

 

注释

在字符数中必须指定使用 ntextchar  或 varchar  数据类型的偏移量(start  和 length )。在字节数中必须指定使用 textimagebinary  或 varbinary  数据类型的偏移量。     

说明   兼容级别可能影响返回值。有关兼容级别的更多信息,请参见 sp_dbcmptlevel 。  

示例 
A. 在字符串上使用 SUBSTRING     

下例显示如何只返回字符串的一部分。该查询在一列中返回 authors  表中的姓氏,在另一列中返回 authors  表中的名字首字母。

USE pubs
    SELECT au_lname, SUBSTRING(au_fname, 1, 1)
    FROM authors
    ORDER BY au_lname
下面是结果集:
au_lname                                   
    ---------------------------------------- - 
    Bennet                                   A 
    Blotchet-Halls                           R 
    Carson                                   C 
    DeFrance                                 M 
    del Castillo                             I 
    ...
    Yokomoto                                 A 
    
    (23 row(s) affected)

   

下例显示如何显示字符串常量 abcdef 中的第二个、第三个和第四个字符。

SELECT x = SUBSTRING('abcdef', 2, 3)
    
下面是结果集:
x
    ----------
    bcd
    
    (1 row(s) affected)
    

      

B. 在 text、ntext 和 image 数据上使用 SUBSTRING 

      

下例显示如何从 pubs  数据库的 publishers  表内的每个 text  和 image  数据列中返回前 200 个字符。text  数据以 varchar  的形式返回,image  数据则以 varbinary  的形式返回。

USE pubs
    SELECT pub_id, SUBSTRING(logo, 1, 10) AS logo, 
       SUBSTRING(pr_info, 1, 10) AS pr_info
    FROM pub_info
    WHERE pub_id = '1756'
   

下面是结果集:

pub_id logo                   pr_info    
    ------ ---------------------- ---------- 
    1756   0x474946383961E3002500 This is sa
    
    (1 row(s) affected)
    
    

下例显示 SUBSTRING 在 text  和 ntext  数据上的效果。首先,下例在 pubs  数据库内创建一个名为 npr_info  的新表。然后,在 npr_info  表中用 pub_info.pr_info  列的前 80 个字符创建 pr_info  列,并添加ü作为首字符。最后,INNER JOIN 检索所有出版商标识号以及 text  和 ntext  出版商信息列的 SUBSTRING。

IF EXISTS (SELECT table_name FROM INFORMATION_SCHEMA.TABLES 
          WHERE table_name = 'npub_info')
       DROP TABLE npub_info
    GO
    -- Create npub_info table in pubs database. Borrowed from instpubs.sql.
    USE pubs
    GO
    CREATE TABLE npub_info
    (
     pub_id         char(4)           NOT NULL
             REFERENCES publishers(pub_id)
             CONSTRAINT UPKCL_npubinfo PRIMARY KEY CLUSTERED,
     pr_info        ntext             NULL
    )
    
    GO
    
    -- Fill the pr_info column in npub_info with international data.
    RAISERROR('Now at the inserts to pub_info...',0,1)
    
    GO
    
    INSERT npub_info VALUES('0736', N'
üThis is sample text data for New Moon Books, publisher 0736 in the pubs database')
    INSERT npub_info values('0877', N'
üThis is sample text data for Binnet & Hardley, publisher 0877 in the pubs databa')
    INSERT npub_info values('1389', N'
üThis is sample text data for Algodata Infosystems, publisher 1389 in the pubs da')
    INSERT npub_info values('9952', N'
üThis is sample text data for Scootney Books, publisher 9952 in the pubs database')
    INSERT npub_info values('1622', N'
üThis is sample text data for Five Lakes Publishing, publisher 1622 in the pubs d')
    INSERT npub_info values('1756', N'
üThis is sample text data for Ramona Publishers, publisher 1756 in the pubs datab')
    INSERT npub_info values('9901', N'
üThis is sample text data for GGG&G, publisher 9901 in the pubs database. GGG&G i')
    INSERT npub_info values('9999', N'
üThis is sample text data for Lucerne Publishing, publisher 9999 in the pubs data')
    GO
    -- Join between npub_info and pub_info on pub_id.
    SELECT pr.pub_id, SUBSTRING(pr.pr_info, 1, 35) AS pr_info,
       SUBSTRING(npr.pr_info, 1, 35) AS npr_info
    FROM pub_info pr INNER JOIN npub_info npr
       ON pr.pub_id = npr.pub_id
    ORDER BY pr.pub_id ASC
    

    select SUBSTRING(date_entered,1,4)  as a from tb_area_collect  group by SUBSTRING(date_entered,1,4)

 

From:

http://hi.baidu.com/qk31/blog/item/ca1dd11b3a69ebd5ad6e75c5.html

------------------------------------------------------

replace:

遇到想替换数据库里某一字段的内容时可以用REPLACE函数: 

语法如下: 
UPDATE tb1 SET f1=REPLACE(f1, 'abc', 'def'); 

REPLACE(str,from_str,to_str) 
在字符串 str 中所有出现的字符串 from_str 均被 to_str替换,然后返回这个字符串: 
mysql> select REPLACE('www.mysql.com', 'w', 'Ww'); 
-> 'WwWwWw.mysql.com' 
这个函数是多字节安全的。 

示例: 
UPDATE `dede_addonarticle` SET body = REPLACE ( body, 
'</td>', 
'' ); 
UPDATE `dede_addonarticle` SET body = REPLACE ( body, 
'</tr>', 
'' ); 
UPDATE `dede_addonarticle` SET body = REPLACE ( body, 
'<tr>', 
'' ); 
UPDATE `dede_archives` SET title= REPLACE ( title, 
'大洋新闻 - ', 
'' ); 
UPDATE `dede_addonarticle` SET body = REPLACE ( body, 
'../../../../../../', 
'http://special.dayoo.com/meal/' ); 

mysql replace 

用法1.replace into

replace into table (id,name) values(‘1‘,‘aa‘),(‘2‘,‘bb‘) 
此语句的作用是向表table中插入两条记录。 
2.replace(object, search,replace) 
把object中出现search的全部替换为replaceselect replace(‘www.webjx.com‘,‘w‘,‘Ww‘)--->WwW wWw.webjx.com 

例:把表table中的name字段中的 aa替换为bbupdate table set name=replace(name,‘aa‘,‘bb‘)

From:

http://www.jcwcn.com/article/2008/0730/mysql_30777.html

ps:自我测试:

例如,数据库中存在如下字段数据

open_date

10  1 1993 12:00AM

2001年 

2003-10-1

.......

----

如果想把上面字段数据改为只含年份,即如:1993,则,写法如下:

 

UPDATE table SET open_date=REPLACE(open_date,open_date,SUBSTRING(open_date,7,4)) WHERE open_date LIKE "%am"

其他如下:

10  1 1993 12:00AM  (%am)

2001年              ("%年")

2003-10-1    ("%-%-%")

 

原创粉丝点击