Search

Wednesday, August 10, 2011

Reverse

REVERSE just reverses the value, for example the code below returns CBA
SELECT REVERSE('ABC')
Reverse is handy if you need to split values, take a look at this example
CREATE TABLE #TestTable (csz CHAR(49))
    INSERT INTO #TestTable VALUES ('city ,st 12223')
    INSERT INTO #TestTable VALUES ('New York City,NY 10028')
    INSERT INTO #TestTable VALUES ('Princeton , NJ 08536')
    INSERT INTO #TestTable VALUES ('Princeton,NJ 08536 ')
    INSERT INTO #TestTable VALUES ('Long Island City, NY 10013')
    INSERT INTO #TestTable VALUES ('Long Island City, NY 10013 ')
    INSERT INTO #TestTable VALUES ('Long Island City , NY 10013')
    INSERT INTO #TestTable VALUES ('Long Island City ,NY 10013 ')
 
    SELECT LEFT(csz,CHARINDEX(',',csz)-1)AS City,
    LEFT(LTRIM(SUBSTRING(csz,(CHARINDEX(',',csz)+1),4)),2) AS STATE,
    RIGHT(RTRIM(csz),CHARINDEX(' ',REVERSE(RTRIM(csz)))-1) AS Zip
    FROM #TestTable

No comments:

Post a Comment