Tag sql-server-2008-r2

[Solved] HOW TO SUBSTRING in SQL QUERY

Just check this sample , you have to use charindex(to find the index of comma) and substring function to get substring value Declare @var varchar(50) = ‘21699,21712’ select charindex( ‘,’, @var), substring ( @var, charindex(‘,’, @var)+1, len(@var)) you can also…

[Solved] Getting Data separated by comma, [duplicate]

Like here, here or here. SELECT [TicketId], STUFF(( SELECT ‘, ‘ + [Name]) FROM [OneTable] WHERE ([TicketId] = OT.[TicketId]) FOR XML PATH(”),TYPE).value(‘(./text())[1]’,’VARCHAR(MAX)’) ,1,2,”) AS Name FROM [OneTable] OT GROUP BY [TicketId] Go and vote it up, then close this question.…

[Solved] SQL Server 2008 R2 stored procedures

Try passing null value for unused parameters and inside the stored procedure put a check for nulls to switch tables. CREATE PROCEDURE [dbo].[proc_search_patient_ByID] ( @PatID_pk int , @Cntid smallint, @FamID int ) AS SET NOCOUNT ON IF @Cntid IS NULL…

[Solved] T-SQL (Un)Pivot Table

I usually use dynamic sql. Something like this create table #T ( ID int, aText1 varchar(4), aText2 varchar(4), aInt1 int, aInt2 int ) insert into #T select 1, ‘ABC1’, ‘XYZ1’, 2, 20 union select 2, ‘ABC1’, ‘XYZ2’, 3, 25 union…