Search for one value in any column of any table inside a database

How to search all columns of all tables in a database for a keyword? http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm EDIT: Here’s the actual T-SQL, in case of link rot: CREATE PROC SearchAllTables ( @SearchStr nvarchar(100) ) AS BEGIN — Copyright © 2002 Narayana Vyas Kondreddi. All rights reserved. — Purpose: To search all columns of all tables for a … Read more

Testing for inequality in T-SQL

These 3 will get the same exact execution plan declare @id varchar(40) select @id = ‘172-32-1176’ select * from authors where au_id <> @id select * from authors where au_id != @id select * from authors where not (au_id = @id) It will also depend on the selectivity of the index itself of course. I … Read more

Extract the first word of a string in a SQL Server query

SELECT CASE CHARINDEX(‘ ‘, @Foo, 1) WHEN 0 THEN @Foo — empty or single word ELSE SUBSTRING(@Foo, 1, CHARINDEX(‘ ‘, @Foo, 1) – 1) — multi-word END You could perhaps use this in a UDF: CREATE FUNCTION [dbo].[FirstWord] (@value varchar(max)) RETURNS varchar(max) AS BEGIN RETURN CASE CHARINDEX(‘ ‘, @value, 1) WHEN 0 THEN @value ELSE … Read more

How to pivot table with T-SQL?

this should give you the results you want. CREATE TABLE #temp ( id int, data varchar(50), section varchar(50) ) insert into #temp values(1, ‘1AAA’, ‘AAA’) insert into #temp values(1, ‘1BBB’, ‘BBB’) insert into #temp values(1, ‘1CCC’, ‘CCC’) insert into #temp values(2, ‘2AAA’, ‘AAA’) insert into #temp values(2, ‘2BBB’, ‘BBB’) insert into #temp values(2, ‘2CCC’, ‘CCC’) … Read more