Wednesday, November 21, 2007

การใช้ Can/Could

การใช้ Can/Could

ร่วมส่งเนื้อหาที่เป็นประโยชน์ คลิกที่นี่ บทความของท่านมีประโยชน์กับผู้ไม่รู้อีกมากมาย


A question from Afreen in India :
what is the difference between - 'can' and 'could.'

Sian Harris answers:
This is a common area for questions, as these verbs can have several functions. Without going into too much detail, I'm going to try and illustrate the key uses for you.

Firstly, we use 'can' for something that is possible or to show that somebody has the ability to do something in the present and future. For example:
We can see the park from our house,

Or
Ella can speak fluent Japanese.

The negative here is 'can't'...as in: He can't swim very well.
In this case, 'could' is generally used as the past form of 'can'. So,
Ella could speak fluent Japanese when she was young, and she can speak several other languages now too.

Or:
When I was a child I could run fast.

We use 'could' for general past ability, but be a little bit careful here, because when we're talking about what happened in a particular situation, we tend to use 'was/were able to' for past ability instead:

For example: The fire spread quickly, but luckily everybody was able to escape.

We also use 'could' to talk about possible actions now and in the future - here the function is possibility (not ability) - and this is what tends to cause the confusion around these words. For example, if you are expecting some friends to visit, but they have been delayed, you might say 'they could arrive at any time now', or if you're trying to make progress with your work you might ask 'Could we talk to the boss again? For this function of possibility, we need to watch out for the past form, as this doesn't work in exactly the same way. If we want to express past possibility, we need to use could + have and the past participle (have done, have been etc). Let's imagine you've received a letter or card from a mystery admirer, and you're trying to work out who sent it to you. As the sending happened in the past, we are speculating about a past possibility. 'John could have sent it' but perhaps 'James could have written it'...we are not sure who, but think there are some possibilities.

Finally, let's look at how we commonly use can/could in question forms to make requests (or ask for things). If we go shopping for clothes, we might ask 'can/could I try that dress o­n please?' or 'could I see those shoes in blue.' Here, either 'can' or 'could' may be used without significant difference. The o­nly thing worth noting is that in terms of register, that is (style and formality), 'could' is considered slightly more formal or polite.

So, to recap, three of the main functions for 'can/could' are to talk about: ability, possibility (with the change in the past form to remember) and for requests.


Sian Harris is the Manager of English Language Training & Development at the BBC World Service, and runs specialist courses in London and overseas for BBC staff. Before joining the BBC, she spent 10 years as an English language teacher, examiner and academic manager in schools and colleges in London.


ประเด็นแรก เราใช้ can สำหรับสิ่งที่เราต้องการบอกว่ามีความเป็นไปได้ หรือเพื่อที่จะแสดงให้เห้นว่าใครคนหนึ่งๆมีความสามารถที่จะทำสิ่งหนึ่งๆทั้งในอดีตและอนาคต

ตัวอย่างเช่น1.We can see the park from our house,.

เราสามารถมองเห็นสวนสาธารณะได้จากบ้านของพวกเรา

2.Ella can speak fluent Japanese.
เอลล่าสามารถพูดภาษาญี่ปุ่นได้อย่างคล่องแคล่ว

ในทางปฎิเสธเราใช้รูป can't

เช่น He can't swim very well.

เราสามารถรถนำ could มาใช้ แสดงความเป็นอดีตของ can

ดังนั้นสังเกต

Ella could speak fluent Japanese when she was young, and she can speak several other languages now too.

เอลล่าพูดภาษาญี่ปุ่นได้ดีเมื่อครั้งเยาว์วัย และตอนนี้หล่อนสามารถพูดได้หลายภาษาด้วยเช่นกัน

หรือWhen I was a child I could run fast. เมื่อครั้งผมยังเป็นเด็กผมวิ่งได้รวดเร็วมาก

เราใช้ could สำหรับความสามารถทั่วๆไปในอดีต แต่ระวังสักนิด เพราะว่าถ้าเรากำลังพูดเกี่ยวกับสิ่งที่เกิดขึ้นในอดีตในสถานการณ์ที่เจาะจงเรามักจะใช้ 'was/were able to' มาแทน could สำหรับบอกความสามารถในอดีตแทน



ตัวอย่างเช่น: The fire spread quickly, but luckily everybody was able to escape.

