Pages

Showing posts with label SQL Server 2008. Show all posts
Showing posts with label SQL Server 2008. Show all posts

Tuesday, September 17, 2013

SQL Server Query for remove first / last character in a string

Hi now we can see how to remove first / last character in a string SQL Server.


Query for remove first char

Example

DECLARE @rollid NVARCHAR(MAX)
SET @rollid=',18,24,40,'
SELECT Substring(@rollid, 2, (len(@rollid))) as Rollno

Result
18,24,40,

Query for remove last character from string

Example

DECLARE @rollid NVARCHAR(MAX)
SET @rollid='18,24,40,'
SELECT Substring(@rollid, 1, (len(@rollid)-1)) as Rollno

Result
18,24,40

SQL Query to display rows value into single column

Query for display last one week record of rollnos and its displayed in single column with sepeartor in between nos

declare @rollid nvarchar(max)=''        
select @rollid=@rollid+convert(varchar(5012),rollno)+',' from studentrecord(nolock) where stuid in('18','16') and cdate >=DATEADD(day,-7, getdate())order by CONVERT(VARCHAR(10), cdate, 103) desc   
select @rollid