StatCounter

View My Stats

Tuesday, February 5, 2008

To Select Odd and Even number of records From Table-SQL Server

To select odd numbers of records:

select distinct id1,name1,name2 from a where (id1%2)=1

To Select even numbers of record:

select distinct id1,name1,name2 from a where (id1%2)=0

Here id1 is a Identity column of a Table a
id1-Identity Column
a-TableName

To get Current Time in SQL Server

The query as Follows:

select CONVERT(char(5), CURRENT_TIMESTAMP,8)

Update Salary Based on Particular Range-SQL Server

update table1
set salary=case when salary between 1000 and 1500 then salary+10
when salary between 2000 and 2500 then salary+56
when salary between 3000 and 3500 then salary+10
when salary between 5000 and 5500 then salary +10
end

table-TableName
salary-Column Name

Types Of Constraints-SQL Server

1.Primary Key
2.Foreign key or references integirity
3.Unique
4.Check
5.NOt null
6.Default

The unique key allows only one null values.But the primary key does,nt allows a null values
By default The primary key is clustered index.
By default the unique key is NOn Clustered index.

Clustered Index:
It alters the way in which the data is srored in the database.

Non Clustered Index:
It does not alters the way in which the data is stored in the database.

Whenever We are implementing an indexes it uses non clustered index of sorting the data

To found n'th Salary Of a Recods:SQl Server

The query as Follows:

select top 1 salary from (select distinct top 2 salary from table1 order by salary desc) a order by salary

Here
salary-Column Name
table1-Table Name
a-Concatenation..It may be any other character

DateTime Formatted Output: SQL Server

0 or 100 mon dd yyyy hh:miAM (or PM)
101 mm/dd/yy 102 yy.mm.dd
103 dd/mm/yy
104 dd.mm.yy
105 dd-mm-yy
106 dd mon yy
107 Mon dd, yy
108 hh:mm:ss
9 or 109 mon dd yyyy hh:mi:ss:mmmAM (or PM)
110 mm-dd-yy
111 yy/mm/dd
112 yymmdd
13 or 113 dd mon yyyy hh:mm:ss:mmm(24h)
114 hh:mi:ss:mmm(24h)
20 or 120 yyyy-mm-dd hh:mi:ss(24h)
21 or 121 yyyy-mm-dd hh:mi:ss.mmm(24h)
126 yyyy-mm-dd Thh:mm:ss.mmm(no spaces)
130 dd mon yyyy hh:mi:ss:mmmAM
131

DateTime Conversion in SQL Server

//get doday date alone
SELECT CONVERT(varchar(8), GETDATE(), 112)

//get tomorrow date alone
SELECT CONVERT(CHAR(8), GETDATE()+1, 112)

//date diferrence in years
select datediff(year,20/3/1983,getdate())


//date difference in months
select datediff(month,20/3/1983,getdate())


//date difference in day
select datediff(day,20/3/1983,getdate())



//to add a current date with the user specified date
SELECT DATEADD(day, 30, GETDATE())

//To get the first day of the current month:
SELECT CONVERT(CHAR(10),GETDATE()+1-DAY(GETDATE()),101)

//TO display the date output like 2007/06/20
select convert(char(100),getdate(),111)

//In the above examples i used the parameter//Here I am using without Parameter.The result is like this Jun 21 2007 2:00AM
select convert(char(100),getdate())

//TO get the number of days in a current month 30
SELECT DAY(DATEADD(MONTH, 1, 1-DAY(GETDATE())+GETDATE())-1)

//TO get the current millisecon
Ans:60SELECT DATEPART(millisecond, GETDATE())

//to get the current week days
Ans: Thursday
SELECT DATENAME(WEEKDAY, GETDATE())

//Check is it a Date or Not
SELECT ISDATE('20030713') -- 1 SELECT ISDATE('2003-07-13') -- 1
select isdate(getdate())//result is 1