ไฟได้ลุกลามไปทั่วแต่โชดีที่ทุกคนรอดพ้นไปได้

นอกจากนี้เรายังใช้ could เพื่อที่จะพูดเกี่ยวกับ การกระทำที่เป็นไปได้ทั้งในปัจจุบันและอนาคตได้อีกด้วย

จงระวัง เพราะนี่เป็นสิ่งที่ทำให้หลายคนสับสน

ตัวอย่าง

ถ้าคุณกำลังคาดหวังรอคอยเพื่อนๆที่จะมาเยี่ยม แต่พวกเขามาล่าช้า คุณสามารถพูดว่า 'they could arrive at any time now'

หรือ

ถ้าคุณพยายามที่จะสร้างความก้าวหน้าในการงาน คุณก็อาจพูดว่า

'Could we talk to the boss again?
ในด้านของความเป็นไปได้นี้ เราจำเป็นที่จะเฝ้าระวังสำหรับรูปอดีต เพราะว่าสิ่งเหล่านี้ไม่ได้ใช้อย่างตายตัวในวิธีทางเดียวกัน

ถ้าเราต้องการที่จะบรรยายความเป็นไปได้ในอดีต เราจำเป็นที่จะต้องใช้ could+have และ past participate (have done ,have been etc) ลองจินตนาการดู สมมุติคุณได้รับจดหมายหรือบัตรอวยพรจากผู้ที่ชื่นชมลึกลับผู้หนึ่ง และคุณกำลังพยายามที่จะค้นหาว่าใครเป็นผู้ส่งมาให้คุณ ในขณะที่การส่งจดหมายได้เกิดขึ้นในอดีต ซึ่งเรากำลังพิจารณาเกี่ยวกับความเป็นไปได้ในอดีตนี้เราอาจตั้งสมมุติฐานที่เป็นไปได้ขึ้นมาว่า 'John could have sent it' แต่บางทีคุณอาจคิดว่า 'James could have written it'... ซึ่งทั้งสองความคิดก็มีความเป็นไปได้ทั้งนั้น

ท้ายสุดนี้ จงมองว่าเรามีการนำ can และ could ไปใช้อย่างไร ในรูปแบบของคำถามเพื่อที่จะร้องขอบางสิ่งบางอย่าง ถ้าเราไปซื้อเสื้อผ้า เราอาจจะถามคนขายว่า

'Can/Could I try that dress o­n please?' หรือ 'Could I see those shoes in blue?

ในที่นี้ทั้งสองประโยคอาจไม่ค่อยแตกต่างกันมากนักสามารถใช้ได้เหมือนกัน แต่ การใช้ could จะให้ความสุภาพมากกว่า และเป็นทางการ

เพื่อป้องกันการการลืมหลังจากอ่านบทความ ขอทวนว่า การใช้ Can/Could มีอยู่ 3 ลักษณะ

1.ใช้เพื่อพูดเกี่ยวกับความสามารถ

เช่น I can swim.(ปัจจุบัน)

I could swim when I was 10 years old.(ความสามารถในอดีตซึ่งไม่รุ้ว่าในปัจจุบันจะทำได้อยู่หรือไม่ต้องดูจากบริบท)

แต่หากความสามารถในอดีตนั้นอยู่ในสถานการณ์ที่เจาะจงเราจะใช้ was/were able to ซึ่งเขาจะ,มีสถานการณ์มาให้คล้ายๆดังตัวอย่างนี้

The fire spread quickly, but luckily everybody was able to escape.

ที่เรารู้ว่ามันเป็นเหตุการณ์เฉพาะเจาะจงก็เพราะเราเห็นประโยคเราจึงใช้ was able to แทน could

2.ใช้เพื่อพูดเกี่ยวกับความเป็นไปได้

ความเป็นไปได้ในปัจจุบันเช่น 'They could arrive at any time now'

ส่วนความเป็นไปได้ในอดีตเราจะใช้ Could have+V.3 (ห้ามใช้ can แทน could เด็ดขาด นะครับ)

3.ใช้เพื่อแสดงความร้องขอในบางสิ่งบางอย่าง

อันนี้จะใช้ can หรือ could ก็ได้ แต่ใช้ could จะสุภาพ กว่า

Could/can you come with me?

credit www.bbc.co.uk

แปลและสรุปโดย

PEiestien

Tuesday, October 30, 2007

