Archive

Archive for the ‘ASCII’ Category

Save storage disk space by replacing "space" with "horizontal tabs"

August 19th, 2009 admin No comments

This was a simple experiment that I performed to find, how much difference it makes by replacing some of the characters in a field with others. The basic requirement was to have a varchar / nvarchar field with huge data :) . I created a sample table with a field of datatype  nvarchar(max). The table used no disk space

name                  rows    reserved    data    index_size    unused
TESTTABLE    0              0 KB          0 KB     0 KB               0 KB

Then inserted data into this field. Also didn’t forget to add lot of  “space” in the data. I inserted around 17 records in the table and then ran the stored proc to find the space used by the table.

the result was

name                 rows    reserved     data            index_size    unused
TESTTABLE     17         984 KB       968 KB 8 KB                  8 KB

I replaced the 3 consecutive spaces with a “horizontal tab” by using the query

UPDATE TESTTABLE
SET CHARS = REPLACE(TESTFIELD,’   ‘,CHAR(9))

then i checked the space used by the table

name                 rows          reserved    data           index_size    unused
TESTTABLE      17             968 KB        696 KB 8 KB                 264 KB

you might have already noticed that, there is a huge differnce in the data size. The size was reduced to 696KB from 968KB. This was for a small table with only 17 records, imagine for a table with millions of records :) ,  it can make a huge difference. Theoratically speaking 3 charchters were replace with a single character with out affecting the way the data is displayed in my UI . This saved me 4 bytes for each replace ( 1 characters = 2 byte for nvarchar, therefore 3-1 = 2 characters removed for each replace ). This does not mean that you can replace all characters with someting else.

One of my record had  23345 characters, the replace reduced it to  14761 characters

The unused space can be regained by using some techniques like shrinking the database.

Now the question in your mind might be, does the same work for char / nchar datatypes.  I leave that to you to find out. May be,  you already have the answer :) .

Please use the content at your own risk. In no event shall the
authors be liable for any claim, damages/issues that may be caused
by using the content.

Insert "Space","new line","tab" characters into a field as value

August 10th, 2009 admin No comments

There are times when you need to insert some special characters such as “newline”,”tab” etc as values into a field. Usually every one tries with ‘\n’ or ‘\t’, in vain. we can do this by using the ASCII value of the character. The steps are very simple. if you want to insert a special character find the ASCII value of that character like ‘ 9 for tab’, ‘ 12 for new line’ etc. you can find the lists of characters with its ASCII values at http://awesomesql.wordpress.com/2009/08/10/ascii-character-set-table. Once this is done, concatenate your string with the character of that particular ASCII value as

‘my string and’+ char(12)+’someting’

this will be

my string and

something

an examle is as shown below

INSERT INTO TABLES( FIELDS)

SELECT ‘hello’+char(9)+’:'+char(12)+’this is a new line’

this will be stored in the table as

hello    :

this is a new line

in the above example I inserted a tab after hello and a new line after ‘:’

you can similarly use

new line = char(12)

space = char(32)

horizontal tab = char(9)

carriage return = char(15)

vertical tab = char(13)

end of text = char(3)

for more look at the ASCII Table mentioned above. :)

Categories: ASCII, Joins, SQL Queries, SQL Server, T-SQL Tags:

ASCII character set table

August 10th, 2009 admin No comments

ASCII character set and values in decimal,hexadecimal,oct

Special Characters

Char Oct Dec Hex Remarks
NUL 0 0 0 Null character
SOH 1 1 1 Start of heading
STX 2 2 2 Start of text
ETX 3 3 3 End of text
EOT 4 4 4 End of transmission
ENQ 5 5 5 Enquiry, goes with ACK
ACK 6 6 6 Acknowledge, clears ENQ logon hand
BEL 7 7 7 Bell
BS 10 8 8 Backspace
HT 11 9 9 Horizontal tab
LF 12 10 a Line Feed
VT 13 11 b Vertical tab
FF 14 12 c Form Feed, page eject
CR 15 13 d Carriage Return
SO 16 14 e Shift Out
SI 17 15 f Shift In
DLE 20 16 10 Data link escape
DC1 21 17 11 XON, with XOFF to pause listings
DC2 22 18 12 Device control 2, block-mode flow control
DC3 23 19 13 XOFF, with XON is TERM=18
DC4 24 20 14 Device control 4
NAK 25 21 15 Negative acknowledge
SYN 26 22 16 Synchronous idle
ETB 27 23 17 End transmission block
CAN 30 24 17 Cancel line
EM 31 25 19 End of medium
SUB 32 26 1a Substitute
ESC 33 27 1b Escape
FS 34 28 1c File separator
GS 35 29 1d Group separator
RS 36 30 1e Record separator
US 37 31 1f Unit separator

