Microsoft SQL Server


String Functions All Versions

SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server 2008 R2
SQL Server 2012
SQL Server 2014
SQL Server 2016

This draft deletes the entire topic.

Introduction

Introduction

expand all collapse all

Examples

  • 10

    Returns a Unicode string surrounded by delimiters to make it a valid SQL Server delimited identifier.

    Parameters:

    1. character string. A string of Unicode data, up to 128 characters (sysname). If an input string is longer than 128 characters function returns null.
    2. quote character. Optional. A single character to use as a delimiter. Can be a single quotation mark (' or ``), a left or right bracket ({,[,(,< or >,),],}) or a double quotation mark ("). Any other value will return null. Default value is square brackets.
    SELECT QUOTENAME('what''s my name?')      -- Returns [what's my name?]
    
    SELECT QUOTENAME('what''s my name?', '[') -- Returns [what's my name?]
    SELECT QUOTENAME('what''s my name?', ']') -- Returns [what's my name?]
    
    SELECT QUOTENAME('what''s my name?', '''') -- Returns 'what''s my name?'
    
    SELECT QUOTENAME('what''s my name?', '"') -- Returns "what's my name?"
    
    SELECT QUOTENAME('what''s my name?', ')') -- Returns (what's my name?)
    SELECT QUOTENAME('what''s my name?', '(') -- Returns (what's my name?)
    
    SELECT QUOTENAME('what''s my name?', '<') -- Returns <what's my name?>
    SELECT QUOTENAME('what''s my name?', '>') -- Returns <what's my name?>
    
    SELECT QUOTENAME('what''s my name?', '{') -- Returns {what's my name?}
    SELECT QUOTENAME('what''s my name?', '}') -- Returns {what's my name?}
    
    SELECT QUOTENAME('what''s my name?', '`') -- Returns `what's my name?`
    
  • 7

    Returns a string (varchar or nvarchar) where all occurrences of a specified sub string is replaced with another sub string.

    Parameters:

    1. string expression. This is the string that would be searched. It can be a character or binary data type.
    2. pattern. This is the sub string that would be replaced. It can be a character or binary data type. The pattern argument cannot be an empty string.
    3. replacement. This is the sub string that would replace the pattern sub string. It can be a character or binary data.
    SELECT REPLACE('This is my string', 'is', 'XX') -- Returns 'ThXX XX my string'.
    

    Note: If string expression is not of type varchar(max) or nvarchar(max), the replace function truncates the return value at 8,000 chars.

  • 6
    SQL Server 2016

    Splits a string expression using a character separator. Note that STRING_SPLIT() is a table-valued function and therefore must be used within FROM clause.

    Parameters:

    1. string. Any character type expression (char, nchar, varchar or nvarchar)
    2. seperator. A single character expression of any type (char(1), nchar(1), varchar(1) or nvarchar(1)).

    Returns a single column table where each row contains a fragment of the string. The name of the columns is value, and the datatype is nvarchar if any of the parameters is either nchar or nvarchar, otherwise varchar.

    The following example splits a string using space as a separator:

    SELECT value FROM STRING_SPLIT('Lorem ipsum dolor sit amet.', ' ');
    

    Result:

    value
    -----
    Lorem
    ipsum
    dolor
    sit
    amet.
    

    Remarks:

    The STRING_SPLIT function is available only under compatibility level 130. If your database compatibility level is lower than 130, SQL Server will not be able to find and execute STRING_SPLIT function. You can change the compatibility level of a database using the following command:

    ALTER DATABASE [database_name] SET COMPATIBILITY_LEVEL = 130
    
    SQL Server 2016

    Older versions of sql server does not have a built in split string function. There are many user defined functions that handles the problem of splitting a string. You can read Aaron Bertrand's article Split strings the right way – or the next best way for a comprehensive comparison of some of them.

Please consider making a request to improve this example.

Syntax

Syntax

Parameters

Parameters

Still have a question about String Functions? Ask Question

Topic Outline