All Questions

240
votes
2answers
511846 views

Efficiently convert rows to columns in sql server

I'm looking for an efficient way to convert rows to columns in SQL server, I heard that PIVOT is not very fast, and I need to deal with lot of records. This is my example: -----------------------...
77
votes
30answers
601583 views

How to split a comma-separated value to columns

I have a table like this Value String ------------------- 1 Cleo, Smith I want to separate the comma delimited string into two columns Value Name Surname ------------------- 1 Cleo ...
72
votes
21answers
232660 views

T-SQL split string

I have a SQL Server 2008 R2 column containing a string which I need to split by a comma. I have seen many answers on StackOverflow but none of them works in R2. I have made sure I have select permissi...
111
votes
15answers
190133 views

Split function equivalent in T-SQL?

I’m looking to split '1,2,3,4,5,6,7,8,9,10,11,12,13,14,15...' (comma delimited) into a table or table variable. Does anyone have a function that returns each one in a row?
54
votes
2answers
19423 views

Using merge..output to get mapping between source.id and target.id

Very simplified, I have two tables Source and Target. declare @Source table (SourceID int identity(1,2), SourceName varchar(50)) declare @Target table (TargetID int identity(2,2), TargetName varchar(...
65
votes
5answers
87343 views

Can I Comma Delimit Multiple Rows Into One Column?

I am attempting to merge something like this in my SQL Server database: [TicketID], [Person] T0001 Alice T0001 Bob T0002 Catherine T0002 Doug T0003 Elaine Into thi...
12
votes
3answers
37284 views

How to cast the DateTime to Time

I am casting DateTime field to Time by using CAST Syntax. select CAST([time] as time) as [CSTTime] DateTime 2015-03-19 00:00:00.000 Present Output : Time 03:05:36.0000000 I need only HH:MM:SS and...
118
votes
14answers
184247 views

How to get cumulative sum

declare @t table ( id int, SomeNumt int ) insert into @t select 1,10 union select 2,12 union select 3,3 union select 4,15 union select 5,23 select * from @t the above se...
47
votes
6answers
119929 views

SQL Server, How to set auto increment after creating a table without data loss?

I have a table table1 in SQL server 2008 and it has records in it. I want the primary key table1_Sno column to be an auto-incrementing column. Can this be done without any data transfer or cloning ...
5
votes
1answers
22420 views

Pivot Dynamic Columns, no Aggregation

I have questionnaire data in, SQL Server 2008, that I want to transpose to a matrix. I saw several posts about the same topic, but I just don't get pivoting. Given are following tables: Question...

Previous Next