SQL – AppDividend https://appdividend.com Latest Code Tutorials Tue, 26 Nov 2019 11:51:26 +0000 en-US hourly 1 https://appdividend.com/wp-content/uploads/2017/08/cropped-ApDivi-32x32.png SQL – AppDividend https://appdividend.com 32 32 SQL CURRENT_TIME Function Example https://appdividend.com/2019/11/26/sql-current_time-function-example/ https://appdividend.com/2019/11/26/sql-current_time-function-example/#respond Tue, 26 Nov 2019 11:49:35 +0000 http://localhost/wordpress/?p=12798 SQL CURRENT_TIME Function Example

SQL CURRENT_TIME function is used for returning the current time of the database server. The time will be returned either as “HH-MM-SS” (string) or as HHMMSS.uuuuuu (numeric). SQL CURRENT_TIME Function The CURRENT_TIME() function returns the current time in the database. The CURRENT_TIME is a standard-SQL function supported by almost all database systems such as DB2, […]

The post SQL CURRENT_TIME Function Example appeared first on AppDividend.

]]>
SQL CURRENT_TIME Function Example

SQL CURRENT_TIME function is used for returning the current time of the database server. The time will be returned either as “HH-MM-SS” (string) or as HHMMSS.uuuuuu (numeric).

SQL CURRENT_TIME Function

The CURRENT_TIME() function returns the current time in the database.

The CURRENT_TIME is a standard-SQL function supported by almost all database systems such as DB2, Firebird, MySQL, PostgreSQL, and SQLite.

SQL Server does not support the CURRENT_TIME function; however, you can use the GET_DATE() function to return the current time.

See the following syntax.

Syntax

SELECT CURRENT_TIME();

See the following example.

Example

SELECT CURRENT_TIME();

Output

22:22:23

Query 2

SELECT CURRENT_TIME () +1

Output

222223

Explanation

Adding 1 to this function, it returned the value in numeric form.

NOTE:

Above CURRENT_TIME function applies only to MYSQL.

To use it on the SQL_SERVER getdate () function has to be used.

Syntax

SELECT CONVERT (TIME, GETDATE ()) AS 'CURRENT_TIME using GETDATE()';

Output

CURRENT_TIME using GETDATE()
22:43:56.2500000

 

Oracle does not have a specific function to return the current time. However, you can use the SYSDATE function that returns both date and time and uses the TO_CHAR() function to format the time.

See the following query.

SELECT 
    TO_CHAR(SYSDATE, 'HH24:MI:SS') 
FROM 
    dual;

Output

17:46:28

In this tutorial, you have learned how to use the SQL CURRENT_TIME function to get the current time of the database server.

The return value does not change during the transaction. Thus, multiple calls to CURRENT_TIME() within the same transaction return the same timestamp.

MySQL CURRENT_TIME() function

In MySQL, the CURRENT_TIME() returns the current time in ‘HH:MM:SS’ format or HHMMSS.uuuuuu format depending on whether numeric or string is used in the function. CURRENT_TIME() and CURRENT_TIME are the synonyms of CURTIME().

Conclusion

If you want to get the current time from the database server, then you can use the CURRENT_TIME() function.

Finally, SQL CURRENT_TIME Function Example is over.

Related Posts

SQL GETUTCDATE Function

SQL GETDATE Function

SQL SYSDATETIME Function

SQL Format Function

SQL Date Functions

The post SQL CURRENT_TIME Function Example appeared first on AppDividend.

]]>
https://appdividend.com/2019/11/26/sql-current_time-function-example/feed/ 0
SQL GETUTCDATE Function Example https://appdividend.com/2019/11/26/sql-getutcdate-function-example/ https://appdividend.com/2019/11/26/sql-getutcdate-function-example/#respond Tue, 26 Nov 2019 10:52:03 +0000 http://localhost/wordpress/?p=12791 SQL GETUTCDATE Function Example

SQL GETUTCDATE function is used for returning the current database date and time in UTC format, i.e. in a ‘YYYY-MM-DD hh:mm:ss.mmm’ format. The GETUTCDATE() function returns the current UTC. The GETUTCDATE() function calculates this value from the operating system on which the SQL server is running. SQL GETUTCDATE Function SQL GETUTCDATE() function returns the current […]

The post SQL GETUTCDATE Function Example appeared first on AppDividend.

]]>
SQL GETUTCDATE Function Example

SQL GETUTCDATE function is used for returning the current database date and time in UTC format, i.e. in a ‘YYYY-MM-DD hh:mm:ss.mmm’ format. The GETUTCDATE() function returns the current UTC. The GETUTCDATE() function calculates this value from the operating system on which the SQL server is running.

