Tag Archives: query

SQL Case-sensitive select statement

If Column1 of Table1 has following values ‘CaseSearch, casesearch, CASESEARCH, CaSeSeArCh’, following statement will return you all the four records.

SELECT Column1
FROM Table1
WHERE Column1 = 'casesearch'

To make the query case sensitive and retrieve only one record (“casesearch”) from above query, the collation of the query needs to be changed as follows.

SELECT Column1
FROM Table1
WHERE Column1 COLLATE Latin1_General_CS_AS = 'casesearch'

Adding COLLATE Latin1_General_CS_AS makes the search case sensitive.

Default Collation of the SQL Server installation SQL_Latin1_General_CP1_CI_AS is not case sensitive.

To change the collation of the any column for any table permanently run following query.

COLLATE Latin1_General_CS_AS

To know the collation of the column for any table run following Stored Procedure.

EXEC sp_help DatabaseName

Second results set above script will return you collation of database DatabaseName.

Reference : Pinal Dave (http://blog.SQLAuthority.com)


Find a column by name (SQL Server 2008)

How to find column within a database with searching by name:

SELECT * FROM Information_Schema.Columns c WHERE  c.COLUMN_NAME like 'manu%'

Returning Records With Children

Here I have a screen for searching Street addresses.  Tables are Continent, Country, City and Street.

The database contains many Countries and Cities which do not have addresses. To make the user experience better I only want to load the autocomplete boxes with Countries and Cities that have addresses. Continue reading