0% found this document useful (0 votes)
11 views2 pages

T-SQL Functions Overview

The document provides a comprehensive overview of various T-SQL functions categorized into numeric, string, date, system, and aggregate functions. It includes examples of each function type, demonstrating their usage in SQL queries. The examples cover operations such as mathematical calculations, string manipulations, date retrieval, and database information queries.

Uploaded by

SHAHIDHA
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views2 pages

T-SQL Functions Overview

The document provides a comprehensive overview of various T-SQL functions categorized into numeric, string, date, system, and aggregate functions. It includes examples of each function type, demonstrating their usage in SQL queries. The examples cover operations such as mathematical calculations, string manipulations, date retrieval, and database information queries.

Uploaded by

SHAHIDHA
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

'''T-SQL Functions'''

'''Numeric Functions'''
select abs(34.5);
select sin(90);
select cos(90);
select tan(90);
select exp(4);
select log(10);
select pi();
select sign(-2);
select sqrt(4);
select rand();
select ceiling(34.78);
select floor(34.78);
select degrees(90);
select radians(90);
select round(34.45566,1);

'''String Functions'''

Select ASCII('CSC');
Select CHAR(67);
select len('text');
select lower('SHAHIDHA');
select UPPER('shahidha');
select ltrim(' shahidha');
select rtrim('shahidha ');
select reverse('shahidha');
select replicate('shahidha',2);
select left('shahidha',3);
select right('shahidha',3);
select space(100);
select str(13.5);
select stuff('Shhaidha',2,4,'Begum');
'''Date Functions'''
select getdate();
select getdate()+5;
select day('3/3/2022');
select month('3/3/2022');
select year('3/3/2022');

'''System Functions'''
select db_name();
select db_id('sultan');
select host_name();
select datalength('shahidha');
select object_id('sultan');
select object_name('sultan');
select suser_id('sa');
select user_id(1);
select suser_name(1);

'''Aggregate Functions'''
use kabeer;
select * from student;
select avg(mark) from student;
select sum(mark) from student;
select count(mark) from student;
select count(*) from student;
select min(mark) from student;
select max(mark) from student;

Common questions

Powered by AI

T-SQL Numeric Functions provide a variety of capabilities to perform arithmetic and trigonometric calculations. For arithmetic operations, functions like 'select abs(34.5);' return the absolute value of a number, 'select exp(4);' computes the exponential value of a number, and 'select sqrt(4);' returns the square root. Trigonometric calculations can be executed using 'select sin(90);', 'select cos(90);', and 'select tan(90);', which calculate the sine, cosine, and tangent of an angle respectively. The rounding functions such as 'select round(34.45566,1);' help in controlling the precision of numeric values .

System Functions like USER_ID and SUSER_NAME in T-SQL are significant for security and auditing as they provide details about user identities operating within the database. 'select user_id(1);' can return a user's system identifier, while 'select suser_name(1);' reveals the corresponding system username. These functions enable precise tracking of user actions, supporting rigorous audit trails and security protocols by identifying who accessed or modified data, essential in maintaining security compliance and monitoring unauthorized access .

T-SQL Aggregate Functions are crucial for data analysis as they provide a means to calculate values across numerous rows. For instance, in a student database, 'select avg(mark) from student;' calculates the average mark, and 'select sum(mark) from student;' aggregates the total marks. These functions help summarize data, detect trends, and derive insights across datasets by reducing complex data into comprehensible summaries .

The T-SQL Floor and Ceiling functions are used for rounding numbers down or up respectively. 'select floor(34.78);' demonstrates how the Floor function rounds a number down to the nearest integer, returning 34 in this case. Conversely, 'select ceiling(34.78);' rounds the number up to the nearest integer, resulting in 35. These functions are useful for scenarios requiring rounding numbers towards or away from zero in financial calculations and reports .

T-SQL String Functions are essential for manipulating and transforming text data within SQL Server environments. For example, the 'select lower('SHAHIDHA');' function transforms a string to lowercase, converting 'SHAHIDHA' to 'shahidha'. Similarly, 'select UPPER('shahidha');' transforms text to uppercase. String functions can also modify the structure of text, such as 'select stuff('Shhaidha',2,4,'Begum');' which replaces part of a string, resulting in 'SBegumidha' .

The ASCII and CHAR functions in T-SQL facilitate character encoding management by converting characters to their respective ASCII values and vice versa. 'Select ASCII('CSC');' returns the ASCII value of the first character, 'C', while 'Select CHAR(67);' returns the character corresponding to ASCII value 67, which is 'C'. This bidirectional conversion supports encoding diagnostics, character data parsing, and transformation tasks often crucial in ensuring text data compatibility across different systems or character sets .

T-SQL Date Functions facilitate the management and manipulation of date data. These functions can extract specific parts of a date, such as 'select day('3/3/2022');', 'select month('3/3/2022');', and 'select year('3/3/2022');' which return the day, month, and year respectively. Additionally, they can be used for calculations involving dates; for instance, 'select getdate()+5;' adds five days to the current date .

T-SQL's RAND() function generates a pseudo-random number between 0 and 1 which is invaluable in simulations where random data is required. Although it generates a floating-point number, RAND() can be used in more considerable operations by scaling and offsetting the result, for example, to generate a random number within a range such as '(RAND() * 100) + 1' to simulate scores between 1 and 100. It's useful in testing, modeling probabilistic processes, and developing scenarios in algorithms requiring randomness .

T-SQL System Functions support database management and operations by providing information about the system and its objects. Functions like 'select db_name();' and 'select db_id('sultan');' offer database identification capabilities. 'select datalength('shahidha');' returns the number of bytes used for data storage, supporting app and database performance tuning. Additionally, functions such as 'select host_name();' and 'select user_id(1);' deliver system-level detail necessary for user management and application configuration .

T-SQL functions handle text trimming using 'select ltrim(' shahidha');' and 'select rtrim('shahidha ');' to remove leading and trailing spaces respectively. Such functions are pivotal in data cleaning processes where ensuring consistent data format is crucial. They often precede comparisons and data insertions when whitespace may cause string mismatches or redundancy, affecting the clarity and correctness of datasets used in reporting or data analytics .

You might also like