SQL GETUTCDATE Function

SQL GETUTCDATE() function returns the current database system UTC date and time, in a ‘YYYY-MM-DD hh:mm:ss.mmm’ format. 

Syntax

SELECT GETUTCDATE();

Parameters

GETUTCDATE: This function will only return the current database date and time.

NOTE:
  1. The operating system settings determine the UTC date and time.
  2. GETUTCDATE() can also be used to store the timestamp that is independent of Time Zones.

Example

SELECT GETUTCDATE();

Output

2019-11-10 06:51:26.703

For a better understanding, see this example.

See the following code.

DECLARE @local_time DATETIME;
DECLARE @gmt_time DATETIME;
SET @local_time = GETDATE();
SET @gmt_time = GETUTCDATE();
SELECT 'Server local time: '
   + CONVERT(VARCHAR(40),@local_time);
SELECT 'Server GMT time: '
   + CONVERT(VARCHAR(40),@gmt_time);
SELECT 'Server time zone: '
   + CONVERT(VARCHAR(40),
      DATEDIFF(hour,@gmt_time,@local_time));
GO

Output

Server local time: Nov 10 2019 10:06PM
Server GMT time: Nov 10 2019 4:21PM
Server time zone: 6

DIFFERENCE between getdate() and getutcdate():

The main Difference between getdate() and getutcdate() is the time zone number of the SQL SERVER MACHINE.

SQL Server GETUTCDATE() example

The following example uses the GETDATE(), GETUTCDATE() and DATEDIFF() functions to return the local time, UTC, and server time zone.

DECLARE 
    @local_time DATETIME,
    @utc_time DATETIME;

SET @local_time = GETDATE();
SET @utc_time = GETUTCDATE();

SELECT
  CONVERT(VARCHAR(40), @local_time) 
        AS 'Server local time';
SELECT
  CONVERT(VARCHAR(40), @utc_time) 
        AS 'Server UTC time'
SELECT
  CONVERT(VARCHAR(40), DATEDIFF(hour, @utc_time, @local_time)) 
        AS 'Server time zone';
GO

Conclusion

If you want to get the current database date and time in UTC format, then you can use the SQL GETUTCDATE() function.

Related Posts

SQL GETDATE Function

SQL SYSDATETIME Function

SQL Format Function

SQL Date Functions

SQL Datatypes Example

The post SQL GETUTCDATE Function Example appeared first on AppDividend.

]]>
https://appdividend.com/2019/11/26/sql-getutcdate-function-example/feed/ 0
SQL GETDATE Function Example https://appdividend.com/2019/11/26/sql-getdate-function-example/ https://appdividend.com/2019/11/26/sql-getdate-function-example/#respond Tue, 26 Nov 2019 09:33:58 +0000 http://localhost/wordpress/?p=12783 SQL GETDATE Function Example

SQL GETDATE function is used to return the current system TIMESTAMP as a DATETIME value without the database time zone offset. The value is derived from the Operating System of the server on which the instance of SQL Server is running at present. SQL GETDATE The GETDATE() function returns the current database system date and […]

The post SQL GETDATE Function Example appeared first on AppDividend.

]]>
SQL GETDATE Function Example

SQL GETDATE function is used to return the current system TIMESTAMP as a DATETIME value without the database time zone offset. The value is derived from the Operating System of the server on which the instance of SQL Server is running at present.

SQL GETDATE

The GETDATE() function returns the current database system date and time, in a ‘YYYY-MM-DD hh:mm:ss.mmm’ format.

SQL GETDATE() function returns the current system timestamp as the DATETIME value without the database time zone offset.

The DATETIME value is derived from the OS of the server on which the instance of SQL Server is running.

Syntax

SELECT GETDATE();

Note

  1. The function returns the system date and time in the format ‘yyyy-mm-dd hh:mi:ss.mmm’.
  2. It is a nondeterministic function; therefore, we cannot create an index for columns that reference this function in the Views.

SQL Server Version

SQL GETDATE() function can be used in the following versions of SQL Server (Transact-SQL):

SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005.

Example of GETDATE() Function

How to get current system date and time in SQL.

Query

SELECT GETDATE();

Output

2019-11-13 15:13:26.270

How to get the Current System Date in SQL example.

Query

SELECT CONVERT (DATE, GETDATE ()) AS CURRENT_DATE;

Output

2019-11-13

Here, we have used a CONVERT() function to convert the DATETIME value to a DATE.

We can also use the TRY_CONVERT() and CAST() functions to convert the result of the GETDATE() function to date replace the CONVERT function with TRY_CONVERT() and CAST().

Query

SELECT TRY_CONVERT (DATE, GETDATE ()), CAST (GETDATE () AS DATE);

