SQL string analyzer Functions

Source: Internet
Author: User

SQL string analyzer Functions

 
-- ===================================================== ====== -- Create function fun_splitstr, this function converts a string separated by a separator into a table -- @ strsource used to store the string to be analyzed -- @ strseprate used to store the delimiter -- @ temp used to return the analysis result (table) -- for example, @ strsource = '20140901'; @ strseprate = ',', a table is returned: first behavior 123 and second row 456 -- ========================================== ============== if exists (select * From sysobjects where name = n' fun _ splitstr ') drop function fun_splitstrgocreate function fun_splitstr (@ strsource varchar (8000), @ strseprate varchar (10) returns @ temp table (F1 varchar (100 )) as begindeclare @ I intset @ strsource = rtrim (ltrim (@ strsource) set @ I = charindex (@ strseprate, @ strsource) while @ I> = 1 begininsert @ temp values (left (@ strsource, @ i-1) set @ strsource = substring (@ strsource, @ I + 1, Len (@ strsource) -@ I) set @ I = charindex (@ strseprate, @ strsource) endif @ strsource <> ''in in insert @ temp values (@ strsource) endreturn end go

InstanceCode:

 
-- ===================================================== =======-- Example to execute function -- ============================== ======================== select * From DBO. fun_splitstr ('1, 2', ',') Go

Result:

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.