Business Solutions
Formatting It is sometimes necessary to alter the output of a query for the sake of readability such as in report generation. This can also be applied to a view creation which users will share.
SUBSTRING Returns a part of a character or binary string. SUBSTRING (expression, start, length) SUBSTRING (expression, start, length) SUBSTRING (‘SQL Programming’, 1, 3) = SQL SUBSTRING (‘SQL Programming’, 1, 3) = SQL
CONVERT Changes one datatype to another. CONVERT(datatype[length], expression) CONVERT(char(2), ‘SQL’) = SQ CONVERT(int, ‘10’) = 10
Using them together... Select substring(title_id, 1, 2) as alpha convert(int, substring(title_id, 3, 4)) as num from titles
Other data Col_length - returns the length defined for a specific column. COL_LENGTH(‘table_name’, ‘col_name’) Data_length - returns the length of the data in an expression DATA_LENGTH(expression)
REPLICATE Replicates a given character or string the number of times specified. REPLICATE(char, int)
ROUND Rounds a number according to the specified accuracy. ROUND (col, int)
CHARINDEX Returns the index position of the specified string. CHARINDEX(search_str, str) CHARINDEX(‘.’, ‘12.3’) = 3
LIKE Ranges LIKE ‘[A-Z] [a-z] [a-z]’ Wildcards LIKE ‘_ _ _ _’ LIKE ‘[^] [^] [^]’ LIKE ‘%SQL%’