Posts

Showing posts from April, 2023

Unpivot using SQL

Image
When converting columns into rows in SQL, unpivoting is used, effectively converting wide format (i.e., data with many columns) into long format (i.e., data with fewer columns and more rows). The process of pivoting, which includes turning rows into columns, is the opposite of this.   Here is the syntax of Unpivot in SQL: SELECT [column1], [column2], [column3] FROM (   SELECT [id], [column1], [column2], [column3]   FROM [table_name] ) AS [source_table] UNPIVOT (   [value] FOR [column_name] IN ([column1], [column2], [column3]) ) AS [unpivot_table]; In this syntax: [column1] , [column2] , and [column3] are the columns you want to unpivot. [table_name] is the name of the table containing the data you want to unpivot. [id] is a column that uniquely identifies each row in the table. [value] is the name of the column that will contain the values that were previously in the unpivoted columns. [column_name] is the name of the column that will contain the original column names. [unpivot_ta

System Functions in SQL

A group of built-in SQL functions known as "system functions" offer details on the database system, the active user, and the system environment. To access metadata about database items, keep an eye on system performance, and control user sessions, utilize these functions. Some examples of SQL system functions include: @@VERSION: Returns the version number of the database server. DATABASE(): Returns the name of the current database. USER(): Returns the name of the current user. SESSION_USER(): Returns the name of the current user for the current session. CURRENT_TIMESTAMP: Returns the current date and time of the database server. @@ROWCOUNT: Returns the number of rows affected by the last SQL statement. @@IDENTITY: Returns the last identity value generated for a table. In administration duties including tracking system performance, auditing user behavior, and maintaining database objects, system functions in SQL are frequently utilized. Type of System Functions in SQL: Databas

String Functions in SQL

SQL functions known as "string functions" enable the manipulation and modification of string data that is stored in a database. For tasks including concatenation, substring extraction, case conversion, length computation, whitespace removal, and pattern matching, SQL offers a collection of built-in string methods. These SQL functions can be used to change and format data in useful ways, and they can be applied to columns or literal strings. CONCAT(), SUBSTRING(), UPPER(), LOWER(), LENGTH(), TRIM(), and REPLACE() are a few typical SQL string operations. String functions CONCAT() Concatenates two or more strings together. Example: SELECT CONCAT('Hello', ' ', 'World'); Output: 'Hello World' SUBSTRING() Returns a portion of a string. Example: SELECT SUBSTRING('Hello World', 1, 5); Output: 'Hello' UPPER() Converts a string to uppercase. Example: SELECT UPPER('hello world'); Output: 'HELLO WORLD' LOWER() Converts a str

Date Functions in SQL

A date function in SQL is a pre-built function that operates on date and time information. With the help of these functions, you may modify date and time information, carry out computations, and extract particular elements from dates, like the day, month, or year. In SQL, common date functions include: CURRENT_DATE(): returns the current date. CURRENT_TIME(): returns the current time. CURRENT_TIMESTAMP(): returns the current date and time. DATEADD(): adds a specified interval to a date. DATEDIFF(): returns the difference between two dates. DATEPART(): returns a specific part of a date, such as the year, month, or day. DAY(): returns the day of the month from a given date. MONTH(): returns the month from a given date. YEAR(): returns the year from a given date. In data analysis and reporting, where dates are frequently used to track patterns over time, date functions are very helpful. Use a date function, for instance, to determine how many days there are between two dates or to separat