Output

2019-11-13

How to get the Current System Time in SQL example.

Query

SELECT CONVERT (TIME, GETDATE ()), TRY_CONVERT (TIME, GETDATE ()), CAST (GETDATE () AS TIME);

Output

16:12:35.8666667

Here, we have used the CONVERT (), TRY_CONVERT (), or CAST () function to convert the result of the GETDATE () function to time.

SQL Server GETDATE() function to get the current system date

If we want to get the current date, you can use the CONVERT() function to convert the DATETIME value to a DATE as follows.

Query

SELECT 
    CONVERT(DATE, GETDATE()) [Current Date];

Output

2019-11-26

Similarly, you can use the TRY_CONVERT() and CAST() functions to convert the result of the GETDATE() function to a date:

Query

SELECT 
    TRY_CONVERT(DATE, GETDATE()),
    CAST(GETDATE() AS DATE);

SQL Server GETDATE() function to get the current system time

If we need to get the current time only, you can use the CONVERT(), TRY_CONVERT(), or CAST() functions to convert the result of the GETDATE() function to a time:

Query

SELECT 
    CONVERT(TIME,GETDATE()),
    TRY_CONVERT(TIME, GETDATE()),
    CAST(GETDATE() AS TIME);

Conclusion

If you want to get the current date and timestamp without any local zone or settings, then you can use the SQL GETDATE() function.

Finally, SQL GETDATE Function Example is over.

Related Posts

SQL SYSDATETIME Function

SQL Format Function

SQL Date Functions

SQL Translate Function

SQL Datatypes Example

The post SQL GETDATE Function Example appeared first on AppDividend.

]]>
https://appdividend.com/2019/11/26/sql-getdate-function-example/feed/ 0
SQL SYSDATETIME Function Example https://appdividend.com/2019/11/26/sql-sysdatetime-function-example/ https://appdividend.com/2019/11/26/sql-sysdatetime-function-example/#respond Tue, 26 Nov 2019 08:50:26 +0000 http://localhost/wordpress/?p=12771 SQL SYSDATETIME Function Example

SQL SYSDATETIME function is a SQL Date Function, which is used to return the Current Date and Time of the system, i.e. DATETIME2, on which the SQL Server instance is running. The SYSDATETIME() function has more fractional seconds precision than the GETDATE() function. The SYSDATETIME() is the nondeterministic function; therefore, views and columns that have […]

The post SQL SYSDATETIME Function Example appeared first on AppDividend.

]]>
SQL SYSDATETIME Function Example

SQL SYSDATETIME function is a SQL Date Function, which is used to return the Current Date and Time of the system, i.e. DATETIME2, on which the SQL Server instance is running. The SYSDATETIME() function has more fractional seconds precision than the GETDATE() function.

The SYSDATETIME() is the nondeterministic function; therefore, views and columns that have expressions reference this function cannot be indexed.

SQL SYSDATETIME Function

SQL SYSDATETIME function is a SQL Date Function, which is used to return the Current Date and Time of a computer on which the SQL Server instance is running. 

The SYSDATETIME() function returns a current date and time as a datetime2(7) value.

This value is derived from the OS of the computer that the instance of the SQL Server is running on.

This post provides examples of the SYSDATETIME() function.

See the following syntax.

SELECT SYSDATETIME();

NOTE:

The function is similar to the GETDATE() function having some differences.

  1. SYSDATETIME returns the fractional seconds precision up to 7, whereas the GETDATE returns the fractional seconds precision up to 3.
  2. SYSDATETIME returns datetime2 as the data type, whereas the GETDATE returns DateTime as the data type. 

The function is a non-deterministic type; therefore, views and columns that have expressions reference this function cannot be indexed.

Examples:

See the following query.

SELECT SYSDATETIME () AS CURRENT_DATE;

Output:

CURRENT_DATE
2019-11-25 14:55:33.3975855

 

I am using the CONVERT() function to convert the SYSDATETIME function to the Current Date.

SELECT CONVERT (DATE, SYSDATETIME ());

Output

2019-11-25

Using CONVERT () function to convert SYSDATETIME function to Current Time.

Query

SELECT CONVERT (TIME, SYSDATETIME());

Output

15:08:33.3975855

Extract a part of the date

If you only want the part of the return value, you can use the DATEPART() to return only that part of the date/time that you’re interested in.

See the following query.

SELECT DATEPART(month, SYSDATETIME()) AS Result;

Output

+----------+
| Result   |
|----------|
| 11       |
+----------+

Sometimes there’s more than one way to get the same result in SQL Server. Here’s another example using the MONTH() function.

SELECT MONTH(SYSDATETIME()) AS Result;

