DateTimeFormatInfo Class
Defines how DateTime values are formatted and displayed, depending on the culture.
For a list of all members of this type, see DateTimeFormatInfo Members.
System.Object
System.Globalization.DateTimeFormatInfo
[Visual Basic] <Serializable> NotInheritable Public Class DateTimeFormatInfo Implements ICloneable, IFormatProvider [C#] [Serializable] public sealed class DateTimeFormatInfo : ICloneable, IFormatProvider [C++] [Serializable] public __gc __sealed class DateTimeFormatInfo : public ICloneable, IFormatProvider [JScript] public Serializable class DateTimeFormatInfo implements ICloneable, IFormatProvider
Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Remarks
This class contains information, such as date patterns, time patterns, and AM/PM designators.
To create a DateTimeFormatInfo for a specific culture, create a CultureInfo for that culture and retrieve the CultureInfo.DateTimeFormat property. To create a DateTimeFormatInfo for the culture of the current thread, use the CurrentInfo property. To create a DateTimeFormatInfo for the invariant culture, use the InvariantInfo property for a read-only version, or use the DateTimeFormatInfo constructor for a writable version. It is not possible to create a DateTimeFormatInfo for a neutral culture.
The user might choose to override some of the values associated with the current culture of Windows through Regional and Language Options (or Regional Options or Regional Settings) in Control Panel. For example, the user might choose to display the date in a different format or to use a currency other than the default for the culture. If the CultureInfo.UseUserOverride property is set to true, the properties of the CultureInfo.DateTimeFormat instance, the CultureInfo.NumberFormat instance, and the CultureInfo.TextInfo instance are also retrieved from the user settings. If the user settings are incompatible with the culture associated with the CultureInfo (for example, if the selected calendar is not one of the OptionalCalendars), the results of the methods and the values of the properties are undefined.
DateTime values are formatted using standard or custom patterns stored in the properties of a DateTimeFormatInfo.
The standard patterns can be replaced with custom patterns by setting the associated properties of a writable DateTimeFormatInfo. To determine if a DateTimeFormatInfo is writable, use the IsReadOnly property.
The following table lists the standard format characters for each standard pattern and the associated DateTimeFormatInfo property that can be set to modify the standard pattern. The format characters are case-sensitive; for example, ‘g’ and ‘G’ represent slightly different patterns.
| Format Character | Associated Property/ Description |
|---|---|
| d | ShortDatePattern |
| D | LongDatePattern |
| f | Full date and time (long date and short time) |
| F | FullDateTimePattern (long date and long time) |
| g | General (short date and short time) |
| G | General (short date and long time) |
| m, M | MonthDayPattern |
| r, R | RFC1123Pattern |
| s | SortableDateTimePattern (based on ISO 8601) using local time |
| t | ShortTimePattern |
| T | LongTimePattern |
| u | UniversalSortableDateTimePattern using the format for universal time display |
| U | Full date and time (long date and long time) using universal time |
| y, Y | YearMonthPattern |
The following table lists the patterns that can be combined to construct custom patterns. The patterns are case-sensitive; for example, “MM” is recognized, but “mm” is not. If the custom pattern contains white-space characters or characters enclosed in single quotation marks, the output string will also contain those characters. Characters not defined as part of a format pattern or as format characters are reproduced literally.
| Format Pattern | Description |
|---|---|
| d | The day of the month. Single-digit days will not have a leading zero. |
| dd | The day of the month. Single-digit days will have a leading zero. |
| ddd | The abbreviated name of the day of the week, as defined in AbbreviatedDayNames. |
| dddd | The full name of the day of the week, as defined in DayNames. |
| M | The numeric month. Single-digit months will not have a leading zero. |
| MM | The numeric month. Single-digit months will have a leading zero. |
| MMM | The abbreviated name of the month, as defined in AbbreviatedMonthNames. |
| MMMM | The full name of the month, as defined in MonthNames. |
| y | The year without the century. If the year without the century is less than 10, the year is displayed with no leading zero. |
| yy | The year without the century. If the year without the century is less than 10, the year is displayed with a leading zero. |
| yyyy | The year in four digits, including the century. |
| gg | The period or era. This pattern is ignored if the date to be formatted does not have an associated period or era string. |
| h | The hour in a 12-hour clock. Single-digit hours will not have a leading zero. |
| hh | The hour in a 12-hour clock. Single-digit hours will have a leading zero. |
| H | The hour in a 24-hour clock. Single-digit hours will not have a leading zero. |
| HH | The hour in a 24-hour clock. Single-digit hours will have a leading zero. |
| m | The minute. Single-digit minutes will not have a leading zero. |
| mm | The minute. Single-digit minutes will have a leading zero. |
| s | The second. Single-digit seconds will not have a leading zero. |
| ss | The second. Single-digit seconds will have a leading zero. |
| f | The fraction of a second in single-digit precision. The remaining digits are truncated. |
| ff | The fraction of a second in double-digit precision. The remaining digits are truncated. |
| fff | The fraction of a second in three-digit precision. The remaining digits are truncated. |
| ffff | The fraction of a second in four-digit precision. The remaining digits are truncated. |
| fffff | The fraction of a second in five-digit precision. The remaining digits are truncated. |
| ffffff | The fraction of a second in six-digit precision. The remaining digits are truncated. |
| fffffff | The fraction of a second in seven-digit precision. The remaining digits are truncated. |
| t | The first character in the AM/PM designator defined in AMDesignator or PMDesignator, if any. |
| tt | The AM/PM designator defined in AMDesignator or PMDesignator, if any. |
| z | The time zone offset (“+” or “-” followed by the hour only). Single-digit hours will not have a leading zero. For example, Pacific Standard Time is “-8″. |
| zz | The time zone offset (“+” or “-” followed by the hour only). Single-digit hours will have a leading zero. For example, Pacific Standard Time is “-08″. |
| zzz | The full time zone offset (“+” or “-” followed by the hour and minutes). Single-digit hours and minutes will have leading zeros. For example, Pacific Standard Time is “-08:00″. |
| : | The default time separator defined in TimeSeparator. |
| / | The default date separator defined in DateSeparator. |
| % c | Where c is a format pattern if used alone. The “%” character can be omitted if the format pattern is combined with literal characters or other format patterns. |
| \ c | Where c is any character. Displays the character literally. To display the backslash character, use “\\”. |
Only format patterns listed in the second table above can be used to create custom patterns; standard format characters listed in the first table cannot be used to create custom patterns. Custom patterns are at least two characters long; for example,
- DateTime.ToString( “d”) returns the DateTime value; “d” is the standard short date pattern.
- DateTime.ToString( “%d”) returns the day of the month; “%d” is a custom pattern.
- DateTime.ToString( “d “) returns the day of the month followed by a white-space character; “d ” is a custom pattern.
A DateTimeFormatInfo or a NumberFormatInfo can be created only for the invariant culture or for specific cultures, not for neutral cultures. For more information about the invariant culture, specific cultures, and neutral cultures, see the CultureInfo class.
This class implements the ICloneable interface to enable duplication of DateTimeFormatInfo objects. It also implements IFormatProvider to supply formatting information to applications.
Example
[Visual Basic, C#, C++] The following code example prints out the different format patterns for the en-US culture using the format characters. It also displays the value of the properties associated with the format character.
[Visual Basic]
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic
Public Class SamplesDTFI
Public Shared Sub Main()
' Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
Dim myDTFI As DateTimeFormatInfo = New CultureInfo("en-US", False).DateTimeFormat
' Creates a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3).
' The Gregorian calendar is the default calendar for the en-US culture.
Dim myDT As New DateTime(2002, 1, 3)
' Displays the format pattern associated with each format character.
Console.WriteLine("FORMAT en-US EXAMPLE")
Console.WriteLine("CHAR VALUE OF ASSOCIATED PROPERTY, IF ANY")
Console.WriteLine()
Console.WriteLine(" d {0}", myDT.ToString("d"))
Console.WriteLine(" {0} {1}", myDTFI.ShortDatePattern, "(ShortDatePattern)")
Console.WriteLine()
Console.WriteLine(" D {0}", myDT.ToString("D"))
Console.WriteLine(" {0} {1}", myDTFI.LongDatePattern, "(LongDatePattern)")
Console.WriteLine()
Console.WriteLine(" f {0}", myDT.ToString("f"))
Console.WriteLine()
Console.WriteLine(" F {0}", myDT.ToString("F"))
Console.WriteLine(" {0} {1}", myDTFI.FullDateTimePattern, "(FullDateTimePattern)")
Console.WriteLine()
Console.WriteLine(" g {0}", myDT.ToString("g"))
Console.WriteLine()
Console.WriteLine(" G {0}", myDT.ToString("G"))
Console.WriteLine()
Console.WriteLine(" m {0}", myDT.ToString("m"))
Console.WriteLine(" {0} {1}", myDTFI.MonthDayPattern, "(MonthDayPattern)")
Console.WriteLine()
Console.WriteLine(" M {0}", myDT.ToString("M"))
Console.WriteLine(" {0} {1}", myDTFI.MonthDayPattern, "(MonthDayPattern)")
Console.WriteLine()
Console.WriteLine(" r {0}", myDT.ToString("r"))
Console.WriteLine(" {0} {1}", myDTFI.RFC1123Pattern, "(RFC1123Pattern)")
Console.WriteLine()
Console.WriteLine(" R {0}", myDT.ToString("R"))
Console.WriteLine(" {0} {1}", myDTFI.RFC1123Pattern, "(RFC1123Pattern)")
Console.WriteLine()
Console.WriteLine(" s {0}", myDT.ToString("s"))
Console.WriteLine(" {0} {1}", myDTFI.SortableDateTimePattern, "(SortableDateTimePattern)")
Console.WriteLine()
Console.WriteLine(" t {0}", myDT.ToString("t"))
Console.WriteLine(" {0} {1}", myDTFI.ShortTimePattern, "(ShortTimePattern)")
Console.WriteLine()
Console.WriteLine(" T {0}", myDT.ToString("T"))
Console.WriteLine(" {0} {1}", myDTFI.LongTimePattern, "(LongTimePattern)")
Console.WriteLine()
Console.WriteLine(" u {0}", myDT.ToString("u"))
Console.WriteLine(" {0} {1}", myDTFI.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)")
Console.WriteLine()
Console.WriteLine(" U {0}", myDT.ToString("U"))
Console.WriteLine()
Console.WriteLine(" y {0}", myDT.ToString("y"))
Console.WriteLine(" {0} {1}", myDTFI.YearMonthPattern, "(YearMonthPattern)")
Console.WriteLine()
Console.WriteLine(" Y {0}", myDT.ToString("Y"))
Console.WriteLine(" {0} {1}", myDTFI.YearMonthPattern, "(YearMonthPattern)")
End Sub 'Main
End Class 'SamplesDTFI
'This code produces the following output.
'
'FORMAT en-US EXAMPLE
'CHAR VALUE OF ASSOCIATED PROPERTY, IF ANY
'
' d 1/3/2002
' M/d/yyyy (ShortDatePattern)
'
' D Thursday, January 03, 2002
' dddd, MMMM dd, yyyy (LongDatePattern)
'
' f Thursday, January 03, 2002 12:00 AM
'
' F Thursday, January 03, 2002 12:00:00 AM
' dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)
'
' g 1/3/2002 12:00 AM
'
' G 1/3/2002 12:00:00 AM
'
' m January 03
' MMMM dd (MonthDayPattern)
'
' M January 03
' MMMM dd (MonthDayPattern)
'
' r Thu, 03 Jan 2002 00:00:00 GMT
' ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
'
' R Thu, 03 Jan 2002 00:00:00 GMT
' ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
'
' s 2002-01-03T00:00:00
' yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)
'
' t 12:00 AM
' h:mm tt (ShortTimePattern)
'
' T 12:00:00 AM
' h:mm:ss tt (LongTimePattern)
'
' u 2002-01-03 00:00:00Z
' yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)
'
' U Thursday, January 03, 2002 8:00:00 AM
'
' y January, 2002
' MMMM, yyyy (YearMonthPattern)
'
' Y January, 2002
' MMMM, yyyy (YearMonthPattern)
'
[C#]
using System;
using System.Globalization;
public class SamplesDTFI {
public static void Main() {
// Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
DateTimeFormatInfo myDTFI = new CultureInfo( "en-US", false ).DateTimeFormat;
// Creates a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3).
// The Gregorian calendar is the default calendar for the en-US culture.
DateTime myDT = new DateTime( 2002, 1, 3 );
// Displays the format pattern associated with each format character.
Console.WriteLine( "FORMAT en-US EXAMPLE" );
Console.WriteLine( "CHAR VALUE OF ASSOCIATED PROPERTY, IF ANY\n" );
Console.WriteLine( " d {0}", myDT.ToString("d") );
Console.WriteLine( " {0} {1}\n", myDTFI.ShortDatePattern, "(ShortDatePattern)" );
Console.WriteLine( " D {0}", myDT.ToString("D") );
Console.WriteLine( " {0} {1}\n", myDTFI.LongDatePattern, "(LongDatePattern)" );
Console.WriteLine( " f {0}\n", myDT.ToString("f") );
Console.WriteLine( " F {0}", myDT.ToString("F") );
Console.WriteLine( " {0} {1}\n", myDTFI.FullDateTimePattern, "(FullDateTimePattern)" );
Console.WriteLine( " g {0}\n", myDT.ToString("g") );
Console.WriteLine( " G {0}\n", myDT.ToString("G") );
Console.WriteLine( " m {0}", myDT.ToString("m") );
Console.WriteLine( " {0} {1}\n", myDTFI.MonthDayPattern, "(MonthDayPattern)" );
Console.WriteLine( " M {0}", myDT.ToString("M") );
Console.WriteLine( " {0} {1}\n", myDTFI.MonthDayPattern, "(MonthDayPattern)" );
Console.WriteLine( " r {0}", myDT.ToString("r") );
Console.WriteLine( " {0} {1}\n", myDTFI.RFC1123Pattern, "(RFC1123Pattern)" );
Console.WriteLine( " R {0}", myDT.ToString("R") );
Console.WriteLine( " {0} {1}\n", myDTFI.RFC1123Pattern, "(RFC1123Pattern)" );
Console.WriteLine( " s {0}", myDT.ToString("s") );
Console.WriteLine( " {0} {1}\n", myDTFI.SortableDateTimePattern, "(SortableDateTimePattern)" );
Console.WriteLine( " t {0}", myDT.ToString("t") );
Console.WriteLine( " {0} {1}\n", myDTFI.ShortTimePattern, "(ShortTimePattern)" );
Console.WriteLine( " T {0}", myDT.ToString("T") );
Console.WriteLine( " {0} {1}\n", myDTFI.LongTimePattern, "(LongTimePattern)" );
Console.WriteLine( " u {0}", myDT.ToString("u") );
Console.WriteLine( " {0} {1}\n", myDTFI.UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)" );
Console.WriteLine( " U {0}\n", myDT.ToString("U") );
Console.WriteLine( " y {0}", myDT.ToString("y") );
Console.WriteLine( " {0} {1}\n", myDTFI.YearMonthPattern, "(YearMonthPattern)" );
Console.WriteLine( " Y {0}", myDT.ToString("Y") );
Console.WriteLine( " {0} {1}\n", myDTFI.YearMonthPattern, "(YearMonthPattern)" );
}
}
/*
This code produces the following output.
FORMAT en-US EXAMPLE
CHAR VALUE OF ASSOCIATED PROPERTY, IF ANY
d 1/3/2002
M/d/yyyy (ShortDatePattern)
D Thursday, January 03, 2002
dddd, MMMM dd, yyyy (LongDatePattern)
f Thursday, January 03, 2002 12:00 AM
F Thursday, January 03, 2002 12:00:00 AM
dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)
g 1/3/2002 12:00 AM
G 1/3/2002 12:00:00 AM
m January 03
MMMM dd (MonthDayPattern)
M January 03
MMMM dd (MonthDayPattern)
r Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
R Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
s 2002-01-03T00:00:00
yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)
t 12:00 AM
h:mm tt (ShortTimePattern)
T 12:00:00 AM
h:mm:ss tt (LongTimePattern)
u 2002-01-03 00:00:00Z
yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)
U Thursday, January 03, 2002 8:00:00 AM
y January, 2002
MMMM, yyyy (YearMonthPattern)
Y January, 2002
MMMM, yyyy (YearMonthPattern)
*/
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
int main()
{
// Creates and initializes a DateTimeFormatInfo associated with the en-US culture.
CultureInfo * MyCI = new CultureInfo(S"en-US", false);
DateTimeFormatInfo* myDTFI = MyCI -> DateTimeFormat;
// Creates a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3).
// The Gregorian calendar is the default calendar for the en-US culture.
DateTime myDT = DateTime(2002, 1, 3);
// Displays the format pattern associated with each format character.
Console::WriteLine(S"FORMAT en-US EXAMPLE");
Console::WriteLine(S"CHAR VALUE OF ASSOCIATED PROPERTY, IF ANY\n");
Console::WriteLine(S" d {0}", myDT.ToString(S"d"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> ShortDatePattern, S"(ShortDatePattern)");
Console::WriteLine(S" D {0}", myDT.ToString(S"D"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> LongDatePattern, S"(LongDatePattern)");
Console::WriteLine(S" f {0}\n", myDT.ToString(S"f"));
Console::WriteLine(S" F {0}", myDT.ToString(S"F"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> FullDateTimePattern, S"(FullDateTimePattern)");
Console::WriteLine(S" g {0}\n", myDT.ToString(S"g"));
Console::WriteLine(S" G {0}\n", myDT.ToString(S"G"));
Console::WriteLine(S" m {0}", myDT.ToString(S"m"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> MonthDayPattern, S"(MonthDayPattern)");
Console::WriteLine(S" M {0}", myDT.ToString(S"M"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> MonthDayPattern, S"(MonthDayPattern)");
Console::WriteLine(S" r {0}", myDT.ToString(S"r"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> RFC1123Pattern, S"(RFC1123Pattern)");
Console::WriteLine(S" R {0}", myDT.ToString(S"R"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> RFC1123Pattern, S"(RFC1123Pattern)");
Console::WriteLine(S" s {0}", myDT.ToString(S"s"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> SortableDateTimePattern, S"(SortableDateTimePattern)");
Console::WriteLine(S" t {0}", myDT.ToString(S"t"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> ShortTimePattern, S"(ShortTimePattern)");
Console::WriteLine(S" T {0}", myDT.ToString(S"T"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> LongTimePattern, S"(LongTimePattern)");
Console::WriteLine(S" u {0}", myDT.ToString(S"u"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> UniversalSortableDateTimePattern, S"(UniversalSortableDateTimePattern)");
Console::WriteLine(S" U {0}\n", myDT.ToString(S"U"));
Console::WriteLine(S" y {0}", myDT.ToString(S"y"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> YearMonthPattern, S"(YearMonthPattern)");
Console::WriteLine(S" Y {0}", myDT.ToString(S"Y"));
Console::WriteLine(S" {0} {1}\n", myDTFI -> YearMonthPattern, S"(YearMonthPattern)");
}
/*
This code produces the following output.
FORMAT en-US EXAMPLE
CHAR VALUE OF ASSOCIATED PROPERTY, IF ANY
d 1/3/2002
M/d/yyyy (ShortDatePattern)
D Thursday, January 03, 2002
dddd, MMMM dd, yyyy (LongDatePattern)
f Thursday, January 03, 2002 12:00 AM
F Thursday, January 03, 2002 12:00:00 AM
dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)
g 1/3/2002 12:00 AM
G 1/3/2002 12:00:00 AM
m January 03
MMMM dd (MonthDayPattern)
M January 03
MMMM dd (MonthDayPattern)
r Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
R Thu, 03 Jan 2002 00:00:00 GMT
ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)
s 2002-01-03T00:00:00
yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)
t 12:00 AM
h:mm tt (ShortTimePattern)
T 12:00:00 AM
h:mm:ss tt (LongTimePattern)
u 2002-01-03 00:00:00Z
yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)
U Thursday, January 03, 2002 8:00:00 AM
y January, 2002
MMMM, yyyy (YearMonthPattern)
Y January, 2002
MMMM, yyyy (YearMonthPattern)
*/
Store proc in T-SQL SERVER 2005
Microsoft SQL Server provides the stored procedure mechanism to simplify the database development process by grouping Transact-SQL statements into manageable blocks.
Benefits of Stored Procedures
Why should you use stored procedures? Let’s take a look at the key benefits of this technology:
- Precompiled execution. SQL Server compiles each stored procedure once and then reutilizes the execution plan. This results in tremendous performance boosts when stored procedures are called repeatedly.
- Reduced client/server traffic. If network bandwidth is a concern in your environment, you’ll be happy to learn that stored procedures can reduce long SQL queries to a single line that is transmitted over the wire.
- Efficient reuse of code and programming abstraction. Stored procedures can be used by multiple users and client programs. If you utilize them in a planned manner, you’ll find the development cycle takes less time.
- Enhanced security controls. You can grant users permission to execute a stored procedure independently of underlying table permissions.
Stored procedures are very similar to user-defined functions, but there are subtle differences. For more information, read Comparing Stored Procedures and User-Defined Functions.
Structure
Stored procedures are extremely similar to the constructs seen in other programming languages. They accept data in the form of input parameters that are specified at execution time. These input parameters (if implemented) are utilized in the execution of a series of statements that produce some result. This result is returned to the calling environment through the use of a recordset, output parameters and a return code. That may sound like a mouthful, but you’ll find that stored procedures are actually quite simple.
Example
Let’s take a look at a practical example. Assume we have the table shown at the bottom of this page, named Inventory. This information is updated in real-time and warehouse managers are constantly checking the levels of products stored at their warehouse and available for shipment. In the past, each manager would run queries similar to the following:
SELECT Product, Quantity
FROM Inventory
WHERE Warehouse = ‘FL’
This resulted in very inefficient performance at the SQL Server. Each time a warehouse manager executed the query, the database server was forced to recompile the query and execute it from scratch. It also required the warehouse manager to have knowledge of SQL and appropriate permissions to access the table information.
We can simplify this process through the use of a stored procedure. Let’s create a procedure called sp_GetInventory that retrieves the inventory levels for a given warehouse. Here’s the SQL code:
CREATE PROCEDURE sp_GetInventory
@location varchar(10)
AS
SELECT Product, Quantity
FROM Inventory
WHERE Warehouse = @location
Our Florida warehouse manager can then access inventory levels by issuing the command
EXECUTE sp_GetInventory ‘FL’
The New York warehouse manager can use the same stored procedure to access that area’s inventory.
EXECUTE sp_GetInventory ‘NY’
Granted, this is a simple example, but the benefits of abstraction can be seen here. The warehouse manager does not need to understand SQL or the inner workings of the procedure. From a performance perspective, the stored procedure will work wonders. The SQL Sever creates an execution plan once and then reutilizes it by plugging in the appropriate parameters at execution time.
Now that you’ve learned the benefits of stored procedures, get out there and use them! Try a few examples and measure the performance enhancements achieved — you’ll be amazed!
Inventory Table
| ID | Product | Warehouse | Quantity |
| 142 | Green beans | NY | 100 |
| 214 | Peas | FL | 200 |
| 825 | Corn | NY | 140 |
| 512 | Lima beans | NY | 180 |
| 491 | Tomatoes | FL | 80 |
| 379 | Watermelon | FL | 85 |
This article covers the basic of writing a stored procedure. It’s the first in a series on writing stored procedures.
<!–
–>
A stored procedure allows for more modular programming. You can create reusable, discrete pieces of functionality using stored procedures. Stored procedures have the parsing and execution plan built at the time they are created so they execute faster than dynamic SQL. If you have a stored procedure that manipulates large amounts of data, that data will remain on the server and not be transported across the network. You can also use stored procedures for security. You can grant users execute permissions on stored procedures without granting them permissions on objects manipulated by those stored procedures.
You can use SQL Server’s Enterprise Manager to create and edit stored procedures. A simple stored procedure looks like:
CREATE PROCEDURE spCaliforniaAuthors
AS
SELECT * FROM authors
WHERE state = 'CA'
ORDER BY zip
This stored procedure is called “spCaliforniaAuthors”. All it contains is a SELECT statement. All stored procedures that SQL Server provides start with “sp_” (and “xp_” for extended stored procedures) and I chose to almost follow this convention for this stored procedure. If you try to call a stored procedure that starts with “sp_” SQL Server will first search the MASTER database before searching the current database.
By default, only members of the dbo_owner role and db_ddladmin role can create stored procedures. Members of the dbo_owner role can give other users the ability to create procedures using a GRANT statement. That might look something like this:
GRANT CREATE PROCEDURE TO Development
You can check Books Online for more information on the GRANT statement.
You execute a stored procedure by typing it’s name or using the EXECUTE statement. To execute our stored procedure you can type
EXECUTE spCaliforniaAuthors
This will execute the stored procedure and return the results. If you are calling this procedure from an ASP page (or other client) you can use the EXECUTE statement as you SQL string to execute. In this case, our stored procedure will return a record set.
javascript string functions
Syntax
| var myStr=new String(string); |
String Object Properties
F: Firefox, IE: Internet Explorer
| Property | Description | F | IE |
|---|---|---|---|
| constructor | Returns a reference to the function that created the object | 1 | 4 |
| length | Returns the number of characters in a string | 1 | 3 |
| prototype | Allows you to add properties and methods to an object | 1 | 4 |
String HTML Wrapper Methods
The HTML wrapper methods return the string wrapped inside the appropriate HTML tag.
| Method | Description | F | IE |
|---|---|---|---|
| anchor() | Creates an HTML anchor | 1 | 3 |
| big() | Creates a string in a big font | 1 | 3 |
| blink() | Creates a blinking string | 1 | |
| bold() | Creates a string in bold | 1 | 3 |
| fixed() | Creates a string as teletype text | 1 | 3 |
| fontcolor() | Creates a string in a specified color | 1 | 3 |
| fontsize() | Creates a string in a specified size | 1 | 3 |
| italics() | Creates a string in italic | 1 | 3 |
| link() | Creates a string as a hyperlink | 1 | 3 |
| small() | Creates a string in a small font | 1 | 3 |
| strike() | Creates a string with a strikethrough | 1 | 3 |
| sub() | Creates a string as subscript | 1 | 3 |
| sup() | Creates a string as superscript | 1 | 3 |
String Object Methods
| Method | Description | F | IE |
|---|---|---|---|
| charAt() | Returns the character at the specified position in a string | 1 | 3 |
| charCodeAt() | Returns the Unicode of the character at the specified position in a string | 1 | 4 |
| concat() | Joins two or more strings | 1 | 4 |
| fromCharCode() | Converts Unicode values to characters | 1 | 4 |
| indexOf() | Returns the position of the first found occurrence of a specified value in a string | 1 | 3 |
| lastIndexOf() | Returns the position of the last found occurrence of a specified value in a string | 1 | 3 |
| match() | Searches for a match between a regular expression and a string, and returns the matches | 1 | 4 |
| replace() | Searches for a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring | 1 | 4 |
| search() | Searches for a match between a regular expression and a string, and returns the position of the match | 1 | 4 |
| slice() | Extracts a part of a string and returns a new string | 1 | 4 |
| split() | Splits a string into an array of substrings | 1 | 4 |
| substr() | Extracts a specified number of characters in a string | 1 | 4 |
| substring() | Extracts the characters in a string between two specified indices | 1 | 3 |
| toLowerCase() | Converts a string to lowercase letters | 1 | 3 |
| toUpperCase() | Converts a string to uppercase letters | 1 | 3 |
| toSource() | Represents the source code of an object | 1 | - |
| valueOf() | Returns the primitive value of a String object | 1 | 4 |
JavaScript replace() Method
Definition and Usage
The replace() method finds a match between a substring (or regular expression) and a string, and replaces the matched substring with a new substring.
Syntax
| stringObject.replace(regexp/substr,newstring) |
| Parameter | Description |
|---|---|
| regexp/substr | Required. A substring or a regular expression object. Read more about the RegExp object |
| newstring | Required. The string to replace the found value in parameter 1 |
Example 1
Perform a case-sensitive search:
| <script type=”text/javascript”>var str=”Say GoodMorning!”; document.write(str.replace(“GoodMorning”, “HelloWorld”)); </script> |
The output of the code above will be:
| Say Hello World! |