tagged [sql]

How to create a table from select query result in SQL Server 2008

How to create a table from select query result in SQL Server 2008 I want to create a table from select query result in SQL Server, I tried but I got an error > Incorrect syntax near the keyword 'AS'

22 May at 05:5

Declare Variable for a Query String

Declare Variable for a Query String I was wondering if there was a way to do this in MS SQL Server 2005: ``` DECLARE @theDate varchar(60) SET @theDate = '''2010-01-01'' AND ''2010-08-31 23:59:59''' ...

Insert all values of a table into another table in SQL

Insert all values of a table into another table in SQL I am trying to insert all values of one table into another. But the insert statement accepts values, but i would like it to accept a select * fro...

23 Feb at 03:27

Split function equivalent in T-SQL?

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 i...

30 Jan at 15:39

Compare DATETIME and DATE ignoring time portion

Compare DATETIME and DATE ignoring time portion I have two tables where column `[date]` is type of `DATETIME2(0)`. I have to compare two records only by theirs Date parts (day+month+year), discarding ...

SQL query for getting data for last 3 months

SQL query for getting data for last 3 months How can you get today's date and convert it to `01/mm /yyyy` format and get data from the table with delivery month 3 months ago? Table already contains de...

How to change a table name using an SQL query?

How to change a table name using an SQL query? How can I in change the table name using a query statement? I used the following syntax but I couldn't find the rename keyword in SQL server 2005.

24 Jan at 10:6

Resolve hostnames with t-sql

Resolve hostnames with t-sql How can i resolve a hostname in t-sql? a 2000 compatible method is preferred. Although something that works on 2005/2008 would also be helpful. eg. If i have the hostname...

9 Jun at 00:39

SQL Query to add a new column after an existing column in SQL Server 2005

SQL Query to add a new column after an existing column in SQL Server 2005 I need a SQL query which add a new column after an existing column, so the column will be added in a specific order. Please su...

25 Jul at 13:17

How to Alter a table for Identity Specification is identity SQL Server

How to Alter a table for Identity Specification is identity SQL Server --- not working Check Image I have a table ProductInProduct is want its id should be Unique.. ![enter image description here](htt...

25 Jun at 07:37

is not a recognized built-in function name

is not a recognized built-in function name Created a function When

SQL Query - how do filter by null or not null

SQL Query - how do filter by null or not null I want to filter a record.... If statusid is null, filter the record (where statusId is not null) If statusid is not null, filter the record where statusi...

Time part of a DateTime Field in SQL

Time part of a DateTime Field in SQL How would I be able to extract the time part of a DateTime field in SQL? For my project I have to return data that has a timestamp of 5pm of a DateTime field no ma...

How to use the divide function in the query?

How to use the divide function in the query? I want to show the percentage for the Overshipment column. My sample query Table Name - CSPGI09_OVERSHIPMENT My formula: ``` ------------------------------...

Refresh Application Automatically When Data changed in SQL Server

Refresh Application Automatically When Data changed in SQL Server I use SQL Server and I have 3 Application servers. When a table in my database have changed I need to those application servers refres...

How do I see active SQL Server connections?

How do I see active SQL Server connections? I am using SQL Server 2008 Enterprise. I want to see any active SQL Server connections, and the related information of all the connections, like from which ...

21 Jan at 21:33

How to insert a blob into a database using sql server management studio

How to insert a blob into a database using sql server management studio How can I easily insert a blob into a `varbinary(MAX)` field? As an example: thing I want to insert is: c:\picture.png the table...

19 Mar at 22:20

Get 2 Digit Number For The Month

Get 2 Digit Number For The Month I have an integer column "Month" I would like to get 2 digit number for month. This is what I have tried: DATEPART(mm, @Date) It returns one digit for months January t...

20 Feb at 09:6

How to generate a range of numbers between two numbers?

How to generate a range of numbers between two numbers? I have two numbers as input from the user, like for example `1000` and `1050`. How do I generate the numbers between these two numbers, using a ...

15 Sep at 20:44

Slow (to none) performance on SQL 2005 after attaching SQL 2000 database

Slow (to none) performance on SQL 2005 after attaching SQL 2000 database Issue: Using the detach/attach SQL database from a SQL 2000 SP4 instance to a much beefier SQL 2005 SP2 server. Run reindex, re...

Are there any disadvantages to always using nvarchar(MAX)?

Are there any disadvantages to always using nvarchar(MAX)? In SQL Server 2005, are there any disadvantages to making all character fields nvarchar(MAX) rather than specifying a length explicitly, e.g....

3 Feb at 13:5

Convert Month Number to Month Name Function in SQL

Convert Month Number to Month Name Function in SQL I have months stored in SQL Server as 1,2,3,4,...12. I would like to display them as January,February etc. Is there a function in SQL Server like Mon...

23 May at 14:9

Select statement to find duplicates on certain fields

Select statement to find duplicates on certain fields Can you help me with SQL statements to find duplicates on multiple fields? For example, in pseudo code: and from the above statement I would like ...

3 Sep at 09:40

SQL Server 2008: how do I grant privileges to a username?

SQL Server 2008: how do I grant privileges to a username? I need to be able to establish an ODBC connection through SQL Server authentication. In SSMS how do I grant permission for a user to be able t...

22 Oct at 16:3

SQL Query - Concatenating Results into One String

SQL Query - Concatenating Results into One String I have a sql function that includes this code: I need to concatenate all results from the select query into CodeNameString. Obviously a FOREACH loop i...

4 Mar at 16:22

how can I Update top 100 records in sql server