Output

+----------+
| Result   |
|----------|
| 11       |
+----------+

Conclusion

If you want to get Current Date and Time of the system, i.e. DATETIME2 on which the SQL Server instance is running, then you can use SQL SYSDATETIME() Function.

Finally, SQL SYSDATETIME Function Example is over.

Related Posts

SQL Format Function

SQL Replace Function

SQL Translate Function

SQL Datatypes Example

SQL Substring Function

The post SQL SYSDATETIME Function Example appeared first on AppDividend.

]]>
https://appdividend.com/2019/11/26/sql-sysdatetime-function-example/feed/ 0
SQL SOUNDEX Function Example https://appdividend.com/2019/11/18/sql-soundex-function-example/ https://appdividend.com/2019/11/18/sql-soundex-function-example/#respond Mon, 18 Nov 2019 09:46:02 +0000 http://localhost/wordpress/?p=12453 SQL SOUNDEX Function Example

SQL SOUNDEX function accepts the string and returns a phonetic expression, i.e., converts it into four-character code based on how the string sounds when spoken.  The SOUNDEX() function returns the four-character code to evaluate the similarity of two expressions. SQL SOUNDEX() converts the string to the four-character code based on how the string sounds when spoken. SQL […]

The post SQL SOUNDEX Function Example appeared first on AppDividend.

]]>
SQL SOUNDEX Function Example

SQL SOUNDEX function accepts the string and returns a phonetic expression, i.e., converts it into four-character code based on how the string sounds when spoken.  The SOUNDEX() function returns the four-character code to evaluate the similarity of two expressions. SQL SOUNDEX() converts the string to the four-character code based on how the string sounds when spoken.

SQL SOUNDEX Function

See the following syntax.

SELECT SOUNDEX (String);

Parameters

String: The string whose character code has to be retrieved.

Note

  1. The SOUNDEX function returned value will always begin with the first letter of String.
  2. The SOUNDEX function uses only the first five consonants to determine the NUMERIC portion of the value returned, except if the first letter of String is a vowel.
  3. The SOUNDEX function is not case-sensitive, i.e. both uppercase, and lowercase characters will generate the same SOUNDEX function return value.

The first character of the SOUNDEX code is always the first character of an input string.

The second to fourth characters of the code are the numbers that represent the letter in an input string.

The letters A, E, I, O, U, H, W, and Y are ignored if they are not the first letters of a string.

SQL SOUNDEX() function will add zeros at the end of the result code if necessary to make the four-character code.

The primary purpose of the SOUNDEX() function is to compare the similarity between strings in terms of their sounds.

We can check the similarity between SOUNDEX codes of two strings, you use the DIFFERENCE() function.

Example

SELECT SOUNDEX('AppDividend');

Output

A131353

Query 2

SELECT SOUNDEX ('APP DIVIDEND');

Output

A131353

Query 3

SELECT SOUNDEX ('SQL');

Output

S400

Query 4

SELECT SOUNDEX ('SQL IS EASY')

Output

S420

Query 5

SELECT SOUNDEX ('PEACE') AS PEACE, SOUNDEX ('PIECE') AS PIECE;

Output

PEACE PIECE
P200 P200

 

Both the strings sound the same, so their Soundex values are the same.

Query 6

SELECT SOUNDEX ('I') AS I, SOUNDEX ('EYE') AS EYE;

Output

I EYE
I000 E000

 

Both the strings sound the same but having different Soundex values because they are from different character families.

Let’s apply the Soundex function in a Table.

Table: Employee

Emp_id Emp_name City State Salary
101 Rohit Patna Bihar 30000
201 Roheet Jalandhar Punjab 20000
301 Karan Allahabad Uttar Pradesh 40000
401 Suraj Kolkata West Bengal 60000
501 Sooraj Vizag Andhra Pradesh 70000

 

Let’s apply the Soundex function on Emp_name to check different Soundex values.

Query

Select Emp_name, SOUNDEX (Emp_name) AS SOUNDEX_VALUE 
from Employee;

Output

Emp_name SOUNDEX_VALUE
Rohit R300
Roheet R300
Karan K650
Suraj S620
Sooraj S620

 

Here, you can see that Soundex values of Emp_name are displayed having some common values whose names sound the same.

Finally, SQL SOUNDEX Function Example is over.

Related Posts

SQL Right Function

SQL STR Function

SQL Space Function

SQL Rtrim Function

SQL Reverse Function

The post SQL SOUNDEX Function Example appeared first on AppDividend.

]]>
https://appdividend.com/2019/11/18/sql-soundex-function-example/feed/ 0
SQL Right Function Example https://appdividend.com/2019/11/18/sql-right-function-example/ https://appdividend.com/2019/11/18/sql-right-function-example/#respond Mon, 18 Nov 2019 09:06:53 +0000 http://localhost/wordpress/?p=12445 SQL Right Function Example

