1-How To Concatenate Two Binary Strings Together?
SQL Server 2005 allows to concatenate two binary strings into a single string with the (+) operator. The following tutorial exercise shows you some binary string concatenation examples: -- Concatenating two binary string literals SELECT 0x57656C636F6D6520746F20 + 0x46594963656E7465722E636F6D; GO 0x57656C636F6D6520746F2046594963656E7465722E636F6D -- Watch out: This is not a binary string concatenation SELECT '0x57656C636F6D6520746F20' + '0x46594963656E7465722E636F6D'; GO 0x57656C636F6D6520746F200x46594963656E7465722E636F6D -- Concatenating two binary strings SELECT CONVERT(VARBINARY(40),'Welcome to ') + CONVERT(VARBINARY(40),'GlobalGuideLine.com'); GO 0x57656C636F6D6520746F2046594963656E7465722E636F6D -- Binary strings can not be concatenated with character strings SELECT 'Welcome to ' + 0x46594963656E7465722E636F6D; GO Msg 402, Level 16, State 1, Line 1 The data types varchar and varbinary are incompatible in the add operator. 2-How To Locate and Take Substrings with CHARINDEX() and SUBSTRING() Functions? Transact-SQL is not a language designed for manipulating strings, but it does have two simple functions to locate and take substrings: CHARINDEX() and SUBSTRING(). The tutorial exercise below assumes two given strings: 'Pages: 18' and 'Words: 3240'. The objective is to calculate the number of words per page. Read the script below to see how this is done by using CHARINDEX() and SUBSTRING() functions: DECLARE @sPages VARCHAR(40), @sWords VARCHAR(40); SET @sPages = 'Pages: 18'; SET @sWords = 'Words: 3240'; SET @sPages = SUBSTRING(@sPages, CHARINDEX(':', @sPages)+1, 20); SET @sWords = SUBSTRING(@sWords, CHARINDEX(':', @sWords)+1, 20); PRINT 'Number of words per page: ' + CONVERT(VARCHAR(20), CONVERT(INT, @sWords)/CONVERT(INT, @sPages)); GO Number of words per page: 180 If you are a PHP developer, you can get this done in a much quick way. 3-
|
No comments:
Post a Comment