How to use Asp.net
  • Home
  • About us
  • Disclaimer
  • joy of helping
KEEP IN TOUCH

Posts in category Sql Server

ROW_NUMBER(), NTILE(), partition by, Duplicate Records, CTE sql server 2008

Feb08
2013
1 Comment Written by vikram

–Now create a table  tbemp1 and insert some records in it.

create table tbemp1(empno int, ename varchar(50), eadd varchar(50), esal int, edno int)

 

–Insert some records in it

insert tbemp1 values(1,’Amit’,'sample address’,12000,10)

insert tbemp1 values(2,’Raj’,'sample address’,14000,20)

insert tbemp1 values(3,’John’,'sample address’,18000,30)

insert tbemp1 values(4,’Rajni’,'sample address’,20000,10)

insert tbemp1 values(5,’Suraj’,'sample address’,18000,20)

insert tbemp1 values(6,’Rohit’,'sample address’,22000,10)

insert tbemp1 values(7,’Bharat’,'sample address’,12000,30)

GO

select * from tbemp1

– ROW_NUMBER() will display records with serial no.
select ROW_NUMBER() over (order by esal) as sr, esal,ename from tbemp1

– partition by It will display serial no. according to edno
select ROW_NUMBER() over (partition by edno order by esal desc) as sr, edno,esal,ename from tbemp1
READ MORE »

Tagged sql server

How to get second highest Salary sql server 2008

Feb08
2013
Leave a Comment Written by vikram

 

create table tbemp(empno int, ename varchar(50), eadd varchar(50), esal int, edno int)

GO

 

insert tbemp values(1,’Amit’,'sample address’,12000,10)

insert tbemp values(2,’Raj’,'sample address’,14000,20)

insert tbemp values(3,’John’,'sample address’,18000,30)

insert tbemp values(4,’Rajni’,'sample address’,20000,10)

insert tbemp values(5,’Suraj’,'sample address’,18000,20)

insert tbemp values(6,’Rohit’,'sample address’,22000,10)

insert tbemp values(7,’Bharat’,'sample address’,12000,30)

 

select * from tbemp

 

– second highest salary

select sr,empno,esal,ename from

(select empno,esal,ename, ROW_NUMBER() over(order by esal desc) as sr from tbemp) as p

where p.sr=2 order by esal desc

 

– another way to get second highest salary

select top 1 empno, esal,ename from

(select top 2 empno, esal,ename from tbemp order by esal desc) as p

order by esal asc

Tagged second highest salary, sql server

Insert values, insert into, insert default value, insert execute and select into sql server 2008

Feb08
2013
Leave a Comment Written by vikram

Insert statements

There are five types to save the records

1)      Insert values

2)      Insert into

3)      Insert Default value

4)      Insert Execute (It will execute store procedure)

5)      Select into (It will create table in runtimeand  fetch record from one table and create same structure but will not copy the constraints.

 

READ MORE »

Tagged insert, sql server

how to use isnull(), if, else if, while loop, return, select case in sql server

Apr21
2012
Leave a Comment Written by vikram

– Here i m writing how can we use
–1. delay/timer
–2. goto
–3. isnull()
–4. select case
–5. while loop
–6. return
–7. if else
–8. if else if
– in sql server.

–1. using WAIT FOR
Select ‘Before Delay’ as [wait/delay]
WAITFOR DELAY ’00:00:05′ — will wait for 5 seconds
Select ‘After Delay’ as [wait/delay]

————–
–2. using How can we use go to statement
– using GOTO statement
GOTO a
select ‘not accessing’ as [goto]
a:
select ‘accessing call2′ as [goto]

——————
–3. using ISNULL
declare @a1 int
set @a1 = null
select ISNULL(@a1,0) as [isnull]

——————-
–4. using SELECT CASE
declare @a2 varchar(50)
set @a2 = ‘ok’
Select CASE
when @a2 = ‘true’
then ’1′
else ‘false’
end as [select case]

———————-
–5. using WHILE LOOP
declare @a3 int
set @a3=1
while(@a3 < =2)
begin
select @a3 as [While Loop]
set @a3 = @a3+1
end

—————–
–6. using return
declare @a4 int
set @a4=1
while(@a4 < =10)
begin
select @a4 as [return]
if (@a4 = 2)
return — you can use here GOTO also
set @a4 = @a4+1
end

—————–
–7. using if else
declare @a5 int
set @a5=1
if(@a5=1)
select ‘true’ as [if else]
else
select ‘false’ as [if else]

—————–
–8. using if elseif kind of select case statement.
declare @a6 int
set @a6=3
if(@a6=1)
select ‘true’ as [else if]
else if(@a6=2)
select ‘in else 1′ as [else if]
else if(@a6=3)
select ‘in else 2′ as [else if]
else
select ‘in else’ as [else if]

with regards

vik

Returning complete words with a Left and Charindex in sql server

Jan23
2012
2 Comments Written by vikram

Here i m explaining that how can we display the full string without any half word e.g. 1st line output with header name string will let you know about the substring in which the last word ‘industry’ is not proper displaying as such i have used a substring function with 100 characters. so thats why its not displaying the full word. To overcome this problem i have tried  something after search on the web and posting it on my website  which might helpful for u .

* this query can help us where we have to display news with read more …

– below variable @Str decalaration

Declare @Str varchar(200)

– assigning the String in @Str

set @Str =’Lorem Ipsum is simply dummy text of the printing & typesetting industry. Lorem Ipsum has been the industry standard dummy text ever since the 1500s, when an unknown printer took a galley of type.’

– substring 100 characters from @Str
select substring(@Str,0,100) as string

 

– with left and charindex keyword displaying the full words.
select Left(Left(@Str, 100), LEN(Left(@Str, 100)) – CHARINDEX(‘ ‘, REVERSE(Left(@Str, 100)))) AS msg

OUPTUT

*click on the below image and see the red mark at the string header named output

with regards
vik

Getting the isoweek number sql server 2005

Jan21
2012
Leave a Comment Written by vikram

Hello there, Actually i was facing problem last year to get the exact week no. of the year.  i found the following code which will let us know the exact week no. of the year. so, i thought to put an article on my site. It will help to other also.  just copy and paste the following script into your database to get the isoweek / week number.

 

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

CREATE FUNCTION [dbo].[ISOweek]  (@DATE DATETIME)

RETURNS INT

AS

BEGIN

DECLARE @ISOweek INT

SET @ISOweek= DATEPART(wk,@DATE)+1

-DATEPART(wk,CAST(DATEPART(yy,@DATE) AS CHAR(4))+’0104′)

–Special cases: Jan 1-3 may belong to the previous year

IF (@ISOweek=0)

SET @ISOweek=dbo.ISOweek(CAST(DATEPART(yy,@DATE)-1

AS CHAR(4))+’12′+ CAST(24+DATEPART(DAY,@DATE) AS CHAR(2)))+1

–Special case: Dec 29-31 may belong to the next year

IF ((DATEPART(mm,@DATE)=12) AND

((DATEPART(dd,@DATE)-DATEPART(dw,@DATE))>= 28))

SET @ISOweek=1

RETURN(@ISOweek)

END

GO

 

——————————————————————————————————–

How to execute see the following example..

select getdate() as currentdate
select DBO.ISOWEEK(getdate()) as weekno

 

 

with Regards
vik

Rtrim, Ltrim, Substring, Charindex,Upper, Lower, Reverse In Sql Server 2005

May20
2011
1 Comment Written by vikram

I m just trying to some write down the some sql functions which is very useful for us as we know we can do these things in asp.net also but this is good if we have some knowledge about something better while developing something. I have just used some common functions which we use in our daily life while doing programming. Hope this will help you.
*Copying the following must replace the –> ‘ from keyboard.

[--] this sign is used for commenting the queries.
–Removing the spaces

DECLARE @str VARCHAR(50)
SET @str =’  Vikram Dhawan  ‘
select @str as [1. myname]
select LTRIM(@str)  as [2. LTRIM]
select RTRIM(@str)  as [3. RTRIM]
select LTRIM(RTRIM((@str)))  as [4. LTRIM/RTRIM]
–you can use here your table column name
–select LTRIM(RTRIM((yourcolumname))) from tablename
–SYNTAX SUBSTRING(expression,starting no.,length)

READ MORE »

Joy of Helping

Any sort of help to the children who are fatherless, poor & are from far-off areas.
Joy of Helping

Facebook Link

Asp dot Net ,Ajax,Xml.

Recent Posts

  • ROW_NUMBER(), NTILE(), partition by, Duplicate Records, CTE sql server 2008
  • How to get second highest Salary sql server 2008
  • Insert values, insert into, insert default value, insert execute and select into sql server 2008
  • How to Bind GridView with SqlDataReader in Asp.net
  • how to attach files to email without storing on disk using Asp.net FileUpload control

Recent Comments

  • Anonymous on ROW_NUMBER(), NTILE(), partition by, Duplicate Records, CTE sql server 2008
  • dhananjay on How to use GridView with Insert, Edit, Update, Delete the Ado.net way C#
  • dev on How can we limit the characters in multiline textbox asp.net using JavaScript
  • navneet on How to display image in Image control after upload on the server asp.net C#
  • AnhVu on How to do Shopping Cart in Asp.net C#

Archives

  • February 2013
  • September 2012
  • April 2012
  • January 2012
  • October 2011
  • August 2011
  • May 2011
  • April 2011
  • March 2011

Categories

  • Asp.net
  • jQuery
  • Sql Server

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

EvoLve theme by Theme4Press  •  Powered by WordPress How to use Asp.net