SQL Right Function is used for extracting a substring starting from the right, followed by the number of characters. The RIGHT() function extracts several characters from a string (starting from right). SQL RIGHT() function extracts the given number of characters from the right side of the specified character string. So, if you are searching for […]

The post SQL Right Function Example appeared first on AppDividend.

]]>
SQL Right Function Example

SQL Right Function is used for extracting a substring starting from the right, followed by the number of characters. The RIGHT() function extracts several characters from a string (starting from right). SQL RIGHT() function extracts the given number of characters from the right side of the specified character string.

So, if you are searching for a substring of the right part of the main string, then you can use the RIGHT() function.  It accepts two parameters the string and no. of characters we are looking for in the string.

SQL Right Function

See the syntax of the SQL Right function.

SELECT RIGHT (string, no_of_characters);

The function accepts two parameters.

Parameters

  1. String: The input string from which the substring will be extracted and can be a literal string, variable, or column.
  2. No_of_characters: The number of characters you wish to extract from the string.

The RIGHT() function returns the value of VARCHAR when the input_string is non-Unicode character data type and returns an NVARCHAR if the input_string is a Unicode character data type.

Query 1

SELECT RIGHT ('AppDividend.com', 3);

Output

'com'

Here 3 characters were displayed starting from the rightmost part of the string.

Query 2

SELECT RIGHT ('AppDividend.com', 4);

Output

'.com'

Here 4 characters were displayed starting from the rightmost part of the string.

Query 3

SELECT RIGHT ('SQL IS FUN', 4);

Output

' FUN'

Here 3 characters were displayed starting from the rightmost part of the string. Before FUN, there is whitespace, which is also counted as a character.

Query 4

SELECT RIGHT ('SQL IS FUN', 20);

Output

'SQL IS FUN'

Here the number of characters exceeded the length of the string as a whole result string is displayed as an output.

Let’s apply the RIGHT function in a Table.

Table: Employee

Emp_id First_name City State Phone
101 Rohit Patna Bihar 8585145852
201 Shivam Jalandhar Punjab 8958458785
301 Karan Allahabad Uttar Pradesh 9987845784
401 Suraj Kolkata West Bengal 8878989898
501 Akash Vizag Andhra Pradesh 9898547500

 

Now if we want to display the last 7 digits of the phone, then the following query has to be written:

Query

Select First_name AS Emp_Name, RIGHT (Phone, 7) AS Last_seven_digit 
from Employee;

Output

Emp_Name Last_seven_digit
Rohit 5145852
Shivam 8458785
Karan 7845784
Suraj 8989898
Akash 8547500

 

So, you can see that the last seven-digit of phone number is displayed under the column named Phone.

Finally, SQL Right Function Example is over.

Related Posts

SQL STR Function

SQL Space Function

SQL Rtrim Function

SQL Reverse Function

SQL Translate Function

The post SQL Right Function Example appeared first on AppDividend.

]]>
https://appdividend.com/2019/11/18/sql-right-function-example/feed/ 0
SQL STR Function Example https://appdividend.com/2019/11/18/sql-str-function-example/ https://appdividend.com/2019/11/18/sql-str-function-example/#respond Mon, 18 Nov 2019 06:26:29 +0000 http://localhost/wordpress/?p=12437 SQL STR Function Example

SQL STR function is used for converting numeric value to a character value. In other words, it is used for returning a string representation of a number. The STR() function returns the number as a string. If there is not enough length or decimal to display the resulting string based on the parameters provided then […]

The post SQL STR Function Example appeared first on AppDividend.

]]>
SQL STR Function Example

SQL STR function is used for converting numeric value to a character value. In other words, it is used for returning a string representation of a number. The STR() function returns the number as a string. If there is not enough length or decimal to display the resulting string based on the parameters provided then the function will round off the result.

SQL STR Function

The syntax for the STR() function is below.

STR(number, length, decimals)

Parameters:

Number: The numeric value which is to be converted to a string.

Length: The length of the string which includes all digits, decimals, and signs and as it is completely optional so by default length of the resulting string will be 10.

Decimal: Number of places to the right of the decimal point of the number to be returned. It must be less than or equal to 16 and if it is bigger than 16, then the function truncates the result to sixteen places to the right of the decimal point.

If supplied, the values for length and decimal parameters to STR should be positive.

The number is rounded to an integer by default or if the decimal parameter is 0.

The specified length should be greater than or equal to the part of the number before the decimal point plus the number’s sign (if any).

