tsql

How to filter the columns?

Using SQL Server 2005 I want to filter the column where column value not equal to numeric Table1 Status 010203 Absent 231415 Ramesh Suresh ..., I want to get only Names from the status column like Tried Query Select Status from table1 where status <> 'absent' and status <> numecivalue How to mentioned status <> numericvalue E...

T-SQL: Pivot but for semicolon-separated values instead of columns.

I've got semicolon-separated values in a column Values in my table: Values 1;2;3;4;5 I would like to transform it in a procedure to have there values as rows: Values 1 2 3 4 5 How could I do it in T-SQL? ...

T-SQL: finding a group by its members

Given the following two tables in SQL Server 2005: IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'GroupItems') DROP TABLE GroupItems; CREATE TABLE GroupItems ( RowID INT IDENTITY(1,1) PRIMARY KEY , GroupID CHAR(1) , ItemID INT ); IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME...

Less repetition in where-clauses when querying many columns

Take a standard broad search query...: DECLARE @sq Varchar(50) = 'Desperate' SELECT * FROM [UnbelievablyHotWomenOrAtLeastAcceptable] u WHERE u.Address LIKE '%' + @sq + '%' OR u.City LIKE '%' + @sq + '%' OR u.firstname LIKE '%' + @sq + '%' OR u.Lastname LIKE '%' + @sq + '%' OR u.Email LIKE '%' + @sq + '%' OR u.Notes LIKE '%' + @sq + '...

SQL Group By One Field and Order By Another Field

The Table: declare @Table table ( id int, ticketid int, sponsor int, dev int, qa int, savedate datetime ) insert into @Table values (1,100,22,0, 0, '2008-10-29 11:17:59.527') insert into @Table values (2,100,5,0, 0, '2008-10-29 11:00:37.030') insert into @Table values (3,101,22,0, 0, '2009-10-29 11:10:27.687') ins...

Not able to remove injected script from database rows

I've been handed a MS SQL 2000 database which has been injected with malware. The malware script is as follows: <script src=http://www.someAddress.ru/aScript.js&gt;&lt;/script&gt; Now I want to remove this piece of code from the table rows. As a test, I inputed < h1> Test < /h1> on a row, and successfully ran the following query: UP...

Creating tables with something similar to INFORMATION_SCHEMA.VIEW_DEFINITION in SQL Server

Is there a standard method to retrieve the 'CREATE TABLE..' definition of a table in SQL Server? I can ask INFORMATION_SCHEMA for definitions of views and functions/procedures, so I thought it would be natural to get the same for tables, but I didn't find anything. ...

DROP...CREATE vs ALTER

When it comes to creating stored procedures, views, functions, etc., is it better to do a DROP...CREATE or an ALTER on the object? I've seen numerous "standards" documents stating to do a DROP...CREATE, but I've seen numerous comments and arguments advocating for the ALTER method. The ALTER method preserves security, while I've heard...

How do I add a column to large sql server table

I have a SQL Server table in production that has millions of rows, and it turns out that I need to add a column to it. Or, to be more accurate, I need to add a field to the entity that the table represents. Syntactically this isn't a problem, and if the table didn't have so many rows and wasn't in production, this would be easy. Reall...

Alternative language to TSQL for data handling

If I wanted to create my own relational database with a modern language to replace TSQL, what language would that be? Or if I end up creating my own language, what features would I have to include to make it better than TSQL ? ...

GET ISNULL from COMPLEX FUNCTION

I have a SQL Table UDF That gets a Standard Deviation from a 20 day moving average of values... The table its computing from is: Tickers([date] datetime, [close] numeric(7,2)) The Function computes a Table GetStDev([date] datetime, stddev numeric(7,2). The last row of the stddev column is always NULL (due to STDEV calc?)... I need to r...

Fetching join using @JoinTable with SQLQuery

I have a mapped entity with a property "latestHistory", which is mapped through a join table, like: class Record { @OneToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REMOVE }, fetch = FetchType.LAZY, optional = true) @JoinTable(name = "latest_history_join_view", joinColumns = { @JoinColumn(name = "record_id")...

is there a code-coverage tool for the TSQL sproc unit tests?

I am looking for a unitttest and code-coverage tool for TSQL sprocs. Can anyone recommend a good one? Commercial or free. ...

T-SQL - GROUP BY with LIKE - is this possible?

Is there a way to include a LIKE expression in a GROUP BY query? For example: SELECT Count(*) from tblWhatever GROUP BY column_x [LIKE %Fall-2009%] column_x: -------- BIOL-Fall_2009 HIST Fall_2009 BIOL Spring_2009 Result: ------ Fall_2009 2 Spring_2009 1 ...

what is the query to get "related tags" like in stack overflow

i have 3 tables: links (id, linkName) tags (id, tagName) tagsBridge (tagID, linkID) i am trying to support showing related tags like in SOF. so if you click on tags "XYZ", right now i am showing all the links with tag "XYZ" but i also want to show the distinct list of all other tags that people have tagged those items that also h...

Equivalent to VB AndAlso in SQL?

Is there an equivalent to VB's AndAlso/OrElse and C#'s &&/|| in SQL (SQL Server 2005). I am running a select query similar to the following: SELECT a,b,c,d FROM table1 WHERE (@a IS NULL OR a = @a) AND (@b IS NULL OR b = @b) AND (@c IS NULL OR c = @c) AND (@d IS NULL OR d = @d) For example, if the "@a" parameter passed in as NULL ther...

Does the SQL Server 2005 database size property include the full-text index?

The title pretty much says it all, but in SQL Server 2005 Management Studio, if I look at the database properties and pick up the size value, will that include the full-text index data? Does it include the log file as well? Rough values are fine, but some of our ft indexes can be large, so I want to be sure of this. ...

SQL Query 'invalid column name'

The following sql query works fine if I leave the four commented SET statements commented out. However, if I uncomment them, or just say I uncomment the first one SET @StoreID = tt_StoreID then I get the following message Invalid column name 'tt_StoreID' Why doesn't it recognize it, and how do I fix it ? *---------------------...

TSQL Conditionally Select Specific Value

This is a follow-up to #1644748 where I successfully answered my own question, but Quassnoi helped me to realize that it was the wrong question. He gave me a solution that worked for my sample data, but I couldn't plug it back into the parent stored procedure because I fail at SQL 2005 syntax. So here is an attempt to paint the broader...

Storing a 30KB BLOB in SQL Server 2005

My data is 30KB on disk (Serialized object) was size should the binary field in t-sql be? Is the brackets bit bytes ? ... so is binary(30000) .... 30KB? Thanks ...