Surendra Sharma

Surendra Sharma

Search This Blog

Friday, July 19, 2013

How to get 3 character or MMM format month name in sql server

Here is query for showing complete month name, month number and month name in MMM or 3 character format

SELECT
DATENAME(mm,GETDATE()) AS [Month Name],   -- Dispaly complete Month Name
MONTH(GETDATE()) AS [Month Number], -- Dispaly Month number as NN digit
SUBSTRING(DATENAME(mm,GETDATE()),1,3) AS Month Name In MMM  -- Dispaly Month name in 3 characters
Output:


Month Name
Month Number
Month Name In MMM
July
7
Jul

Meet the richest man who became world's only 'quadrillionaire' for a minute - thanks to PayPal error

Due to some technical error in Paypal system, this man became world richest man. 

Please read more from here 
http://in.news.yahoo.com/meet-man-became-worlds-only-quadrillionaire-minute-thanks-044313668.html

Thursday, July 18, 2013

Best number of world

The world's best number is 73 as
  • 73 is 21st prime number
  • The number 21 = 7 X 3
  • Binary representation of 21 is 10101 which is palindrome
  • Its mirror is 37 which is also prime number. 37 is the 12th prime number
  • Binary representation of 73 is 1001001 which is palindrome

Human pee to charge cellphones

Scientists developed a technology from which human urine is used to charge small electric gazettes such as mobile. Beauty of this technology is that its eco-friendly and not dependent on external factors such as sun etc.

Please read complete article from here


Wednesday, July 17, 2013

World‘s most advanced computer dumber than a five-year old

Here is interesting article about Artificial Intelligence (AI) where engineers are trying to implement commonsense functionality in computer developed by MIT.

More details are here

Tuesday, July 16, 2013

Connected my blog to GOOGLE+

Today I connected my blog to GOOGLE+ . Its cool.

How to get one desire record by using WHERE and ORDER BY clause

CreatetblSettingusing following query

/****** Object:  Table [dbo].[tblSetting]    Script Date: 07/16/2013 11:43:49 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tblSetting](
      [ID] [int] IDENTITY(1,1) NOT NULL,
      [UniversityID] [int] NOT NULL,
      [CollegeID] [int] NOT NULL,
      [cItem] [varchar](50) NOT NULL,
      [cDescription] [varchar](200) NOT NULL,
      [cValue] [varchar](200) NOT NULL
CONSTRAINT [Pk_ID] PRIMARY KEY CLUSTERED
(
      [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


Get record according to College Level or university level or global level such that we get only one record
Conditions are

1. Get record according to college and University level i.e. where UniversityID AND CollegeID is non zero
2. Get record according to college level i.e. If 1st condition does not match then execute this where UniversityID is zero AND CollegeID is non zero
3. Get record according to University level i.e. If 1st, 2nd condition does not match then execute this where UniversityID is non zero AND CollegeID is zero
4. Get record according to global setting level i.e. If 1st, 2nd, 3rd condition does not match then execute this where both UniversityID AND CollegeID is zero

Get single record by using only one SELECT query.

I follow following way to get the desire result. Execute each condition one by one and check the result

Condition 4

Insert Following records where UniversityID AND CollegeID is zero

INSERT INTO dbo.tblSetting (UniversityID, CollegeID, cItem, cDescription, cValue) VALUES(0,0,'FileUploadPath','File Upload Path','\\XYZ\GlobalReports\')

SELECT TOP 1 * FROM tblSetting WHERE cItem = 'FileUploadPath' AND
(UniversityID = 177 OR UniversityID = 0) AND (CollegeID = 65 OR CollegeID = 0) ORDER BY CollegeID DESC , UniversityID DESC

Here our 1st, 2nd, 3rd conditions are not satisfied, however 4th condition is valid. So its output is

ID
UniversityID
CollegeID
cItem
cDescription
cValue
1
0
0
FileUploadPath
File Upload Path
\\XYZ\GlobalReports\

Condition 3

Insert Following records where UniversityID is non zero AND CollegeID is zero

INSERT INTO dbo.tblSetting (UniversityID, CollegeID, cItem, cDescription, cValue) VALUES(177,0,'FileUploadPath','File Upload Path','\\XYZ\UnivReports\')

SELECT TOP 1 * FROM tblSetting WHERE cItem = 'FileUploadPath' AND
(UniversityID = 177 OR UniversityID = 0) AND (CollegeID = 65 OR CollegeID = 0)ORDER BY CollegeID DESC , UniversityID DESC

Here 1st, 2nd, 4rd conditions are not satisfied, however 3rd condition is valid. So its output is

ID
UniversityID
CollegeID
cItem
cDescription
cValue
2
177
0
FileUploadPath
File Upload Path
\\XYZ\UnivReports\



Condition 2

Insert Following records where UniversityID is zero AND CollegeID is non zero

INSERT INTO dbo.tblSetting (UniversityID, CollegeID, cItem, cDescription, cValue) VALUES(0,65,'FileUploadPath','File Upload Path','\\XYZ\CollegeReports\')

SELECT TOP 1 * FROM tblSetting WHERE cItem = 'FileUploadPath' AND
(UniversityID = 177 OR UniversityID = 0) AND (CollegeID = 65 OR CollegeID = 0)ORDER BY CollegeID DESC , UniversityID DESC

Here our 1st, 3rd, 4th conditions are not satisfied, however 2th condition is valid. So its output is

ID
UniversityID
CollegeID
cItem
cDescription
cValue
3
0
65
FileUploadPath
File Upload Path
\\XYZ\CollegeReports\

Condition 4

Insert Following records where both UniversityID AND CollegeID is non zero

INSERT INTO dbo.tblSetting (UniversityID, CollegeID, cItem, cDescription, cValue) VALUES(177,65,'FileUploadPath','File Upload Path','\\XYZ\UnivCollegeReports\')

SELECT TOP 1 * FROM tblSetting WHERE cItem = 'FileUploadPath' AND
(UniversityID = 177 OR UniversityID = 0) AND (CollegeID = 65 OR CollegeID = 0)ORDER BY CollegeID DESC , UniversityID DESC

Here our 1st, 2nd, 3rd conditions are not satisfied, however 4th condition is valid. So its output is

ID
UniversityID
CollegeID
cItem
cDescription
cValue
4
177
65
FileUploadPath
File Upload Path
\\XYZ\UnivCollegeReports\

As you can analyze we are using same SELECT query for all output

SELECT TOP 1 * FROM tblSetting WHERE cItem = 'FileUploadPath' AND
(UniversityID = 177 OR UniversityID = 0) AND (CollegeID = 65 OR CollegeID = 0)ORDER BY CollegeID DESC , UniversityID DESC

I believe there are multiple ways of doing this in SQL Server. Let me know if you have any other way of achieving the desire result.

Please leave your comments / suggestion.