The short float_expression is right-justified in the specified length, and the long float_expression is truncated to a specified number of decimal places.

For example, STR(12**,10) yields the result of 12. This is right-justified in the result set. However, STR(1223,**2) truncates the result set to **. String functions can be nested.

Example 1

SELECT STR (123)

Output

123

Example 2

SELECT STR (123.5);

Output

124

Here, the decimal parameter was not provided. So, the result was rounded to 0.

Example 3

SELECT STR (123.5, 5);

Output

124

Example 4

SELECT STR (123.456, 8, 2);

Output

123.46   

Here, the result is rounded to 2 decimal places.

Example 5

SELECT STR (123.456, 7, 1);

Output

123.5

Here, the result is rounded to 1 decimal place.

Example 6

SELECT STR (987.65, 1);

Output

*

Here, the length of the number exceeded the specified_length. So, * was returned.

Finally, SQL STR Function Example is over.

Related Posts

SQL Space Function

SQL Rtrim Function

SQL Reverse Function

SQL Translate Function

SQL Upper Function

The post SQL STR Function Example appeared first on AppDividend.

]]>
https://appdividend.com/2019/11/18/sql-str-function-example/feed/ 0
SQL SPACE Function Example | SQL Server SPACE() https://appdividend.com/2019/11/08/sql-space-function-example-sql-server-space/ https://appdividend.com/2019/11/08/sql-space-function-example-sql-server-space/#respond Fri, 08 Nov 2019 09:49:32 +0000 http://localhost/wordpress/?p=12253 SQL SPACE Function Example | SQL Server SPACE()

SQL SPACE function is used for returning a repeated sequence of space, i.e. a string with a specified number of spaces. In SQL Server (Transact-SQL), the SPACE function returns a string with a specified number of spaces. SPACE is one of the SQL String Functions, which is used to reverse the specified expression. SQL SPACE […]

The post SQL SPACE Function Example | SQL Server SPACE() appeared first on AppDividend.

]]>
SQL SPACE Function Example | SQL Server SPACE()

SQL SPACE function is used for returning a repeated sequence of space, i.e. a string with a specified number of spaces. In SQL Server (Transact-SQL), the SPACE function returns a string with a specified number of spaces. SPACE is one of the SQL String Functions, which is used to reverse the specified expression.

SQL SPACE Function

SPACE() function returns the string of the specified number of space characters.

The way SQL SPACE() function works is, you provide a number of spaces you need, and it will return the string of precisely that number of spaces.

Syntax

See the following SQL SPACE() function.

SELECT SPACE (count);

Parameters

Count: A positive integer that is used for specifying the number of spaces, and if the count is negative, then NULL will be returned.

Query 1

SELECT SPACE (4);

Output

 '    '

Query 2

SELECT SPACE (1);

Output

' '

Concatenation with Spaces

See the following query.

SELECT 'Space' + SPACE (1) + 'Function' + SPACE (1) + 'Tutorial';

Output

Space Function Tutorial

Negative Integer Value

The argument needs to be the positive value. If it’s the negative value, the result is NULL.

See the following query.

SELECT SPACE(-11) AS Result;

See the output.

+----------+
| Result   |
|----------|
| NULL     |
+----------+

 

Above were the examples to make clear how space function works. Let’s apply it to a table.

Consider table the Employee:

Emp_id First_name Last_name City State Salary
101 Rohit Raj Patna Bihar 30000
201 Shivam Rana Jalandhar Punjab 20000
301 Karan Kumar Allahabad Uttar Pradesh 40000
401 Suraj Bhakat Kolkata West Bengal 60000
501 Akash Cherukuri Vizag Andhra Pradesh 70000

 

Suppose if we want to display first_name and last_name under a single column named full_name separated by a space, then the following query has to be considered.

Query:

Select first_name + SPACE (1) + last_name AS Full_name from Employee;

Output

Full_name
Rohit Raj
Shivam Rana
Karan Kumar
Suraj Bhakat
Akash Cherukuri

 

Here, you can see that the full_name of every employee is displayed under a single column named Full_name.

A SPACE function can be used in the following versions of SQL Server (Transact-SQL):

SQL Server 2017, SQL Server 2016, SQL Server 2014, SQL Server 2012, SQL Server 2008 R2, SQL Server 2008, SQL Server 2005.

What About 1 or 2 Spaces

This is an excellent function for adding lots of spaces, but it can also improve the code readability when including the small number of spaces – mainly if the code contains many instances of adding spaces.

When using the SPACE(), you can see how many spaces in the instant, without having to count them.

For example, see if you can tell how many spaces are in the following:

SELECT 'App' + '  ' + 'dividend!';

