Categories

.NET (1) CSS (5) OOP (1) PHP (2) SQL (5) SQL Server (8) TSQL (1)
Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts

Monday, September 10, 2012

SQL Joins

Very good article regarding basics of JOINS...

SQL JOINS

Thursday, March 24, 2011

INSERT, DELETE, UPDATE in VIEW

A view can be Updated, deleted and inserted only if it has only one base table.
For example consider there is a table called book and it has view called Book_View. So we can update, delete and insert in that view and this view will insert the data in actual table “Book”. So if we have a view which contains more than one table then we can't perform these operations on that view.
Consider the view that list the books on the basis of category. So there would be join on book table and category table on categoryID column in this view. So we can’t perform updating,inserting and deleting operations on this view because SQL server will get confuse and will give error.

Friday, February 18, 2011

T-SQL String Functions

 --[SUBSTRING( string, start, length )]
    SELECT SUBSTRING('abcdef', 4, 3);
   -- ## OUTPUT -- def ##--
   
 --[RUGHT( string, n )]
    SELECT RIGHT('abcde', 3);           
-- ## OUTPUT -- cde ##--
   
 ---[LEFT( string, n )]
    SELECT LEFT('abcde', 3);            
-- ## OUTPUT -- abc ##--
   
 --[LEN( string );]
    SELECT LEN('abcde');              
  -- ## OUTPUT -- 5 ##--
   
 --[CHARINDEX( substring, string [, start_pos] )]
    SELECT CHARINDEX('-','abcdef ghi-jk');   
-- ## OUTPUT -- 11 ##--
   
 --[PATINDEX( pattern, string )]
    SELECT PATINDEX('%[0-9]%', 'abcdfe124563efgh');   
-- ## OUTPUT -- 7 ##--
   
 --[REPLACE( string, substring1, substring2 )]
    SELECT REPLACE('1*3 2*87', '*', '-');           
-- ## OUTPUT -- 1-3 2-87 ##--
   
 --[REPLICATE( string, n )]
    SELECT REPLICATE('abcdef', 2);                
-- ## OUTPUT -- abcdefabcdef ##--
   
 --(The STUFF function allows you to remove a substring from a string
 -- and insert a new substring)
 -- [STUFF( string, pos, delete length, inserting string )]
    SELECT STUFF('tabc', 2, 3, 'his');    
-- ## OUTPUT -- this ##--
   
-- [UPPER( string ), LOWER( string )]
    SELECT UPPER('This is a BOOK');       
-- ## OUTPUT -- THIS IS A BOOK ##--
    SELECT LOWER('This is a BOOK');        
-- ## OUTPUT -- this is a book ##--
   
 --RTRIM( string ), LTRIM( string )
    SELECT LTRIM(' abc');                  
-- ## OUTPUT -- abc ##--
    SELECT RTRIM('abc ');
   
 -- [The LIKE predicate]
    SELECT NAME FROM TBLNAME WHERE NAME LIKE '_d%'  

 -- ## OUTPUT --    Will return names where second character will be d ##-- 
    SELECT NAME FROM TBLNAME WHERE NAME LIKE '[ABC]%'
 
-- ## OUTPUT -- Return the names where starting characters will be A,B or C  ##-- 
    SELECT NAME FROM TBLNAME WHERE NAME LIKE '[A-F]%' 
  
-- ## OUTPUT -- Return the names where starting characters will be A to F  ##-- 
    SELECT NAME FROM TBLNAME WHERE NAME LIKE '[^A-F]%'
-- ## OUTPUT -- Return the names where starting characters will not from A to F  ##--

Friday, January 7, 2011

Efficient Text Searching Query

Normally when you want to write a query for searching something then you use the like operator. Consider for example that you want to write an algorithm for book search. Consider a situation where you ask BOOK TITLE from the user and then user enter the title the you write query something like this

SELECT BOOK_NAME WHERE TITLE LIKE ‘%@title%’

Now what if you want that if user write some title then your query perform above operations and if user write nothing , leave the field empty then you want to show all the books available in your system, so here is the query which will perform the same operation

SELECT
BOOK_NAME
WHERE
          ((@title IS NULL) OR (@title = ‘’) OR ([TITLE] LIKE '%'+@title+'%'))

Now when you will enter some string then last part of the query will be executed and if you enter nothing then first part of the query will be executed.

Monday, December 20, 2010

SQL Writing and Execution Order

I'm writing this post because developers often get confused about the SQL clauses execution order. The most important and basic SQL clauses are 
  • SELECT
  • FROM
  • WHERE
  • GROUP BY
  • HAVING
  • ORDER BY 
And there order in SQL query is
  1. SELECT
  2. FROM
  3. WHERE
  4. GROUP BY
  5. HAVING 
  6. ORDER BY
 But the database engine processes them in different ways. And the order of these clauses in DB is
  1. FROM
  2. WHERE
  3. GROUP BY
  4. HAVING
  5. SELECT
  6. ORDER BY