7/23/2021

SQL Server day-to-day mostly used functions - String, Aggregate, Window, Date Functions in SQL

 SQL Server day-to-day mostly used functions - String, Aggregate, Window, Date Functions in SQL

CAST() – This function is used to convert an expression of one data type to another. The CAST function is used to convert a data type without a specific format.

          Syntax – CAST(Coulmn_Name, Data_Type)

SELECT CAST(1234, INT)

CONVERT()This function is used to convert and formatting data types at the same time.

          Syntax – CONVERT(Data_Type ,Coulmn_Name, Format_TYPE)

SELECT CONVERT(Varchar,GateDate(), 111) FROM Table_Name

SUBSTR()This function extracts a substring from a string (starting at any position).

          Syntax SELECT SUBSTRING(‘ABCDEF'13)

STUFF()The STUFF function inserts a string into another string. It deletes a specified length of characters in the first string at the start position.

          Syntax – SELECT STUFF('ABCDEF', 2, 3, 'IJKL')

REPLACE() – This function is used to replace an existing specified string value with another string value of all occurrence.

          Syntax – SELECT REPLACE('ABCDEFGHI','EFG','LMN')

RTRIM()This function removes trailing spaces from a string from the right side.

          Syntax – SELECT RTRIM(' ABCDEF ')

LTRIM() - This function removes trailing spaces from a string from the left side.

          Syntax – SELECT LTRIM(' ABCDEF ')

LEFT() – This Function extracts a number of characters from a string (starting from left)

          Syntax – SELECT LTRIM('ABCDEF', 3)

RIGHT() – This Function extracts a number of characters from a string (starting from right).

          Syntax – SELECT RIGHT('ABCDEF',3)

CHARINDEX() – This Function is used to searches for a substring in a string and returns the position.

          Syntax – SELECT (CHARINDEX('CDEF', 'ABCDEFGH'))

PATINDEX() – This Function is used to get starting position of a pattern by using wild characters in the pattern.

          Syntax – SELECT (PATINDEX('%CDEF%', 'ABCDEFGH'))

ISNULL() – This function is used to specify how we want to treat NULL values.

Syntax – SELECT ISNULL(Column_Name, Specified_value) FROM Table_Name

COALESCE() – This function is used to handle null value from multiple arguments, and always returns first non-null value.  

          Syntax – SELECT COALESCE( NULL, ‘ABC’, ‘DEF’)

NVL() – This Function converts a null value to an actual value. Data types that can be used are date, character, and number.

          Syntax – SELECT NVL(ColumnValue, 0)

UCASE() – This function is used to convert the column value into upper case.

Syntax – SELECT UCASE(Column_Name) FROM Table_Name

LCASE() – This function is used to convert the column value into lower case.

Syntax – SELECT LCASE(Column_Name) FROM Table_Name

MID() – This Function extracts a substring from a string (starting at any position).

          Syntax SELECT MID(‘ABCDEFGH’43)

LEN() – This function calculates the number of characters of an input string.

          Syntax – SELECT LEN(‘ABCDEFGH’)

ROUND() – This Function rounds a number to a specified number of decimal places.

          Syntax – SELECT ROUND (12345.6789, 2)

AVG()This function returns the average value of a numeric column.

          Syntax – SELECT AVG(Column_Name) FROM Table_Name

COUNT()This function returns the number of rows.

          Syntax – SELECT COUNT(Column_Name) FROM Table_Name

FIRST()This function returns the first value of the selected column.

Syntax – SELECT FIRST(Column_Name) FROM Table_Name

LAST()This function returns the first value of the selected column.

Syntax – SELECT FIRST(Column_Name) FROM Table_Name

MAX() – This function returns the maximum value of the selected column.

Syntax – SELECT MAX(Column_Name) FROM Table_Name

MIN() – This function returns the minimum value of the selected column.

Syntax – SELECT MIN(Column_Name) FROM Table_Name

SUM() – This function returns aggregated sum value of the selected column.

Syntax – SELECT SUM(Column_Name) FROM Table_Name

LEG() – Lead and Leg are called WINDOW functions as well, are used to get preceding and succeeding value of any row within its partition. The LAG() function is used to get value from the row that precedes the current row.

          Syntax – Select LAG(sale_value) OVER(ORDER BY sale_value) as previous_sale_value From SalesTable

LEAD()The LEAD() function is used to get value from row that succedes the current row.

          Syntax Select LEAD(sale_value) OVER(ORDER BY sale_value) as Next_sale_value From SalesTable

RANK()Ranking functions return a ranking value for each row in a partition. 

Syntax – Select RANK() OVER(PARTITION BY Column_Name ORDER BY Column_Name DESC) Rank From Table_Name

ROW_NUMBER()This function to get a unique sequential number for each row in the specified data.

          Syntax – Select ROW_NUMBER() OVER(ORDER BY Column_Name) RowNumber From Table_Name

DENSE_RANK() – This function to specify a unique rank number within the partition as per the specified column value. 

Syntax – Select DENSE_RANK() OVER(ORDER BY Column_Name) DenseRank From Table_Name

DATEDIFF() – This function returns the difference between two dates based on argument value for Day, Month, and Year.

          Syntax – SELECT DATEDIFF(YEAR, ‘01/01/2011’, ‘01/01/2021, )

DATEADD() – This Function adds a time/date interval to date and then returns the date.

          Syntax SELECT DATEADD(year, 1'01/01/2021’)

DATEPART() – This function returns an integer representing the specified datepart of the specified date.

          Syntax SELECT DATEPART(year, '01/01/2021’)

DATENAME() – This function returns a character string representing the specified datepart of the specified date

          Syntax – SELECT DATENAME(Month, '01/01/2021’)

GETDATE() – This function returns the current date and time of the system/server, in a 'YYYY-MM-DD hh:mm:ss.mmm' format.

          Syntax – SELECT GETDATE()

CURRENT_TIMESTAMP() – This Function also returns the current date and time, in a 'YYYY-MM-DD hh:mm:ss.mmm' format.

          Syntax – Select CURRENT_TIMESTAMP

FORMAT() – This function returns a value formatted with the specified format and optional culture, mostly used for date format.

          Syntax – SELECT FORMAT(‘01/01/2021’, 'd', 'en-US' ) 'US English' 

SYSTEM_USER – This Function is used to return the current username.

          Syntax – Select SYSTEM_USER

 

 

 

 

Types of Filters in Tableau

Types of Filters in Tableau

There are different types of filters in a tableau that can be used to organize data based on predefined conditions and use them for visualizing the data onto the dashboard and worksheet. The different types of filters used in Tableau are:

1. Extract Filters

2. Data Source Filters

3. Context Filters

4. Dimension Filters

5. Measure Filters

Extract Filters

This is the initial level filter in Tableau. The Extract filters in tableau create an extract of a small subset of data from the original data source or we can say that Extract filters are used to filter the extracted data from the data source. This filter is utilized only if the user extracts the data from the data source.

Data Source Filters:

As the name suggested, this filter applies to the data sources. This filter any important or sensitive information that we want to control while loading the data into Tableau. It works on both the Live and Extract connections. We can add the data source filter on any column by clicking on the ADD option.

Context Filters

Context filter is an independent filter that creates a different worksheet out of the original dataset and computes the calculation in the filtered dataset. To add a filter as a context in your Tableau sheet, right-click on the field value in the filter pane and click on the Add to Context option. if we want to remove any existing Context filter, then we can remove the context from the same drop-down box.

Dimension Filters

When we apply a filter on the dimension it is called a dimension filter. The dimension filters are the filters that we apply to individual dimensions. Dimensions are not aggregated and so, we can select distinct values from the list of field values. To apply the dimension filter, drag a dimension from the list of dimensions to the filter pane. Complete the filtering steps and apply the filter. The dimension filters are shown in blue in the filter pane.

Measure Filters

Like dimension filter we can also apply the filter on the measures. The measure filters are the filters that we apply using the measure field values. Measure values are always aggregated like sum, average, median, etc. To apply a measure filter, drag a measure field from the measures list to the filter pane. It will ask you to select an aggregation type on the filter value.

 

Tableau interview questions and answers for experienced professionals

  Tableau Interview Questions and Answers for experienced professional 1. What is TABLEAU? Tableau  is the powerful and fastest visualizing ...