Your first reaction might be the quick guess (say, “2 or 3”), before looking the little closer to check.

If you want to be 100% sure, you’d probably have to run your cursor over the space to count the number of spaces.

Now look at the following code:

SELECT 'App' + SPACE(2) + 'dividend!';

No need to guess. You can see the 2 written right into the code.

The value could be within the variable so that your code might look more like this:

SELECT 'App' + SPACE(@space_size) + 'dividend!';

Which obviously, provides no clue as to how many spaces there are without having to look it up.

However, if you already know the value of @space_size, you’re good to go.

Finally, SQL SPACE Function Example | SQL Server SPACE() Tutorial is over.

Recommended Posts

SQL REVERSE function

SQL RTRIM function

SQL LTRIM function

SQL TRANSLATE function

SQL Replicate function

The post SQL SPACE Function Example | SQL Server SPACE() appeared first on AppDividend.

]]>
https://appdividend.com/2019/11/08/sql-space-function-example-sql-server-space/feed/ 0
SQL RTRIM Function Example | SQL Server RTRIM() https://appdividend.com/2019/11/08/sql-rtrim-function-example-sql-server-rtrim/ https://appdividend.com/2019/11/08/sql-rtrim-function-example-sql-server-rtrim/#respond Fri, 08 Nov 2019 08:55:54 +0000 http://localhost/wordpress/?p=12241 SQL RTRIM Function Example | SQL Server RTRIM()

SQL RTRIM function is used for removing all the leading whitespaces or specified characters from a string, i.e., from the left-hand side of the string. SQL RTRIM is one of the SQL String Function, which is used to remove the empty spaces from the Right-hand side of the character expression. SQL RTRIM Function SQL RTRIM Function […]

The post SQL RTRIM Function Example | SQL Server RTRIM() appeared first on AppDividend.

]]>
SQL RTRIM Function Example | SQL Server RTRIM()

SQL RTRIM function is used for removing all the leading whitespaces or specified characters from a string, i.e., from the left-hand side of the string.

SQL RTRIM is one of the SQL String Function, which is used to remove the empty spaces from the Right-hand side of the character expression.

SQL RTRIM Function

SQL RTRIM Function is used to return the character expression after removing the empty spaces from the right side of the given expression.

Syntax

SELECT RTRIM (input_string, [trim_string])

Parameters

Input_string: It is the string from where the characters have to be removed from the right-hand side.

Trim_string: It is entirely optional. It is mainly used for specifying the characters to be removed. If this parameter is omitted, then all the leading spaces will be removed.

Example

SELECT RTRIM (‘SQL     ’);

Output

SQL

Here all the trailing spaces are removed from the string.

Query 2

SELECT RTRIM (‘1232000’, ‘0’);

Output

1232

As Trim_string was specified as 0. So, all trailing 0 was removed from the string.

Query 3

SELECT RTRIM (‘00012300’, ‘0’);

Output

000123

As Trim_string was specified as 0. So, all trailing 0 were removed from the string but not the leading zeroes because the function used here was RTRIM, which removes only the leading zeroes.

Query 4

SELECT RTRIM (‘AppDividend2145’, ‘012345’);

Output

AppDividend

In the above example, all combinations of 0 to 5 are listed. By doing this, all the orders of the trailing numbers from the string will be removed.

Query 5

SELECT RTRIM (‘aabbabcSQLabc’, ‘abc’);

Output

aabbabcSQL

In the above example, ‘abc’ is declared as Trim_string. So, ‘a’, ‘b’, ‘c’ is considered as an individual character. So, all trailing characters of ‘abc’ are removed from the string.

Above were the common examples to make you clear how RTRIM function works. Let’s apply this function to a table.

Table: Employee

Emp_id Emp_name City State Salary
101# Rohit Patna Bihar 30000
201# Shivam Jalandhar Punjab 20000
301# Karan Allahabad Uttar Pradesh 40000
401# Suraj Kolkata West Bengal 60000
501# Akash Vizag Andhra Pradesh 70000

 

Previously, in the employee table, ‘#’ was added after the emp_id. Now we want to remove this character from the emp_id and display only the number. So, to do this following query has to be written.

QUERY