Find Any String In SQL Server

SELECT ROUTINE_NAME, ROUTINE_DEFINITION FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_DEFINITION LIKE '%foobar%' AND ROUTINE_TYPE='PROCEDURE'

Monday, October 1, 2007

Create a copy of an existing SQL Server database

USE master

GO

-- the original database (use 'SET @DB = NULL' to disable backup)
DECLARE @DB varchar(200)
SET @DB = 'PcTopp'

-- the backup filename
DECLARE @BackupFile varchar(2000)
SET @BackupFile = 'c:\pctopp\sqlserver\backup.dat'

-- the new database name
DECLARE @TestDB varchar(200)
SET @TestDB = 'TestDB'

-- the new database files without .mdf/.ldf
DECLARE @RestoreFile varchar(2000)
SET @RestoreFile = 'c:\pctopp\sqlserver\backup'


-- ****************************************************************
-- no change below this line
-- ****************************************************************


DECLARE @query varchar(2000)

DECLARE @DataFile varchar(2000)
SET @DataFile = @RestoreFile + '.mdf'

DECLARE @LogFile varchar(2000)
SET @LogFile = @RestoreFile + '.ldf'

IF @DB IS NOT NULL
BEGIN
SET @query = 'BACKUP DATABASE ' + @DB + ' TO DISK = ' + QUOTENAME(@BackupFile, '''')
EXEC (@query)
END

-- RESTORE FILELISTONLY FROM DISK = 'C:\temp\backup.dat'
-- RESTORE HEADERONLY FROM DISK = 'C:\temp\backup.dat'
-- RESTORE LABELONLY FROM DISK = 'C:\temp\backup.dat'
-- RESTORE VERIFYONLY FROM DISK = 'C:\temp\backup.dat'

IF EXISTS(SELECT * FROM sysdatabases WHERE name = @TestDB)
BEGIN
SET @query = 'DROP DATABASE ' + @TestDB
EXEC (@query)
END

RESTORE HEADERONLY FROM DISK = @BackupFile
DECLARE @File int
SET @File = @@ROWCOUNT

DECLARE @Data varchar(500)
DECLARE @Log varchar(500)

SET @query = 'RESTORE FILELISTONLY FROM DISK = ' + QUOTENAME(@BackupFile , '''')

CREATE TABLE #restoretemp
(
LogicalName varchar(500),
PhysicalName varchar(500),
type varchar(10),
FilegroupName varchar(200),
size int,
maxsize bigint
)
INSERT #restoretemp EXEC (@query)

SELECT @Data = LogicalName FROM #restoretemp WHERE type = 'D'
SELECT @Log = LogicalName FROM #restoretemp WHERE type = 'L'

PRINT @Data
PRINT @Log

TRUNCATE TABLE #restoretemp
DROP TABLE #restoretemp

IF @File > 0
BEGIN
SET @query = 'RESTORE DATABASE ' + @TestDB + ' FROM DISK = ' + QUOTENAME(@BackupFile, '''') +
' WITH MOVE ' + QUOTENAME(@Data, '''') + ' TO ' + QUOTENAME(@DataFile, '''') + ', MOVE ' +
QUOTENAME(@Log, '''') + ' TO ' + QUOTENAME(@LogFile, '''') + ', FILE = ' + CONVERT(varchar, @File)
EXEC (@query)
END
GO

Monday, September 24, 2007

Executing Common SQL Coding Tasks Using Function Calls

Executing Common SQL Coding Tasks Using Function Calls

by Kevin Kline, Daniel Kline
12/04/2000

One of the great things about RDMS (Relational Database Management Systems) is its unmatched flexibility in addressing a number of different work requirements. This article shows examples of several common work requirements that are answered using function calls. A function is a special single-word command in SQL that returns a single value. The value of a function can be determined by input parameters, such as a function that averages a list of database values. But many functions do not use any type of input parameter, such as the function that returns the current system time, CURRENT_TIME.

Related Reading

SQL In A Nutshell

SQL In A Nutshell
By Kevin Kline

Table of Contents
Index
Sample Chapter

Read Online--Safari Search this book on Safari:


Code Fragments only

The database vendor implementations shown in examples below (Microsoft SQL Server, MySQL, Oracle, and PostgreSQL) are discussed in our upcoming book SQL in a Nutshell. There are a great many function calls that are universally supported by the ANSI (American National Standards Institute) standard and all the database vendors. For example, most vendors support the commonly used aggregate functions of SUM, AVG, MIN, and MAX. These functions extract summary value, average value, and minimum or maximum value from a column or an expression, respectively. There are also a whole variety of functions that are not universally supported, such as RPAD and LPAD or SUBSTRING versus SUBSTR.

Although this article discusses database implementations by Microsoft SQL Server, MySQL, Oracle, and PostgreSQL, this information represents just the tip of the iceberg on how business operations accomplish common, everyday SQL coding tasks using functions calls. As you will see, these functions can vary widely.

Date Operations

This first set of examples show how to query the database for common date-processing operations using functions. To get the current date and time:

Microsoft SQL Server

  SELECT GETDATE()
GO
MySQL [retrieving the date but not time]
  SELECT CURDATE();
MySQL [retrieving date and time]
  SELECT NOW();
Oracle
  SELECT SYSDATE
FROM dual;
PostgreSQL
  SELECT CURRENT_DATE;

As the examples illustrate, each vendor retrieves the current date and time differently using its own proprietary function calls. Microsoft SQL Server uses a SELECT statement calling the GETDATE() function. MySQL has two different function calls: CURDATE() and NOW(). The former retrieves the date without time; the latter retrieves date and time. Oracle uses the SYSDATE function call. And PostgreSQL uses the SQL99 CURRENT_DATE function call. Note that for all of these function calls, no passed parameters are needed.

These next examples show how to find out what day a given date falls on:

Microsoft SQL Server

  SELECT DATEPART(dw, GETDATE())
GO
MySQL
  SELECT DAYNAME(CURDATE());
Oracle
  SELECT TO_CHAR(SYSDATE,'Day')
FROM dual;
PostgreSQL
  SELECT DATE_PART('dow', date 'now');

Microsoft SQL Server uses the DATEPART function call using the syntax DATEPART(datetype, date_expression). This function requires the type of date (month, day, week, day of week, and so on), as the first argument, and the date expression (either a column containing a date or an actual date value), as the second part. MySQL offers the DAYNAME(date_expression) as its function of choice for finding the day of the week for a given date value. Oracle requires that the date be converted into a character value using TO_CHAR, but allows the application of a format mask that returns the data of the week value. Conversions of this type in Oracle follow the syntax TO_CHAR(conversion_expression, 'datetype'). In this case, TO_CHAR can be used to convert any other datatype to character datatype, including INT and DATE datatypes. PostgreSQL accomplishes date conversion using the DATE_PART function to extract the day of the week from the date expression. The syntax is DATE_PART('text', timestamp), where text defines how the date is returned (in our example, as a day of the week), and timestamp defines the date expression.

Sometimes an application needs to know how far two dates are from one another. To determine how far away a date is from the current date (or any other date for that matter), either in the future or in the past, use these examples:

Microsoft SQL Server

  SELECT DATEDIFF(dd, '1/1/01', GETDATE())
GO
MySQL
  SELECT FROM_DAYS(TO_DAYS(CURDATE()) -
TO_DAYS('2001-11-25'));
Oracle
  SELECT TO_DATE('25-Nov-2000','dd-mon-yyyy') -
TO_DATE('25-Aug-1969','dd-mon-yyyy')
FROM dual;
PostgreSQL
  SELECT AGE(CURRENT_DATE, '25-Aug-1969');

Measuring the time span between two dates is a procedure best left to procedure calls. But again, the syntax varies widely between the vendors. Microsoft uses the DATEDIFF function to measure the time span between two dates (in the example, between January 1, 2001 and today's date). The syntax is DATEDIFF(datetype, start_date, end_date), where datetype is a code representing how the time span should be represented (days, weeks, months, and so on), the start_date is the date to measure from, and the end_date is the date to measure to. MySQL must use the somewhat more cumbersome FROM_DAYS and TO_DAYS functions in a nested format to tell the time span between two dates. Oracle very neatly allows date addition and subtraction. The only reason the TO_DATE function is even needed is that the operation is being performed on character strings. If the operation were performed against two columns of DATE datatype, then no TO_DATE conversion would be necessary and the subtraction operation would act directly on the date expression. PostgreSQL has a cool function called AGE(start_date, end_date) that tells the time span between two passed dates as parameters.

It is common procedure to retrieve a date in a different format mask (Mon, DD, YYYY; mm/dd/yy; dd/mm/yy; etc.). Here are some examples:

Microsoft SQL Server

  SELECT CONVERT(VARCHAR(11), GETDATE(), 102)
GO
MySQL
  SELECT DATE_FORMAT( "2001-11-25", "%M %e, %Y");
Oracle
  SELECT TO_CHAR(SYSDATE,'dd-Mon-yyyy hh:mi:ss PM')
FROM dual;
PostgreSQL
  SELECT TO_CHAR (timestamp(CURRENT_DATE),
'dd-Mon-yyyy hh:mi:ss PM');

In these examples, the vendors use specialized function calls to retrieve date expressions in a specific format. Microsoft SQL Server uses the CONVERT function (though CAST could also be used) in the syntax of CONVERT(convert_to_datatype, source_expression, date_format), where the convert_to_datatype is the datatype to return in the query, the source_expression is the source that will be converted, and the date_format is a set of codes that Microsoft has set aside for specific date format masks. MySQL uses the DATE_FORMAT function in the syntax of DATE_FORMAT(source_expression, date_format). Oracle uses TO_CHAR, as shown earlier, in the syntax of TO_CHAR(source_expression, date_format). PostgreSQL also uses TO_CHAR, though somewhat differently in that the source_expression must be enclosed within the time-stamp subfunction, as shown in the example above.

String Operations

Often, an application may need to find one string within another string. This is one way of performing this operation across the different vendors:

Microsoft SQL Server

  SELECT CHARINDEX('eat', 'great')
GO
MySQL
  SELECT POSITION('eat' IN 'great');
Oracle
  SELECT INSTR('Great','eat') FROM dual;
PostgreSQL
  SELECT POSITION('eat' IN 'great');

Microsoft SQL Server uses its own function, CHARINDEX, to extract values from other strings. In this example, it will return the starting position of one string, 'eat,' within another, 'great.' The syntax is CHARINDEX(search_string, searched_string, [starting_position]). MySQL and PostgreSQL both accomplish a similar operation using the POSITION function, showing where 'eat' occurs within 'great.' Oracle uses the INSTR function, although the order of the passed parameters are reversed. Unlike the other vendors, Oracle requires the searched_string first, then the search_string.

It is often necessary to trim trailing and leading spaces from an expression in an SQL operation:

Microsoft SQL Server

  SELECT LTRIM('  sql_in_a_nutshell'),
SELECT RTRIM('sql_in_a_nutshell '),
SELECT LTRIM(RTRIM(' sql_in_a_nutshell ')
GO
MySQL
  SELECT LTRIM('  sql_in_a_nutshell'),
SELECT RTRIM('sql_in_a_nutshell '),
SELECT TRIM(' sql_in_a_nutshell '),
SELECT TRIM(BOTH FROM ' sql_in_a_nutshell ');
Oracle
  SELECT LTRIM('  sql_in_a_nutshell'),
SELECT RTRIM('sql_in_a_nutshell '),
TRIM(' sql_in_a_nutshell ')
FROM dual;
PostgreSQL
  SELECT TRIM(LEADING FROM '     sql_in_a_nutshell'),
TRIM(TRAILING FROM 'sql_in_a_nutshell '),
TRIM(BOTH FROM ' sql_in_a_nutshell ');

Microsoft SQL Server uses the LTRIM and RTRIM functions to remove spaces from the left or right side of an expression, respectively. When trimming spaces on both sides of an expression in Microsoft SQL Server, the LTRIM function must encapsulate the RTRIM function (or vice versa). MySQL and Oracle both use LTRIM and RTRIM, but differ from SQL Server in that spaces can be trimmed from both sides of an expression with the TRIM function. MySQL also allows TRIM with the BOTH operator to indicate that both left and right sides of the expression should be trimmed. PostgreSQL uses only the TRIM function and controls whether the left, right, or both sides should be trimmed using the LEADING, TRAILING, and BOTH operators, as shown in the example above.

The opposite of trimming spaces is to pad them into an expression. To pad in x number of trailing or leading spaces with the various vendors:

Microsoft SQL Server

  Not supported
MySQL
  SELECT LPAD('sql_in_a_nutshell', 20, ' '), 
RPAD('sql_in_a_nutshell', 20, ' ');
Oracle
  SELECT LPAD(('sql_in_a_nutshell', 20, ' '),
RPAD(('sql_in_a_nutshell', 20, ' ')
FROM dual;
PostgreSQL
  SELECT LPAD('sql_in_a_nutshell', 20, ' '), 
RPAD('sql_in_a_nutshell', 20, ' ');

In this example, the supporting vendors all use LPAD to insert spaces (or a character expression) on the left side of a string expression and RPAD to put them on the right. The syntax for MySQL, Oracle, and PostgreSQL is xPAD('string_expression1', length, 'string_expression2'), where string_expression1 is the string to have characters padded, length is the total length of the string, and string_expression2 is the characters to pad out.

An operation similar to pad is to substitute characters within a string with other characters:

Microsoft SQL Server [returns 'wabbit_hunting_season']

  SELECT STUFF('wabbit_season', 7, 1, '_hunting_')
GO
MySQL [returns 'wabbit_hunting_season']
  SELECT
REPLACE('wabbit_season','it_','it_hunting_');
Oracle [returns 'wabbit_hunting_season']
  SELECT
REPLACE('wabbit_season','it_','it_hunting_')
FROM dual;
PostgreSQL
  SELECT TRANSLATE('wabbit_season','it_','it_hunting_');

Microsoft SQL Server uses the STUFF function to overwrite existing characters. Using this syntax, STUFF(string_expression, start, length, replacement_characters), string_expression is the string that will have characters substituted, start is the starting position, length is the number of characters in the string that are substituted, and replacement_characters are the new characters interjected into the string. MySQL and Oracle both use the function call REPLACE, using the syntax REPLACE(string_expression, search_string, replacement_string), where every incidence of search_string found in the string_expression will be replaced with replacement_string. PostgreSQL uses the TRANSLATE function as a synonym of REPLACE.

Many times, a SQL statement must retrieve only a portion of a string. The following examples show how to extract 'duck_season' from the string 'wabbit_duck_season' for each vendor:

Microsoft SQL Server

  SELECT SUBSTRING('wabbit_duck_season', 7, 11)
GO
MySQL
  SELECT
SUBSTRING('wabbit_duck_season', 7, 11);
Oracle
  SELECT SUBSTR('wabbit_duck_season', 7, 11)
FROM dual;
PostgreSQL
  SELECT
SUBSTR('wabbit_duck_season', 7, 11);

In each example, the syntax for SUBSTRING (or SUBSTR) is essentially the same: SUBSTRING(string_expression, start, length), where string_expression is the expression or column to be searched, start is an integer telling the starting position, and length is an integer telling the database how many characters to extract.

Some vendors allow function calls that can structure an IF, THEN, ELSE result set within the query:

Microsoft SQL Server

  SELECT  CASE
WHEN foo = 'hi' THEN 'there'
WHEN foo = 'good' THEN 'bye'
ELSE 'default'
END
FROM t2
GO
MySQL
  N/A 
Oracle
  SELECT DECODE
(payments_info,'CR','Credit','DB','Debit', null)
FROM dual;
PostgreSQL
  SELECT CASE
WHEN foo = 'hi' THEN 'there'
WHEN foo = 'good' THEN 'bye'
ELSE 'default'
END
FROM t2;

Microsoft SQL Server and PostgreSQL support the powerful ANSI SQL command CASE. CASE has two usages: simple and searched. Simple CASE expression compares one value, the input_value, with a list of other values and returns a result associated with the first matching value. Searched CASE expressions allow the analysis of several logical conditions and returns a result associated with the first one that is true.

Simple comparison operation

  CASE input_value
WHEN when_condition THEN resulting_value
[...n]
[ELSE else_result_value]
END
Boolean searched operation
  CASE
WHEN Boolean_condition THEN resulting_value
[...n]
[ELSE else_result_expression]
END

In the simple CASE function, the input_value is evaluated against each WHEN clause. The resulting_value is returned for the first TRUE instance of input_value = when_condition. If no when_condition evaluates as TRUE, the else_result_value is returned. If no else_result_value is specified, then NULL is returned.

In the more elaborate Boolean searched operation, the structure is essentially the same as the simple comparison operation except that each WHEN clause has its own Boolean comparison operation. In either usage, multiple WHEN clauses are used, although only one ELSE clause is necessary.

Oracle supports its own extremely powerful IF, THEN, ELSE function call: DECODE. DECODE has a unique syntax along these lines, DECODE(search_expression, search1, replace1, search[,.n], replace,.n], default), where search_expression is the string to be searched; subsequently each search string is paired with a replacement string. If a search is successful, the corresponding result is returned. In our example, when returning a result set from the payments_info column, any incident of 'CR' will be replaced with 'Credit,' any instance of 'DB' will be replace with 'Debit,' and any other values will be replaced with a default value of Null.

Nulls Operations

Nulls are sometimes tricky business. Sometimes a company process, such as a query or other data manipulation statement, must explicitly handle NULLs. These examples show how to return a value specified when a field or result is null:

Microsoft SQL Server

  SELECT ISNULL(foo, 'Value is Null')
GO
MySQL
  N/A
Oracle
  SELECT NVL(foo,'Value is Null')
FROM dual;
PostgreSQL [allows you to write a user-defined function to handle this feature]
  N/A

Microsoft SQL Server uses the ISNULL function following the syntax ISNULL(string_expression, replacement_value), where string_expression is a string or column being searched and replacement value is the value returned if string_expression is NULL. Oracle uses a different function, NVL, but follows an almost identical syntax.

Alternately, there may be times when a NULL value is needed if a field contains a specific value:

Microsoft SQL Server [returns NULL when foo equates to 'Wabbits!']

  SELECT NULLIF(foo, 'Wabbits!')
GO
MySQL
  N/A
Oracle
  SELECT DECODE(foo,'Wabbits!',NULL)
FROM dual;
PostgreSQL
  SELECT NULLIF(foo, 'Wabbits!');

Aside from using CASE or DECODE to solve this problem, Microsoft and PostgreSQL allow use of the NULLIF function. The syntax is NULLIF(expression1, expression2), which tells the database that if expression1 equals expression2, then returns a NULL value.

Summary

There are a great many function calls that are universally supported by the ANSI standard and all the database vendors. This article has shown a variety of useful function calls available in database implementations by Microsoft SQL Server, MySQL, Oracle, and PostgreSQL. For more details on ANSI standard functions, check out our book, SQL in a Nutshell.

Tuesday, September 18, 2007

logparser Command

CD D:\IISLogs\www\W3SVC242049591 --HS
CD D:\IISLogs\www\W3SVC971333791 --WHM
CD D:\IISLogs\www\W3SVC1074683251 --ADTOP
CD D:\IISLogs\www\W3SVC1059179751 --HBT

logparser "SELECT date, time, cs-uri-stem, cs-uri-query, c-ip, cs-method, sc-status, COUNT(*) AS Hits FROM ex*.log where date = '2007-08-30'and cs-uri-stem like '%.asp' and sc-status = 500 GROUP BY date, time, cs-uri-stem, cs-uri-query, c-ip, cs-method, sc-status ORDER BY date, time" -i:iisw3c -o:datagrid





logparser "SELECT date, time, cs-uri-stem, cs-uri-query, c-ip, cs-method, sc-status, COUNT(*) AS Hits FROM ex*.log where date > '2007-08-20'and cs-uri-stem like '%.asp' and cs-uri-query like '%zoom%' and sc-status = 500 GROUP BY date, time, cs-uri-stem, cs-uri-query, c-ip, cs-method, sc-status ORDER BY date, time" -i:iisw3c -o:datagrid


logparser "SELECT date, time, cs-uri-stem, cs-uri-query, c-ip, cs-method, sc-status, COUNT(*) AS Hits FROM ex*.log where date > '2007-08-28'and cs-uri-stem like '%.asp' and cs-uri-query like '%zoom%' GROUP BY date, time, cs-uri-stem, cs-uri-query, c-ip, cs-method, sc-status ORDER BY date, time" -i:iisw3c -o:datagrid




logparser "SELECT date, time, cs-uri-stem, cs-uri-query, c-ip, cs-method, sc-status, COUNT(*) AS Hits FROM ex*.log where sc-status >=500 GROUP BY date, time, cs-uri-stem, cs-uri-query, c-ip, cs-method, sc-status ORDER BY date, time" -i:iisw3c -o:datagrid

logparser "SELECT date, time, cs-uri-stem, cs-uri-query, c-ip, cs-method, sc-status, COUNT(*) AS Hits FROM ex*.log where c-ip='209.51.147.66' GROUP BY date, time, cs-uri-stem, cs-uri-query, c-ip, cs-method, sc-status ORDER BY date, time" -i:iisw3c -o:datagrid
logparser "SELECT * FROM ex*.log where c-ip='209.51.147.66' ORDER BY date, time" -i:iisw3c -o:datagrid

logparser "SELECT TOP 10 * FROM ex*.log where cs-uri-stem like '%default.asp%' ORDER BY date, time" -i:iisw3c -o:datagrid


logparser "SELECT date, time, cs-uri-stem, cs-uri-query, c-ip, cs-method, sc-status, COUNT(*) AS Hits FROM ex*.log where c-ip='58.64.20.2' and sc-status >=500 GROUP BY date, time, cs-uri-stem, cs-uri-query, c-ip, cs-method, sc-status ORDER BY date, time" -i:iisw3c -o:datagrid

logparser "SELECT date, time, cs-uri-stem, cs-uri-query, c-ip, cs-method, sc-status, COUNT(*) AS Hits FROM ex*.log where cs-uri-stem like '%admin2.asp' and sc-status >=500 GROUP BY date, time, cs-uri-stem, cs-uri-query, c-ip, cs-method, sc-status ORDER BY date, time" -i:iisw3c -o:datagrid




logparser "SELECT date, time, cs-uri-stem, cs-uri-query, c-ip, cs-method, sc-status, COUNT(*) AS Hits FROM ex*.log where sc-status >=500 GROUP BY date, time, cs-uri-stem, cs-uri-query, c-ip, cs-method, sc-status ORDER BY date, time" -i:iisw3c -o:datagrid

Thursday, September 13, 2007

DTree is a free JavaScript tree menu.

Description

dTree is a free JavaScript tree menu.

There are a lot of tree generating scripts just like this one, and many of them are great. So why use dTree?
dTree is very simple to set up and use. You don't have to worry about adding pages to the tree in a specific order, which makes it ideal for generating a tree from a database.

The first version of the tree is still available here.

Key features

  • Unlimited number of levels
  • Can be used with or without frames
  • Remembers the state of the tree between pages
  • Possible to have as many trees as you like on a page
  • All major browsers suported
    • Internet Explorer 5+
    • Netscape 6+
    • Opera 7+
    • Mozilla
  • Generates XHTML 1.0 strict validated output
  • Alternative images for each node

Example

Tigra Menu

Overview

Download Now! Tigra Tree Menu is a free JavaScript DHTML navigation system for web sites and web applications. Product looks and acts exactly as Microsoft Windows Tree Control. Offering the best performance on the market, script can manage hierarchies containing thousands of items.

Features

Click Here for Live Demo

  • unlimited tree menu depth
  • unlimited number of tree menus on single page all independently configurable
  • easy items text styles setup
  • any icons set can be used
  • small script file (~5KB)
  • highly optimized code guarantees incredible performance (good results on trees containing 10,000+ items)
  • can be used either inline or with frames
  • future browser's versions ready
  • simple configuration file structure, easy to generate dynamically from database
  • object oriented JavaScript implementation for those who want to learn DHTML programming
  • best choice for site maps, online documentation and intranet applications

Compatibility Table

Note: As any other javascript component Tigra Tree Menu requires JavaScript and CSS support enabled in the browser.
Here is a table of the script's compatibility:

Platform Browser
MS IE Netscape Mozilla Firefox Opera Safari
Windows 95/98/ME/2000/XP 4.0+ 6.2 0.9+ 7.0+ 3+
Mac OS 8/9/X 5.2 7.0+ 0.9+ 7.0+ 1.3+

Tree Menu PRO additionally offers:

  • Wider browsers' support (versions 4.x+)
  • All styles (fonts, colors, distances, decorations) are defined with CSS independently for each state of the item. Settings are applied globally for the tree instance and can be overridden locally for each item
  • Supports frames and windows targeting (other frame, new window, parent frame). The target can be defined globally for the tree instance and can be overridden locally for each item
  • Rollover, on open and on select effects can be implemented
  • Tree state is kept across the pages and can be reset or redefined at any moment
  • Events Model allows you to hook your code on tree menu events such as node open/close, tree show and others. You can even cancel the default functionality and replace it with your own
  • Open Item Manipulation API makes it possible to control the state of the tree with external javascript code
  • PHP and ASP samples for dynamic tree generation from the database or file system
  • Free setup support, free product updates, discounts for other products