414
votes
17answers
591k views

How to SELECT * INTO [temp table] FROM [stored procedure]

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 ...
145
votes
11answers
88k views

varchar vs nvarchar performance

I'm working on a database for a small web app at my school using SQL Server 2005. I see a couple of schools of thought on the issue of varchar vs nvarchar: Use varchar unless you deal with a lot of ...
136
votes
16answers
236k views

Split string in SQL

Using SQL Server 2005, how do I split a string so I can access item x? For example, take the string "Hello John Smith". How can I split the string by a space and access the item at index 1 which ...
125
votes
6answers
111k views

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

I need to update table in SQL Server 2005 with data from its 'parent' table, see below: sale --------- id (int) udid (int) assid (int) ud --------- id (int) assid (int) sale.assid contains the ...
108
votes
10answers
81k views

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 best to use, decimal or float? This will store ...
89
votes
8answers
23k views

How do I create unique constraint that also allows nulls in sql server

The question is quite simple, as the title. Added a column to table I want a unique constraint , the column will insert guids, but to allow for existing data before column was added I need to allow ...
75
votes
7answers
127k views

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 ManuId ...
67
votes
8answers
71k views

Create a date with T-SQL

I am trying to convert a date with individual parts such as 12, 1, 2007 into a datetime in SQL Server 2005. I have tried the following: CAST(DATEPART(year, DATE)+'-'+ DATEPART(month, DATE) +'-'+ ...
62
votes
10answers
48k views

Drop all the tables, stored procedures, triggers, constriants and all the dependencies in one sql statement

Is there any way in which I can clean a db in SQl Server 2005 i.e by dropping all the tables and deleting stored procedures, triggers, constraints and all the dependencies in one sql statement. ...
56
votes
5answers
49k views

Turn off constraints temporarily

I'm looking the way to temporarily turn off all DB's constraints (eg table relationships) I need to copy (using INSERTs) one DBs tables to another DB I know I can archive that by executing commands ...
55
votes
4answers
21k views

Dynamic SELECT TOP @var In SQL Server

How can I have a dynamic variable setting the amount of rows to return in SQL Server? Below is not valid syntax in SQL Server 2005+: DECLARE @count int SET @count = 20 SELECT TOP @count * FROM ...
48
votes
11answers
80k views

How to avoid the “divide by zero” error in SQL?

I hate this error message: Msg 8134, Level 16, State 1, Line 1 Divide by zero error encountered. What is the best way to write SQL code, so that I will never see this error message again? I mean, ...
23
votes
3answers
49k views

Convert varchar to uniqueidentifier in SQL Server

A table I have no control of the schema for, contains a column defined as varchar(50) which stores uniqueidentifiers in the format 'a89b1acd95016ae6b9c8aabb07da2010' (no hyphens) I want to convert ...
8
votes
5answers
8k views

Rounding SQL DateTime to midnight

I am having a small problem with my SQL query. I'm using the GETDATE function, however, let's say I execute the script at 5PM, it will pull up records between 12/12/2011 5PM to 12/18/2011 5PM. How can ...
2
votes
4answers
19k views

How to copy a table schema and constraints to a table of different database?

I need an sql which will copy schema of a specified table to a table in different db. How to implement this? Please help.

1 2 3 4 5 434
15 30 50 per page