Functions For Manipulating Data

Choosing the appropriate data type

topics covered: function for 🧮 data and time character coveredfunctionas working with numberic data

data types: exact numbers🧮

approximate numbers float and real note: you should use these tpees carefully and avoid using them on where clause with equality operator because you may get different results

Characters and unicode character data types🧮 ASCII and unicode character ascii🧮 (English characters) char varchar text unicode (from any language) nchar nvarchar ntext

implict conversion🧮

keep in mind: for comparing two values, they need to have the same datatype otherwise🧮 1-sql try to implicit conversion 2-explicit conversion

the value 0 conreesponcs to '1-1-1900' adding 1 add a new day

note about performance using implict conversion🧮 implict convertion is done row by row, so this may slow the performance alittle bit, having a good schema design is good

explict conversion🧮 CAST() and CONVERT() CAST() is sql standard, CONVERT() is sql server using convert() is slightly better more than CAST(), becuase when you type cast() is sql server, it converts it first to convert() so it execute addiotnal opertations

SELECT
 -- Transform the year part from the birthdate to a string
 first_name + ' ' + last_name + ' was born in ' + CAST(YEAR(birthdate) AS nvarchar) + '.'
FROM voters;


SELECT
 -- Transform to int the division of total_votes to 5.5
 CAST(total_votes / 5.5 AS int) AS DividedVotes
FROM voters;


SELECT
 first_name,
 last_name,
 total_votes
FROM voters
-- Transform the total_votes to char of length 10
WHERE CAST(total_votes AS int) LIKE '5%';

Manipulating Time

Working with strings

Functions for positions

LEN(): number of characters in a string CHARINDEX(): looks for a character expression in a given string and returns its starting positions. CHARINDEX(expression_to_find, expression_to_search [, start_location])

PATINDEX(): returning the starting position of a string. just like charindex but more powerful. using searching using pattern.

printing the lenght of a string

searching a string for a string

searching using a pattern

Functions for string transformations

LOWER() -> convert to lower case and UPPER() -> convert to upper case. LEFT(character_expression, number_of_characters) returns the specified number of characters from the beginning of the string. RIGHT(character_expression, number_of_characters) v=returns the specified number of characters from the end of the string.

LTRIM(chararcter_expression) -> returns a string after removing the leading blanks. RTRIM(character_expression) -> returns a string after removing the trainling blanks;TRIM([character FROM] character_Expression) -> returns a string after removing the blanks or other specified characters

REPLACE(character_expression, searched_expression, replacment_expression) -> returns a string where all occurrences of an expression are replaced with another one

SUBSTRING(character_expression, start, number_of_characters) -> returns part of a string.

exerices Most of the time, you can't make changes directly to the data from the database to make it look more user-friendly. However, when you query the data, you can control the aspect of the results, and you can make them easier to read.

Functions manipulting groups of string

these functions are newly introducted in sql server

CONCAT(string1 , string2, [. stringN]) CONCAT_WS(separator, string1, string2, [,stringN])

keep in mind: concatenating data with functions is better than usign the + operato; because the "+" may do addition if thr values are not strings and you can concatentate from all data types, nit just strings

STRING_AGG(expression, seperator) [<order_clause>] or string aggreagate; the sperator is added between the strings, but not in the end. an example

char(13) is the carriage return character, and a list seperated by this character will show values one below the other.

another effective way for using this function is with GROUP BY concatenating values in groups;

each year will have list of voters;

STRING_AGG() with optional <order_clause>

the voters will apear in alphabetical order in this table

STRING_SPLIT(string, seperator) - divide a string into smaller pieces, based on a seperator. - returns a single column table

we cannot use it as column in the select statement, but we can use it in the FROM clause just like a normal table;

exercises

Simple application

Recognizing Numeric Data Properties

Aggregate arithmetic functions

Analytic function

Exercise

Mathematical functions

Exercise

Last updated