SQL Server variable string concatenation

Source: Internet
Author: User

Reference:SQL Server null+ strings, isnull and assigning query results to one character variable to form another query statement

1. The string that starts the definition is null. For example: Declare @sql

Requirement: A string that loops through a cursor to get a phone number is stitched together. But the stitching was not successful.

Declare @dhhm  as varchar( -)--Defines the variable phone number if it is null if it is not assigned a value, and then the string is appended with an error .Declare @rcount  as INT--Defines the number of variables!!!!!!!!!!!!!!     Set @rcount=0--You must assign a value of 0 to a variable, or it is null after it cannot be ++!!!!!!!!     DECLARE @mobilnumber nvarchar( -)    DECLAREMyCursor1CURSOR    --Defining Cursors     for SelectMobilephone fromSys_userwhereFullNameinch(Select *  fromF_SPLITSTR ('system, China',','))        OPENMyCursor1--Open Cursor    FETCH NEXT  fromMyCursor1 into @mobilnumber    --print (' Cursor start lookup data ')    Print('Status of foreach'+Convert(varchar( $),@ @fetch_status))     while(@ @fetch_status=0)    BEGIN        Set @dhhm = IsNull(@dhhm,"')+@mobilnumber+','--the IsNull method is used here to determine the value of the variable @dhhm If NULL is assigned to an empty string, so the string concatenation error is not caused.
--If you do not want to use IsNull, in @dhhm, after defining the variable, you want to assign a value to the @dhhm to NULL, such as: set @dhhm = "
Set @rcount = @rcount+1 FETCH NEXT fromMyCursor1 into @mobilnumber END --Close Cursors CLOSEMyCursor1--Freeing Resources deallocateMyCursor1Print @dhhm Print('Number of SMS'+Convert(varchar( -),@rcount))

Interception of strings in sql: http://www.cnblogs.com/yeminglong/archive/2011/10/15/2212967.html

Declare @id  as varchar (+) Set @id = ' 123456, ' Select SUBSTRING (@id,0,Len(@id))
Returns the result 123456, the last comma minus the

SQL Server variable string concatenation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.