how can I Update top 100 records in sql server I want to update the top 100 records in SQL Server. I have a table `T1` with fields `F1` and `F2`. `T1` has 200 records. I want to update the `F1` field ...

8 Mar at 06:53

Sql server Integration Services 2008-2005 compatibility

Sql server Integration Services 2008-2005 compatibility I recently developed an SSIS package on my dev machine using the 2008 version. Later I foud the customer had Sql server 2005 and doesn't plan to...

Is there a way to list open transactions on SQL Server 2000 database?

Is there a way to list open transactions on SQL Server 2000 database? Does anyone know of any way to list open transactions on SQL Server 2000 database? I am aware that I can query the view `sys.dm_tr...

Is the LIKE operator case-sensitive with SQL Server?

Is the LIKE operator case-sensitive with SQL Server? In the [documentation about the LIKE operator](http://msdn.microsoft.com/en-us/library/ms179859.aspx), nothing is told about the case-sensitivity o...

21 Dec at 00:13

Error on renaming database in SQL Server 2008 R2

Error on renaming database in SQL Server 2008 R2 I am using this query to rename the database: But it shows an error when excuting: > Msg 5030, Level 16, State 2, Line 1 The database could not be exc...

22 May at 08:9

Reset identity seed after deleting records in SQL Server

Reset identity seed after deleting records in SQL Server I have inserted records into a SQL Server database table. The table had a primary key defined and the auto increment identity seed is set to “Y...

Safest way to get last record ID from a table

Safest way to get last record ID from a table In SQL Server 2008 and higher what is the best/safest/most correct way 1. to retrieve the ID (based on autoincrementing primary key) out of the database t...

6 Aug at 08:37

SQL update query using joins

SQL update query using joins I have to update a field with a value which is returned by a join of 3 tables. Example: ``` select im.itemid ,im.sku as iSku ,gm.SKU as GSKU ,mm.ManufacturerId as ...

How to update Identity Column in SQL Server?

How to update Identity Column in SQL Server? I have SQL Server database and I want to change the identity column because it started with a big number `10010` and it's related with another table, now I...

14 May at 07:13

How to kill/stop a long SQL query immediately?

How to kill/stop a long SQL query immediately? I am using SQL server 2008 and its management studio. I executed a query that yields many rows. I tried to cancel it via the red cancel button, but it ha...

10 Apr at 09:3

SQL Server - Return value after INSERT

SQL Server - Return value after INSERT I'm trying to get a the key-value back after an INSERT-statement. Example: I've got a table with the attributes name and id. id is a generated value. Now I want ...

27 Oct at 14:46

Add primary key to existing table

Add primary key to existing table I have an existing table called `Persion`. In this table I have 5 columns: - - - - - When I created this table, I set `PersionId` and `Pname` as the . I now want to i...

SQL, How to Concatenate results?

SQL, How to Concatenate results? I currently have a SQL query that returns a number of fields. I need one f the fields to be effectively a sub query sub that. > If I have a table X with two columns, M...

Get last 30 day records from today date in SQL Server

Get last 30 day records from today date in SQL Server I have small question about SQL Server: how to get last 30 days information from this table Sample data: `Product`: Based on this table data i wan...

Can I create a One-Time-Use Function in a Script or Stored Procedure?

Can I create a One-Time-Use Function in a Script or Stored Procedure? In SQL Server 2005, is there a concept of a one-time-use, or local function declared inside of a SQL script or Stored Procedure? I...

Convert SQL Server result set into string

Convert SQL Server result set into string I am getting the result in SQL Server as I am getting the output like The output parameter of the stored procedure is `@studentId` string and I want the retur...

27 Jan at 17:7

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

T-SQL datetime rounded to nearest minute and nearest hours with using functions In SQL server 2008, I would like to get datetime column rounded to nearest hour and nearest minute preferably with exist...

SQL DELETE with INNER JOIN

SQL DELETE with INNER JOIN There are 2 tables, `spawnlist` and `npc`, and I need to delete data from `spawnlsit`. `npc_templateid = n.idTemplate` is the only thing that "connect" the tables. I have tr...

SQL Server - Create a copy of a database table and place it in the same database?

SQL Server - Create a copy of a database table and place it in the same database? I have a table ABC in a database DB. I want to create copies of ABC with names ABC_1, ABC_2, ABC_3 in the same DB. How...

What does SQL Select symbol || mean?

What does SQL Select symbol || mean? What does `||` do in SQL?

14 Oct at 14:5

What represents a double in sql server?

What represents a double in sql server? I have a couple of properties in `C#` which are `double` and I want to store these in a table in SQL Server, but noticed there is no `double` type, so what is b...

How to delete the top 1000 rows from a table using Sql Server 2008?

How to delete the top 1000 rows from a table using Sql Server 2008? I have a table in SQL Server. I would like to delete the top 1000 rows from it. However, I tried this, but I instead of just deletin...

19 Aug at 19:29

Fastest way to update 120 Million records

Fastest way to update 120 Million records I need to initialize a new field with the value -1 in a 120 Million record table. I let it run for 5 hours before canceling it. I tried running it with transa...

31 Jan at 10:47

Copy data into another table

Copy data into another table How to copy/append data from one table into another table with same schema in SQL Server? let's say there is a query which creates `table1` with the same schema as well as...

21 Apr at 12:25

nvarchar(max) vs NText

nvarchar(max) vs NText What are the advantages and disadvantages of using the `nvarchar(max)` vs. `NText` data types in SQL Server? I don't need backward compatibility, so it is fine that `nvarchar(ma...