My Blog List

LOVE IS LIFE

#htmlcaption1 #htmlcaption2 Stay Connected

Pages

Friday, June 28, 2013

sql server trim word from last

SQL server trim word from last

Suppose we have a string: I am a good boy.John is bad boy

Requirement: To remove boy from last.

DECLARE @replcae_str VARCHAR(max)
DECLARE @str VARCHAR(max)

SELECT @replcae_str = 'boy'
SELECT @str = 'I am a good boy. John is bad boy'


SELECT
 SUBSTRING
  (
  @str,
  1,
  CASE CHARINDEX(REVERSE(@replcae_str),REVERSE(@str))
  when 0 then 0   
  else LEN(@str) - ( CHARINDEX(REVERSE(@replcae_str), REVERSE(@str)) + (LEN(@replcae_str) - 1) ) + 1
  END - 1

  )

Thursday, June 27, 2013

sql server query optimization tips


  • QUERY OPTIMIZATION SQL
  •   The sql query becomes faster if you use the actual columns names in                               SELECT statement instead of than '*'
  •  Select carefully where we have to use “where” and “having

·         Sometimes you may have more than one sub queries in your main query. Try to minimize the number of sub query block in your query. 
For Example: Write the query as
SELECT name
FROM employee
WHERE (salary, emp_age) = (SELECT MAX (salary), MAX (emp_age)
FROM employee_details)
AND dept = 'Electronics'
; “
Instead of:
SELECT name
FROM employee
WHERE salary = (SELECT MAX (salary) FROM employee_details)
AND age = (SELECT MAX (emp_age) FROM employee_details)
AND emp_dept = 'Electronics'
;”
·         Usually “IN” has the slowest performance, “IN” is efficient when most of the filter criteria is in the sub-query
·         EXISTS” is efficient when most of the filter criteria is in the main query
·         Use “EXISTS” instead of “DISTINCT” when using joins which involves tables having one-to-many relationship. 
For Example: Write the query as

SELECT d.dept_id, d.dept_name
FROM dept d
WHERE EXISTS (SELECT 'X' FROM employee e WHERE e.dept_name = d.dept_name);
Instead of:
SELECT DISTINCT d.dept_id, d.dept_name
FROM dept d,employee e
WHERE e.dept_name = e.dept_name;
·         Try to use UNION ALL in place of UNION
·         Be careful while using conditions in WHERE clause. 
For Example: Write the query as

SELECT id, name, age FROM student WHERE age > 30;
Instead of:

SELECT id, name, age FROM student WHERE age! = 50;

SQL server Check Ascii charater value for a string

Check Ascii charater value for a string

DECLARE @string VARCHAR(50)
DECLARE @i INT

SET @string = 'your String'
SET @i = 0

WHILE @i <= DATALENGTH(@string)
BEGIN
PRINT SUBSTRING(@string, @i, 1) + ' : ' + CAST(ASCII(SUBSTRING(@string, @i, 1)) AS VARCHAR(3))
SET @i = @i + 1

END

Clean\delete\truncate full database having key constraints

Clean\delete\truncate full database having key constraints

-- Disable the Referential Integrity
EXEC sp_MSForEachTable 'ALTER TABLE ? NOCHECK CONSTRAINT ALL'
GO

EXEC sp_MSForEachTable 'DELETE FROM ?'
GO

-- Enable Referential Integrity again
EXEC sp_MSForEachTable 'ALTER TABLE ? CHECK CONSTRAINT ALL'

GO

Wednesday, June 26, 2013

Case Sensitive JOIN

For case senstive joins use
  • COLLATE Latin1_General_CS_AS
For example:


SELECT table1.column1, table1.column2,table2.column1
FROM 
table1 T1 inner join table2 T2
ON
T1.column1 COLLATE Latin1_General_CS_AS = T2.column1 COLLATE Latin1_General_CS_AS