Questions tagged [sql-server]

Microsoft SQL Server is a relational database management system (RDBMS). Use this tag for all SQL Server editions including Compact, Express, Azure, Fast-track, APS (formerly PDW) and Azure SQL DW. Do not use this tag for other types of DBMS (MySQL, PostgreSQL, Oracle etc.). Do not use this tag for ...

308
votes
9answers
156k views

Simulating group_concat MySQL function in Microsoft SQL Server 2005?

I'm trying to migrate a MySQL-based app over to Microsoft SQL Server 2005 (not by choice, but that's life).In the original app, we used almost entirely ANSI-SQL compliant statements, with one ...
1458
votes
43answers
1.8m views

How to concatenate text from multiple rows into a single text string in SQL server?

Consider a database table holding names, with three rows:PeterPaulMaryIs there an easy way to turn this into a single string of Peter, Paul, Mary?
938
votes
38answers
292k views

Parameterize an SQL IN clause

How do I parameterize a query containing an IN clause with a variable number of arguments, like this one?SELECT * FROM Tags WHERE Name IN ('ruby','rails','scruffy','rubyonrails')ORDER BY Count ...
137
votes
6answers
133k views

SQL Server dynamic PIVOT query?

I've been tasked with coming up with a means of translating the following data:date category amount1/1/2012 ABC 1000.002/1/2012 DEF 500.002/1/2012 ...
445
votes
41answers
761k views

How do I split a string so I can access item x?

Using SQL Server, how do I split a string so I can access item x?Take a string "Hello John Smith". How can I split the string by space and access the item at index 1 which should return "John"?
265
votes
2answers
573k 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:------------------------...
1154
votes
36answers
982k views

How can I remove duplicate rows?

What is the best way to remove duplicate rows from a fairly large SQL Server table (i.e. 300,000+ rows)?The rows, of course, will not be perfect duplicates because of the existence of the RowID ...
2978
votes
27answers
3.3m views

How do I UPDATE from a SELECT in SQL Server?

In SQL Server, it's possible to insert into a table using a SELECT statement:INSERT INTO Table (col1, col2, col3)SELECT col1, col2, col3 FROM other_table WHERE sql='cool'Is it also possible ...
1318
votes
24answers
1.6m views

Insert results of a stored procedure into a temporary table

How do I do a SELECT * INTO [temp table] FROM [stored procedure]? Not FROM [Table] and without defining [temp table]?Select all data from BusinessLine into tmpBusLine works fine.select *into ...
291
votes
16answers
385k views

How to use GROUP BY to concatenate strings in SQL Server?

How do I get:id Name Value1 A 41 B 82 C 9toid Column1 A:4, B:82 C:9
134
votes
14answers
186k views

Calculate a Running Total in SQL Server

Imagine the following table (called TestTable):id somedate somevalue-- -------- ---------45 01/Jan/09 323 08/Jan/09 512 02/Feb/09 077 14/Feb/09 739 20/...
77
votes
7answers
96k views

Why do we always prefer using parameters in SQL statements?

I am very new to working with databases. Now I can write SELECT, UPDATE, DELETE, and INSERT commands. But I have seen many forums where we prefer to write:SELECT empSalary from employee where salary ...
212
votes
7answers
592k views

Convert Rows to columns using 'Pivot' in SQL Server

Well I guess I am really dense. I have read the stuff on MS pivot tables and I am still having problems getting this correct. I have seen some of you that seem to eat and sleep this stuff, so I just ...
158
votes
11answers
296k views

Turning a Comma Separated string into individual rows

I have a SQL Table like this:| SomeID | OtherID | Data+----------------+-------------+-------------------| abcdef-..... | cdef123-... | 18,20,22| abcdef-..... | 4554a24-... | 17,...
290
votes
8answers
331k views

How do I obtain a Query Execution Plan?

In Microsoft SQL Server how can I get a query execution plan for a query / stored procedure?
199
votes
4answers
50k views

What makes a SQL statement sargable?

By definition (at least from what I've seen) sargable means that a query is capable of having the query engine optimize the execution plan that the query uses. I've tried looking up the answers, but ...
607
votes
11answers
301k views

Count(*) vs Count(1) - SQL Server

Just wondering if any of you guys use Count(1) over Count(*) and if there is a noticeable difference in performance or if this is just a legacy habit that has been brought forward from days gone past?...
135
votes
11answers
161k views

T-SQL: Opposite to string concatenation - how to split string into multiple records [duplicate]

Possible Duplicate:Split string in SQL I have seen a couple of questions related to string concatenation in SQL.I wonder how would you approach the opposite problem: splitting coma delimited ...
260
votes
32answers
1.0m views

Why am I getting “Cannot Connect to Server - A network-related or instance-specific error”?

I get the following error when trying to connect to SQL Server:Cannot connect to 108.163.224.173. A network-related orinstance-specific error occurred while establishing a connection to...
84
votes
32answers
657k views

How to split a comma-separated value to columns

I have a table like thisValue String-------------------1 Cleo, SmithI want to separate the comma delimited string into two columnsValue Name Surname-------------------1 Cleo ...
99
votes
6answers
55k views

Identity increment is jumping in SQL Server database

In one of my tables Fee in column "ReceiptNo" in SQL Server 2012 database identity increment suddenly started jumping to 100s instead of 1 depending on the following two things.if it is 1205446 it ...
115
votes
15answers
198k 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?
1372
votes
37answers
2.1m views

How to return only the Date from a SQL Server DateTime datatype

SELECT GETDATE()Returns: 2008-09-22 15:24:13.790I want that date part without the time part: 2008-09-22 00:00:00.000How can I get that?
121
votes
6answers
187k views

SQL Server: Examples of PIVOTing String data

Trying to find some simple SQL Server PIVOT examples. Most of the examples that I have found involve counting or summing up numbers. I just want to pivot some string data. For example, I have a ...
75
votes
4answers
133k views

How to make a query with group_concat in sql server [duplicate]

I know that in sql server we cannot use Group_concat function but here is one issue i have in which i need to Group_Concat my query.I google it found some logic but not able to correct it.My sql query ...
872
votes
11answers
649k views

Best way to get identity of inserted row?

What is the best way to get IDENTITY of inserted row?I know about @@IDENTITY and IDENT_CURRENT and SCOPE_IDENTITY but don't understand the pros and cons attached to each.Can someone please explain ...
81
votes
7answers
377k views

Simple way to transpose columns and rows in Sql?

How do I simply switch columns with rows in SQL?Is there any simple command to transpose?ie turn this result:Paul | John | Tim | EricRed 1 5 1 3Green 8 ...
54
votes
7answers
14k views

SQL, Auxiliary table of numbers

For certain types of sql queries, an auxiliary table of numbers can be very useful. It may be created as a table with as many rows as you need for a particular task or as a user defined function that ...
1007
votes
14answers
1.2m views

How can I do an UPDATE statement with JOIN in SQL?

I need to update this table in SQL Server 2005 with data from its 'parent' table, see below:saleid (int)udid (int)assid (int)udid (int)assid (int)sale.assid contains the correct value ...
423
votes
10answers
1.1m views

NOT IN vs NOT EXISTS

Which of these queries is the faster?NOT EXISTS:SELECT ProductID, ProductName FROM Northwind..Products pWHERE NOT EXISTS (SELECT 1 FROM Northwind..[Order Details] od WHERE p....

153050per page
angop.ao, elkhabar.com, noa.al, afghanpaper.com, bbc.com, time.com, cdc.gov, nih.gov, xnxx.com, github.com,