Prinatable characters

Char Octal Dec Hex Remarks
SP 40 32 20 Space
! 41 33 21 Exclamation mark
42 34 22 Quotation mark
# 43 35 23 Cross hatch
$ 44 36 24 Dollar sign
% 45 37 25 Percent sign
& 46 38 26 Ampersand
` 47 39 27 back quote
( 50 40 28 Opening parentheses
) 51 41 29 Closing parentheses
* 52 42 2a Asterisk
+ 53 43 2b Plus
, 54 44 2c Comma
- 55 45 2d Hyphen, dash, minus
. 56 46 2e Period
/ 57 47 2f Slant (forward slash)
0 60 48 30 Zero
1 61 49 31 One
2 62 50 32 Two
3 63 51 33 Three
4 64 52 34 Four
5 65 53 35 Five
6 66 54 36 Six
7 67 55 37 Seven
8 70 56 38 Eight
9 71 57 39 Nine
: 72 58 3a Colon
; 73 59 3b Semicolon
< 74 60 3c Less than sign
= 75 61 3d Equals sign
> 76 62 3e Greater than sign
? 77 63 3f Question mark
@ 100 64 40 At-sign
A 101 65 41 Uppercase A
B 102 66 42 Uppercase B
C 103 67 43 Uppercase C
D 104 68 44 Uppercase D
E 105 69 45 Uppercase E
F 106 70 46 Uppercase F
G 107 71 47 Uppercase G
H 110 72 48 Uppercase H
I 111 73 49 Uppercase I
J 112 74 4a Uppercase J
K 113 75 4b Uppercase K
L 114 76 4c Uppercase L
M 115 77 4d Uppercase M
N 116 78 4e Uppercase N
O 117 79 4f Uppercase O
P 120 80 50 Uppercase P
Q 121 81 51 Uppercase Q
R 122 82 52 Uppercase R
S 123 83 53 Uppercase S
T 124 84 54 Uppercase T
U 125 85 55 Uppercase U
V 126 86 56 Uppercase V
W 127 87 57 Uppercase W
X 130 88 58 Uppercase X
Y 131 89 59 Uppercase Y
Z 132 90 5a Uppercase Z
[ 133 91 5b Opening square bracket
\ 134 92 5c Reverse slant (Backslash)
] 135 93 5d Closing square bracket
^ 136 94 5e Caret
_ 137 95 5f Underscore
` 140 96 60 Opening single quote
a 141 97 61 Lowercase a
b 142 98 62 Lowercase b
c 143 99 63 Lowercase c
d 144 100 64 Lowercase d
e 145 101 65 Lowercase e
f 146 102 66 Lowercase f
g 147 103 67 Lowercase g
h 150 104 68 Lowercase h
i 151 105 69 Lowercase i
j 152 106 6a Lowercase j
k 153 107 6b Lowercase k
l 154 108 6c Lowercase l
m 155 109 6d Lowercase m
n 156 110 6e Lowercase n
o 157 111 6f Lowercase o
p 160 112 70 Lowercase p
q 161 113 71 Lowercase q
r 162 114 72 Lowercase r
s 163 115 73 Lowercase s
t 164 116 74 Lowercase t
u 165 117 75 Lowercase u
v 166 118 76 Lowercase v
w 167 119 77 Lowercase w
x 170 120 78 Lowercase x
y 171 121 79 Lowercase y
z 172 122 7a Lowercase z
{ 173 123 7b Opening curly brace
| 174 124 7c Vertical line
} 175 125 7d Cloing curly brace
~ 176 126 7e Tilde
DEL 177 127 7f Delete




Categories: ASCII, SQL Server Tags: