Go Back   CodeCall Programming Forum > Software Development > Tutorials
Register Blogs Search Today's Posts Mark Forums Read

Tutorials Programming Tutorials - Post your tutorials here!

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-02-2009, 07:39 PM
chili5's Avatar
Code Slinger
 
Join Date: Mar 2008
Posts: 7,018
chili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond repute
SQL: String Functions

SQL Functions - String Functions

SQL provides a large amount of functions for string manipulations.

ASCII

The ASCII function is used to return a numeric value for the leftmost character. Each character has a numeric value that represents it.

Example:

Code:
SELECT ASCII('A')
Output:

Quote:
65
65 is the numeric representation. An interesting property of characters is that each letter is one integer value away. This makes it great for adding and subtracting other values. You can perform math with characters.

So:

Code:
SELECT CHAR(ASCII('A')+1)
Returns B. This converts A to it's ascii value and adds 1. Then it converts it back to a character. This shows that the letters are exactly one integer value away.

Thus we can use this method to convert chars from upper to lowercase by observing patterns in an ASCII chart.

Code:
SELECT CHAR(ASCII('A')+ASCII('a')-ASCII('A'))
This code converts A to lowercase "a".

Character Length

The character length function returns the number of letters in the string.

Code:
SELECT CHARACTER_LENGTH("Hiii")
This function returns the number of characters in the string.

Output:

Quote:
4
Another method that does the same thing is CHAR_LENGTH.

CONCAT method

This method is used to join strings together.

Code:
SELECT CONCAT("James","Hi","There");
The function takes a variable number of arguments and joins them together.

The returning value is:

Quote:
Jame****here
Format Function

Returns a number formatted to a certain amount of decimals. The function prototype is:

Quote:
FORMAT(num,D)
The number num is formatted in a format similar to '#,###.##'. The number of decimals is D values.

Example:

Code:
SELECT FORMAT(1234.343332,3)
Output:

Quote:
1,234.343
Lowercase Functions

The function LCASE returns a string in lower case. This function does the same thing as the LOWER function.

Example:

Code:
SELECT LOWER("Test");
Output:

Quote:
test
Trim Functions

There are three functions:
  • LTRIM
  • RTRIM
  • TRIM

that remove the space from around the string. The LTRIM function removes all leading spaces before the left side of the string. The RTRIM function removes all trailing spaces after the right side of the string. The TRIM function removes all leading and trailing spaces.

Examples:

Code:
SELECT LTRIM("      Test");
SELECT RTRIM("Test       ");
SELECT TRIM("       Test      ");
SELECT RTRIM("         Test          ");
Output:

Quote:
Test
Test
Test
Test
Replace

The generic function is:

Code:
REPLACE(str,replace,new)
The replace function replaces all instances of a string replace with new.

Example:

Code:
SELECT REPLACE("Test  this is just a test test test","test","yo!");
The output:

Quote:
Test this is just a yo! yo! yo!
All instances of "this" will be replaced with "yo". This method is case-sensitive. Notice, how Test was not replaced?

Reverse Function

This function returns a string that is reversed.

Example:

Code:
SELECT REVERSE("test");
The output is:

Quote:
tset
Substring Function

There are two versions of this function:

Code:
SUBSTR(str,pos)
and

Code:
SUBSTR(str,pos,len)
The first function copies all the characters from index pos to the end of the string. One thing to note, the index of the first character is one.

Example:

Code:
SELECT SUBSTR("test",2);
Output:

Quote:
est
The second version specifies how many characters to copy and where to start copying from.

Example:

Code:
SELECT SUBSTR("test",1,2);
This function starts at the first character and copies two characters.

Output:

Quote:
te
Uppercase Function

The UPPER function converts a string into upper case.

Example:

Code:
SELECT UPPER("test");
Output:

Quote:
TEST
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 09-03-2009, 08:07 AM
Jordan's Avatar
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 24,556
Jordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to allJordan is a name known to all
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan Send a message via Yahoo to Jordan
Re: SQL: String Functions

Nice little tutorial. +rep!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-03-2009, 08:52 AM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
Re: SQL: String Functions

Very handy list of functions. I use them a LOT. +rep
__________________
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL Functions - Math Functions chili5 Tutorials 6 09-02-2009 05:11 PM
String insertion in C as part of a string and pointer tutorial Yuriy M C and C++ 1 02-20-2009 12:53 PM
C++: Just some stuff.... void_3e01 Classes and Code Snippets 17 10-14-2008 12:54 PM
OOP DB Access Wrapper digioz C# Programming 6 09-07-2008 01:58 PM
SecurityAudit vinay Visual Basic Programming 27 01-07-2008 01:14 PM


All times are GMT -5. The time now is 11:39 PM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0