Friday, September 30, 2011

Converts an int ASCII code to a character.

Char will convert an int ASCII code to a character. 
Syntax 
CHAR ( integer_expression ) 
Integer_expression
            Integer_expression is an integer from 0 through 255. NULL is returned if the integer expression is not in this range. 

Example  
Char(1) . . . . . . . Char(255)  
               CHAR can be used to insert control characters into character string. The following table shows some frequently used control characters.

Control character
value
Tab
Char(9)
Line feed
Char(10)
Carriage return
Char(13)


CREATE TABLE #Temp (id INT IDENTITY,symd CHAR(10))

DECLARE @I INT =1
WHILE (@I<=255)
BEGIN
INSERT INTO #Temp SELECT CHAR(@I)
SET @I =@I+1
END
SELECT id 'Char Value',symd 'Control character'  FROM #Temp

DROP TABLE #Temp

Wednesday, September 21, 2011

Constructing row values into comma-separate string

 ---Create Table
CREATE TABLE #USER (USERNAME VARCHAR(25))

---Inserting Values
INSERT INTO #USER (USERNAME) VALUES ('Loki')
INSERT INTO #USER (USERNAME) VALUES ('Chinnu')
INSERT INTO #USER (USERNAME) VALUES ('Chinni')
INSERT INTO #USER (USERNAME) VALUES ('Laddu')

---Constricting comma-separate string
SELECT DISTINCT STUFF((SELECT ',' +USERNAME  FROM #user  FOR XML PATH ('')),1,1,'')AS LIST FROM #user

--- Drop table
DROP TABLE #USER