tsql

SQL Server 2008: Bulk Datatype Change

I have an SQL Server 2008 database with many tables. I've been using the now lame datetime datatype and want to use the new and better datetime2. In most places where I have a datetime field, the corresponding column name is Timestamp. Is there anywhere to do a bulk change from datatime to datetime2? ...

How can you use variables or references in SQL?

In the following: (running a query on the Stack Exchange Data Explorer, which uses an SQL Azure implementation of OData. The FAQ says it supports most of the TSQL commands): DECLARE @MinPosts int = ##MinNumberOfPosts## SELECT Id AS [User Link], Reputation, (SELECT COUNT(*) FROM posts WHERE posts.OwnerUserId = Users.I...

pass 2 parameters and use "like" in SQL Server 2005 Stored procedure

Hi, I want to pass 2 parameters to SQL stored procedure and use "like" statement as so: CREATE PROCEDURE dbo.StoredProcedure1 @project varchar(max) @group varchar(50) AS BEGIN SELECT * FROM table1 WHERE Projectname = @project and Resource_ID like '%' + @group + '%' END It says that there is incorrect syntax near @group a...

Full-Text search on a table column issue

I have a Table that I want to search its title(nvarchar(max)) column. But I am getting an error when I create an index over the title column so I can enable Full Text search on it. I am going to use the Contains keyword to do the job. Any ideas? Thanks, ...

Return a subset of rows with a complex order by in T-SQL

I have a stored proc select statement that lookes like that: SELECT p.ID AS ID, p.Title AS Title, p.Text AS Text, p.CategoryID AS CategoryID, p.PostDate AS PostDate, p.Author AS Author, p.AuthorID AS AuthorID, p.IsApproved AS IsApproved, p.Rating AS Rating, p.RatesCount AS RatesCount, t.Text A...

SQL Not Like Statement

How can I select table from sys.tables where table name does not containt special word (which pass in parameters). I want to select all tables where its contains word 'customer' but not those who ends with 'old' in name. TableName In DB customer1 customer2 customer3 customerold1 customerold2 Output Wanted customer1 customer2 c...

Selecting the top 5 unique rows, sorted randomly in SQL Server 2005?

I have a table in SQL Server 2005 that looks like 1 | bob | joined 2 | bob | left 3 | john | joined 4 | steve | joined 5 | andy | joined 6 | kyle | joined What I want is to give someone the ability to pull up the activity of 5 random users (showing their latest activity) ex: I want to return results 1, 3, 4, 5, 6 - or - 2, 3, 4, 5, ...

how to return number of records efficiently in SQL?

Hello everyone, I am using SQL Server 2008 Enterprise on Windows Server 2008 Enterprise. I am using the following code to return a part of data for a query to implement paging (i.e. page up/down to show a part of result at each page, like Google search result paging) on my web application (I use pageCount as number of results showed on ...

Combining Union results

I have the below SQL Query select Count(emailID) as ViewsThatMonth, Day(entry_date) as day, Month(entry_date) as month, Year(entry_date) as year from email_views where emailID = 110197 Group By Day(entry_date), Month(entry_date), Year(entry_date) UNION ALL select Count(emailID) as ViewsThatMon...

Switch Case in T-SQL In where Clause

Hello Experts i have A Sp , which Contains lot of if/else Condition Please help me how to use Switch Case in T-SQL In where Clause. here is my Query if (@Form ='page.aspx') begin select DT.Column,DST.Column1, DST.Code from Table DT join Table2 DST on DT.ID= DST.ID where DT.code = 'XX1' and DT.CDID = @cdid end i...

Incrementing a counter for dateadd

Hello, I'm trying to increase the time between dates by a set amount. For example, I want to add two months to the time between date 1,2 and 3. I'm having trouble incrementing my 'date counter'... DECLARE @monthDiff int SET @monthDiff = 1; UPDATE [Table1] SET [Date] = DATEADD(MONTH, (SET @monthDiff = @monthDiff + 1), [Date]) WHERE ...

Convert From Bigint to datetime value

Hi people, please help me with this one, i want to convert a value from Bigint to datetime. For example im reading HISTORY table of teamcity server, on the field *build_start_time_server* i have this value on one record 1283174502729. How can i convert to datetime value??? Thanks ...

SQL SERVER Foreign Keys and Indexes

Is it a good practice to create an index to every foreing key in my database? ...

How do I select records less than 1 year old...

Howdy, this should be a fairly simple question but I know very little about SQL. I have a database which has the following fields: client_id, scheduled_amount, deposit_amount, message_code_id, note_text, system_date now I wish to select all the records that are less than 1 year old from when the sql statement is run. I know I should u...

Junction tables in T-SQL

In my database application, when I try to attach a page to a site, I get the following error: System.InvalidOperationException: Can't perform Create, Update or Delete operations on 'Table(Junc_Page_Site)' because it has no primary key. I intend a page to be able to be added to multiple sites - by using a junction table. Below is the ...

Returning the parent/ child relationship on a self-joining table...

Hi, I need to be able to return a list of all children given a parent Id at all levels using SQL. The table looks something like this: ID ParentId Name --------------------------------------- 1 null Root 2 1 Child of Root 3 2 Child of Child of Root Give an Id of '1', how would I return the entire...

Help writing SQL query..

Possible Duplicate: Need help with a SQL query that combines adjacent rows into a single row So this is how my table looks. ..and I need to write a query to get the output like this: This is not a homework question. ...

SQL - select * from X WHERE (if value is numeric, else ...)

I would need to make a DB select that will behave differently when number or text is entered, just different condition should be used. I think it should be done but not quite sure with the syntax (MSSQL). Thank you I would need: SELECT * FROM X WHERE (IF value passed is numeric = "ID" ELSE "NAME") = Value //ID or Name are columns ...

SQL Server Query to bring groupwise multiplication

When a table having following value Say TableA **Grp Value** Grp1 2 Grp1 5 Grp1 3 Grp2 3 Grp2 -5 Grp2 -2 Grp3 4 Grp3 0 Grp3 1 Grp4 -2 Grp4 -4 Grp5 7 Grp5 NULL Grp6 NULL Grp6 NULL Grp7 -1 Grp7 ...

SQL Server 2000 Dump Statement

Hi I'm migrating a few databases from sql 2000 to sql 2008. while running upgrade advisor I got a message that says that there are objects with the Dump/Load sintax. that is not on use anymore. So I found the following text in a proc Dump DataBase @name to @path With Init Further investigating I discovered that this was a backup and...