Questions tagged [tsql]

T-SQL (Transact Structured Query Language) is the extension of SQL functionality supported by Sybase ASE and Microsoft SQL Server. Do not use this tag for MySQL, PostgreSql, Oracle(Pl/SQL) related queries. Please note that SQL code which is being written using LINQ will not also be the part of this tag. This tag specifically created for advanced SQL programming using Microsoft SQL Server.

When to use this tag:
Use this tag for SQL questions for or .
Do not use this tag for questions about other database vendors, since T-SQL is only used by these two products.

Questions about T-SQL should be tagged appropriately with either or , and include the version as well.

How to write a good T-SQL question:
There are five ingredients to this recipe:

  1. Provide a clear textual explanation of the problem at hand.
  2. Provide proper sample data, as DDL (Create table statement(s)) and DML (Insert statement(s)). The sample data should be enough to demonstrate the problem, including edge cases. Usually, a few relevant rows are enough. (You can use SQLFiddle as an online tool to generate DDL and DML statements for you from tabular data using their Text To DDL button.)
  3. Provide expected output for the sample data you've posted.
  4. Show your attempts to solve the problem.
  5. Tag properly. In addition to , also provide the product tag (either or ), and the lowest version you need the solution for.
    (i.e. If you're developing on SQL Server 2016, but the production server is SQL Server 2012, Use .)

Do not include images of data or code!

A link to an online SQL test environment such as SQL Fiddle or DB Fiddle might help, but it is not a replacement for having all the data inside the question.
For more information, Read Why should I provide an MCVE for what seems to me to be a very simple SQL query? and Help me write this query in SQL

General Information

T-SQL (Transact Structured Query Language) is the extension of SQL functionality supported by Sybase ASE and Microsoft SQL Server. They share the name because Microsoft SQL Server was based on Sybase ASE code in a partnership that later dissolved. While the name is shared, functionality is not necessarily identical. With the introduction of Microsoft SQL Server 2005, the SQL Server code was completely re-written to provide integration with .Net.

Transact-SQL is central to using Microsoft SQL Server. All applications that communicate with an instance of SQL Server do so by sending Transact-SQL statements to the server, regardless of the user interface of the application.

It adheres to nearly all SQL standards, yet supplies an additional set of functionality not a part of the SQL standard, with concepts such as stored procedure, triggers , common table expressions, index hints, and the ability to SELECT scalars.

More information can be found in SQL Server Transact-SQL Reference and Sybase Transact-SQL User's Guide.

Free online T-SQL courses

72684 questions
141
votes
6 answers

What is a "batch", and why is GO used?

I have read and read over MSDN, etc. Ok, so it signals the end of a batch. What defines a batch? I don't see why I need go when I'm pasting in a bunch of scripts to be run all at the same time. I've never understood GO. Can anyone explain this…
PositiveGuy
  • 46,620
  • 110
  • 305
  • 471
141
votes
6 answers

How to truncate string using SQL server

i have large string in SQL Server. I want to truncate that string to 10 or 15 character Original string this is test string. this is test string. this is test string. this is test string. Desired string this is test string. this is ......
SanamShaikh
  • 1,699
  • 3
  • 13
  • 22
140
votes
6 answers

T-SQL datetime rounded to nearest minute and nearest hours with using functions

I would like to get datetime column rounded to nearest hour and nearest minute preferably with existing functions. For this column value 2007-09-22 15:07:38.850, the output will look like: 2007-09-22 15:08 -- nearest minute 2007-09-22 15 --…
user219628
  • 3,755
  • 8
  • 35
  • 37
140
votes
17 answers

Integer PadLeft function in T-SQL

I have the following table A: id ---- 1 2 12 123 1234 I need to left-pad the id values with zero's: id ---- 0001 0002 0012 0123 1234 How can I achieve this?
Gali
  • 14,511
  • 28
  • 80
  • 105
140
votes
6 answers

Getting result of dynamic SQL into a variable for sql-server

Executing dynamic SQL as follows in Stored Procedure: DECLARE @sqlCommand nvarchar(1000) DECLARE @city varchar(75) SET @city = 'London' SET @sqlCommand = 'SELECT COUNT(*) FROM customers WHERE City = @city' EXECUTE sp_executesql @sqlCommand, N'@city…
Peter Lindholm
  • 2,420
  • 5
  • 23
  • 27
140
votes
6 answers

SQL Server Regular expressions in T-SQL

Is there any regular expression library written in T-SQL (no CLR, no extended SP, pure T-SQL) for SQL Server, and that should work with shared hosting? Edit: Thanks, I know about PATINDEX, LIKE, xp_ sps and CLR solutions I also know it is not the…
xnagyg
  • 4,784
  • 2
  • 32
  • 24
139
votes
16 answers

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?
jinsungy
  • 10,717
  • 24
  • 71
  • 79
139
votes
11 answers

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

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 string into rows of data: Lets say I have…
kristof
  • 52,923
  • 24
  • 87
  • 110
138
votes
28 answers

How do you list the primary key of a SQL Server table?

Simple question, how do you list the primary key of a table with T-SQL? I know how to get indexes on a table, but can't remember how to get the PK.
swilliams
  • 48,060
  • 27
  • 100
  • 130
138
votes
5 answers

How much size "Null" value takes in SQL Server

I have a large table with say 10 columns. 4 of them remains null most of the times. I have a query that does null value takes any size or no size in bytes. I read few articles some of them are saying…
Rocky Singh
  • 15,128
  • 29
  • 99
  • 146
138
votes
4 answers

SQL Server ':setvar' Error

I am trying to create some script variables in T-SQL as follows: /* Deployment script for MesProduction_Preloaded_KLM_MesSap */ GO SET ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, ARITHABORT, CONCAT_NULL_YIELDS_NULL,…
Randy Minder
  • 47,200
  • 49
  • 204
  • 358
137
votes
3 answers

SQL Server: Maximum character length of object names

What is the maximum character length of object name (e.g. constraint, column) in SQL Server 2008?
jrara
  • 16,239
  • 33
  • 89
  • 120
137
votes
16 answers

How do I find a default constraint using INFORMATION_SCHEMA?

I'm trying to test if a given default constraint exists. I don't want to use the sysobjects table, but the more standard INFORMATION_SCHEMA. I've used this to check for tables and primary key constraints before, but I don't see default constraints…
WildJoe
  • 5,740
  • 3
  • 26
  • 30
136
votes
17 answers

T-SQL query to show table definition?

What is a query that will show me the full definition, including indexes and keys for a SQL Server table? I want a pure query - and know that SQL Studio can give this to me, but I am often on "wild" computers that have only the most bare-bones apps…
Daniel Williams
  • 8,912
  • 15
  • 68
  • 107
136
votes
15 answers

Replace duplicate spaces with a single space in T-SQL

I need to ensure that a given field does not have more than one space (I am not concerned about all white space, just space) between characters. So 'single spaces only' needs to be turned into 'single spaces only' The below will not…
Christoph
  • 4,251
  • 3
  • 24
  • 38