解释下面几个函数用途,并写出sql的应该用方法及解释。  1)CONVERT()     用途:
     Sql:
   2)DATEDIFF()     用途:
     Sql:
      3)REPLACE()     用途:
4)SUBSTRING()      用途:
     Sql:
     5)SPACE()      用途:
     Sql:
 

解决方案 »

  1.   

    CONVERT() 用途CAST 和 CONVERT
    将某种数据类型的表达式显式转换为另一种数据类型。CAST 和 CONVERT 提供相似的功能。示例
    A. 同时使用 CAST 和 CONVERT
    每个示例都将检索书名(这些图书的截止当前销售额的第一位数字为 3),并将这些图书的 ytd_sales 转换为 char(20)。-- Use CAST.
    USE pubs
    GO
    SELECT SUBSTRING(title, 1, 30) AS Title, ytd_sales
    FROM titles
    WHERE CAST(ytd_sales AS char(20)) LIKE '3%'
    GO-- Use CONVERT.
    USE pubs
    GO
    SELECT SUBSTRING(title, 1, 30) AS Title, ytd_sales
    FROM titles
    WHERE CONVERT(char(20), ytd_sales) LIKE '3%'
    GO下面是任一查询的结果集:Title                          ytd_sales   
    ------------------------------ ----------- 
    Cooking with Computers: Surrep 3876        
    Computer Phobic AND Non-Phobic 375         
    Emotional Security: A New Algo 3336        
    Onions, Leeks, and Garlic: Coo 375         (4 row(s) affected)B. 使用带有算术运算符的 CAST
    下面的示例通过将总的截止当前销售额 (ytd_sales) 与每本图书的价格 (price) 相除,进行单独列计算 (Copies)。在四舍五入到最接近的整数后,此结果将转换为 int 数据类型。 USE pubs
    GO
    SELECT CAST(ROUND(ytd_sales/price, 0) AS int) AS 'Copies'
    FROM titles
    GO下面是结果集:Copies      
    ------ 
    205         
    324         
    6262        
    205         
    102         
    7440        
    NULL        
    383         
    205         
    NULL        
    17          
    187         
    16          
    204         
    418         
    18          
    1263        
    273         (18 row(s) affected)C. 使用 CAST 进行串联
    下面的示例使用 CAST 数据类型转换函数来串联非字符、非二进制表达式。USE pubs
    GO
    SELECT 'The price is ' + CAST(price AS varchar(12))
    FROM titles
    WHERE price > 10.00
    GO下面是结果集:------------------ 
    The price is 19.99        
    The price is 11.95        
    The price is 19.99        
    The price is 19.99        
    The price is 22.95        
    The price is 20.00        
    The price is 21.59        
    The price is 10.95        
    The price is 19.99        
    The price is 20.95        
    The price is 11.95        
    The price is 14.99        (12 row(s) affected)D. 使用 CAST 获得更多易读文本
    下面的示例在选择列表中使用 CAST 将 title 列转换为 char(50) 列,这样结果将更加易读。USE pubs
    GO
    SELECT CAST(title AS char(50)), ytd_sales
    FROM titles
    WHERE type = 'trad_cook'
    GO下面是结果集:                                                       ytd_sales
    --------------------------------------------------     ---------
    Onions, Leeks, and Garlic: Cooking Secrets of the      375
    Fifty Years in Buckingham Palace Kitchens              15096
    Sushi, Anyone?                                         4095(3 row(s) affected)E. 使用带有 LIKE 子句的 CAST
    下面的示例将 int 列(ytd_sales 列)转换为 char(20) 列,以便使用 LIKE 子句。USE pubs
    GO
    SELECT title, ytd_sales
    FROM titles
    WHERE CAST(ytd_sales AS char(20)) LIKE '15%'
       AND type = 'trad_cook'
    GO下面是结果集:title                                                        ytd_sales   
    ------------------------------------------------------------ ----------- 
    Fifty Years in Buckingham Palace Kitchens                    15096       (1 row(s) affected)
    DATEDIFF() 用途:
    DATEDIFF
    返回跨两个指定日期的日期和时间边界数。 语法
    DATEDIFF ( datepart , startdate , enddate ) 
    示例
    此示例确定在 pubs 数据库中标题发布日期和当前日期间的天数。USE pubs
    GO
    SELECT DATEDIFF(day, pubdate, getdate()) AS no_of_days
    FROM titles
    GO
    REPLACE() 用途:
    REPLACE
    用第三个表达式替换第一个字符串表达式中出现的所有第二个给定字符串表达式。语法
    REPLACE ( 'string_expression1' , 'string_expression2' , 'string_expression3' )参数
    'string_expression1'待搜索的字符串表达式。string_expression1 可以是字符数据或二进制数据。'string_expression2'待查找的字符串表达式。string_expression2 可以是字符数据或二进制数据。'string_expression3'替换用的字符串表达式。string_expression3 可以是字符数据或二进制数据。返回类型
    如果 string_expression(1、2 或 3)是支持的字符数据类型之一,则返回字符数据。如果 string_expression(1、2 或 3)是支持的 binary 数据类型之一,则返回二进制数据。示例
    下例用 xxx 替换 abcdefghi 中的字符串 cde。SELECT REPLACE('abcdefghicde','cde','xxx')
    GO下面是结果集:------------
    abxxxfghixxx
    (1 row(s) affected)
      

  2.   

    SUBSTRING() 用途:SUBSTRING
    返回字符、binary、text 或 image 表达式的一部分。有关可与该函数一起使用的有效 Microsoft® SQL Server™ 数据类型的更多信息,请参见数据类型。 语法
    SUBSTRING ( expression , start , length ) 参数
    expression是字符串、二进制字符串、text、image、列或包含列的表达式。不要使用包含聚合函数的表达式。start是一个整数,指定子串的开始位置。length是一个整数,指定子串的长度(要返回的字符数或字节数)。
    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)GOINSERT 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
    SPACE() 用途:
    SPACE
    返回由重复的空格组成的字符串。语法
    SPACE ( integer_expression )参数
    integer_expression是表示空格个数的正整数。如果 integer_expression 为负,则返回空字符串。返回类型
    char注释
    若要在 Unicode 数据中包括空格,请使用 REPLICATE 而非 SPACE。示例
    下例剪裁作者的姓氏并串联一个逗号、两个空格和作者的名字。USE pubs
    GO
    SELECT RTRIM(au_lname) + ',' + SPACE(2) +  LTRIM(au_fname)
    FROM authors
    ORDER BY au_lname, au_fname
    GO下面是结果集:Name                                                            
    --------------------------------------------------------------- 
    Bennet,  Abraham                                                
    Blotchet-Halls,  Reginald                                       
    Carson,  Cheryl                                                 
    DeFrance,  Michel                                               
    del Castillo,  Innes                                            
    Dull,  Ann                                                      
    Green,  Marjorie                                                
    Greene,  Morningstar                                            
    Gringlesby,  Burt                                               
    Hunter,  Sheryl                                                 
    Karsen,  Livia                                                  
    Locksley,  Charlene                                             
    MacFeather,  Stearns                                            
    McBadden,  Heather                                              
    O'Leary,  Michael                                               
    Panteley,  Sylvia                                               
    Ringer,  Albert                                                 
    Ringer,  Anne                                                   
    Smith,  Meander                                                 
    Straight,  Dean                                                 
    Stringer,  Dirk                                                 
    White,  Olivier                                                 
    Yokomoto,  Akiko                                                (23 row(s) affected)