SQL Server provides a function named Coalesce to do the task to join all the column value by comma separated string.
The Query is below:
Go
Declare @strEmail varchar(max)
SELECT @strEmail=coalesce(@strEmail+',','')+[Email]
FROM Users
print @strEmail
Life is a Short Span, try to earn knowledge in every Field.
Showing posts with label SQL. Show all posts
Showing posts with label SQL. Show all posts
Friday, 1 February 2013
Monday, 12 March 2012
FIND Nth Maximum value and Nth minimum Value from a colum in a table in MS-Sql Server.
FIND Nth Maximum value
SELECT id, amount FROM(SELECT id, amount, Row_Number() OVER(ORDER BY amount DESC) AS highest FROM @tmp ) as x WHERE highest = 3 -- FIND Nth Minimum value
SELECT id, amount FROM(SELECT id, amount, Row_Number() OVER(ORDER BY amount ASC) AS lowest FROM @tmp ) as x WHERE lowest = 3
Second Highest Salary From a table( such as Employee Table)
Sometimes it requires to find the second most from a column(say second highest salary from employee table) in a table, to do it write the Query below:
select max(salary) from employee where salary not in (select max(salary) from employee );
select max(salary) from employee where salary not in (select max(salary) from employee );
Subscribe to:
Posts (Atom)