Categories

.NET (1) CSS (5) OOP (1) PHP (2) SQL (5) SQL Server (8) TSQL (1)
Showing posts with label TSQL. Show all posts
Showing posts with label TSQL. Show all posts

Friday, February 18, 2011

T-SQL String Functions

 --[SUBSTRING( string, start, length )]
    SELECT SUBSTRING('abcdef', 4, 3);
   -- ## OUTPUT -- def ##--
   
 --[RUGHT( string, n )]
    SELECT RIGHT('abcde', 3);           
-- ## OUTPUT -- cde ##--
   
 ---[LEFT( string, n )]
    SELECT LEFT('abcde', 3);            
-- ## OUTPUT -- abc ##--
   
 --[LEN( string );]
    SELECT LEN('abcde');              
  -- ## OUTPUT -- 5 ##--
   
 --[CHARINDEX( substring, string [, start_pos] )]
    SELECT CHARINDEX('-','abcdef ghi-jk');   
-- ## OUTPUT -- 11 ##--
   
 --[PATINDEX( pattern, string )]
    SELECT PATINDEX('%[0-9]%', 'abcdfe124563efgh');   
-- ## OUTPUT -- 7 ##--
   
 --[REPLACE( string, substring1, substring2 )]
    SELECT REPLACE('1*3 2*87', '*', '-');           
-- ## OUTPUT -- 1-3 2-87 ##--
   
 --[REPLICATE( string, n )]
    SELECT REPLICATE('abcdef', 2);                
-- ## OUTPUT -- abcdefabcdef ##--
   
 --(The STUFF function allows you to remove a substring from a string
 -- and insert a new substring)
 -- [STUFF( string, pos, delete length, inserting string )]
    SELECT STUFF('tabc', 2, 3, 'his');    
-- ## OUTPUT -- this ##--
   
-- [UPPER( string ), LOWER( string )]
    SELECT UPPER('This is a BOOK');       
-- ## OUTPUT -- THIS IS A BOOK ##--
    SELECT LOWER('This is a BOOK');        
-- ## OUTPUT -- this is a book ##--
   
 --RTRIM( string ), LTRIM( string )
    SELECT LTRIM(' abc');                  
-- ## OUTPUT -- abc ##--
    SELECT RTRIM('abc ');
   
 -- [The LIKE predicate]
    SELECT NAME FROM TBLNAME WHERE NAME LIKE '_d%'  

 -- ## OUTPUT --    Will return names where second character will be d ##-- 
    SELECT NAME FROM TBLNAME WHERE NAME LIKE '[ABC]%'
 
-- ## OUTPUT -- Return the names where starting characters will be A,B or C  ##-- 
    SELECT NAME FROM TBLNAME WHERE NAME LIKE '[A-F]%' 
  
-- ## OUTPUT -- Return the names where starting characters will be A to F  ##-- 
    SELECT NAME FROM TBLNAME WHERE NAME LIKE '[^A-F]%'
-- ## OUTPUT -- Return the names where starting characters will not from A to F  ##--