Select RTRIM (Emp_id, ‘#’) AS Emp_id, Emp_name, City, State, Salary 
from Employee;

Output

Emp_id Emp_name City State Salary
101 Rohit Patna Bihar 30000
201 Shivam Jalandhar Punjab 20000
301 Karan Allahabad Uttar Pradesh 40000
401 Suraj Kolkata West Bengal 60000
501 Akash Vizag Andhra Pradesh 70000

 

Here, you can see that the new table is created by removing trailing ‘#’ special character from the Emp_id.

SQL LTRIM function is used for removing all the leading whitespaces or specified characters from the string, i.e., from the left-hand side of the string.

SQL Server RTRIM() function

SQL Server RTRIM() function returns the string after truncating all trailing blanks.

The following is the syntax of the RTRIM() function.

RTRIM(input_string)

In this syntax, the input_string is the expression of character or binary data.

It can be the literal string, variable, or column.

The input_string must evaluate to the value of the data type, except for TEXT, NTEXT, and IMAGE, that can be implicitly convertible to VARCHAR. Otherwise, you must use the CAST() function to convert it to the character string explicitly.

Recommended Posts

SQL REVERSE Function

SQL LTRIM Function

SQL TRANSLATE Function

SQL Replicate Function

SQL UPPER Function

The post SQL RTRIM Function Example | SQL Server RTRIM() appeared first on AppDividend.

]]>
https://appdividend.com/2019/11/08/sql-rtrim-function-example-sql-server-rtrim/feed/ 0
SQL REVERSE Function Example | SQL Server REVERSE() https://appdividend.com/2019/11/08/sql-reverse-function-example-sql-server-reverse/ https://appdividend.com/2019/11/08/sql-reverse-function-example-sql-server-reverse/#respond Fri, 08 Nov 2019 06:49:26 +0000 http://localhost/wordpress/?p=12233 SQL REVERSE Function Example | SQL Server REVERSE()

SQL REVERSE function is used for reversing the string. It accepts a string of characters as an argument and returns the reverse order of the string. The REVERSE is one of the SQL String Functions, which is used to reverse the specified expression. The REVERSE() function reverses a string and returns the result. SQL REVERSE […]

The post SQL REVERSE Function Example | SQL Server REVERSE() appeared first on AppDividend.

]]>
SQL REVERSE Function Example | SQL Server REVERSE()

SQL REVERSE function is used for reversing the string. It accepts a string of characters as an argument and returns the reverse order of the string. The REVERSE is one of the SQL String Functions, which is used to reverse the specified expression. The REVERSE() function reverses a string and returns the result.

SQL REVERSE Function

The REVERSE function is used to reverse the given string.

See the following syntax of the REVERSE function.

Syntax

SELECT REVERSE(string)

Parameters

String: It is the source string of which characters have to be reversed.

Note

The string expression must be of a data type that can be implicitly converted to VARCHAR. Otherwise, CAST has to be used explicitly for converting to VARCHAR.

Example

See the following query.

SELECT REVERSE ('AppDividend.com');

Output

moc.dendiviDppA

Query 2

SELECT REVERSE ('edcba');

Output

abcde

Query 3

SELECT REVERSE (' SQL');

Output

LQS

Query 4

SELECT REVERSE ('123');

Output

321

Above were the common examples to make clear how this function work. Let’s apply this function to a TABLE.

Table: Employee

Emp_id First_name City State Phone
101 Rohit Patna Bihar 8585145852
201 Shivam Jalandhar Punjab 8958458785
301 Karan Allahabad Uttar Pradesh 9987845784
401 Suraj Kolkata West Bengal 88789898980
501 Akash Vizag Andhra Pradesh 98985475001

 

Suppose we want to reverse the first_name of the employee, then the following query has to be considered.

Query

Select First_name, REVERSE (First_name) 
AS Reversed_Name 
from Employee;

Output

First_name Reversed_Name
Rohit tihoR
Shivam mavihS
Karan naraK
Suraj jaruS
Akash hsakA

 

Here, you can see that the First_name of the employee has been reversed.

SQL SERVER Reverse Function

Let’s use SQL SERVER REVERSE function to check whether a string is a palindrome or not.

A palindrome string is a string whose original and reversed strings are equal.

EX: MaM, DaD

Query

DECLARE 
@input VARCHAR (100) = 'MadaM';
SELECT 
CASE
WHEN @input = REVERSE(@input)
THEN 'Palindrome'
ELSE 'Not Palindrome'
END result;

Output

Palindrome

Explanation

Here, above string MadaM, which was given as input, was a palindrome.

MySQL REVERSE() Function

MySQL REVERSE() function reverses a string and returns the result.

It works from MySQL 4.0.

Finally, SQL REVERSE Function Example | SQL Server REVERSE() Function Tutorial is over.

Recommended Posts

SQL TRANSLATE Function

SQL Replicate Function

SQL LTRIM Function

SQL UPPER Function

SQL UNICODE Function

The post SQL REVERSE Function Example | SQL Server REVERSE() appeared first on AppDividend.

]]>
https://appdividend.com/2019/11/08/sql-reverse-function-example-sql-server-reverse/feed/ 0