Category Archives: Database

SQL – Select into XML structure

from imp_dataSource d
join def_mapping m on m.sup_propertyCode = d.sup_propertyCode
where m.sup_familyCode = d.sup_familyCode or m.sup_familyCode ='GENERIC'
and m.map_type in ('SIMPLE', 'COMPLEX')
and d.sup_propertyValue != '' and d.sup_propertyValue is not null
order by d.sup_familyCode, d.sup_productCode, d.sup_propertyCode
for xml auto, type, elements, root('Products')

MS SQL – Removing non-printable characters

SQL select on XML type column

SELECT column
FROM table
where column.value(‘(/xpath/expression/parameter[@name=”supplierID”])[1]’, ‘nvarchar(100)’) = ‘matchValue’

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 (

Update Table A column from Table B

update TableA set col = TableB.col
from TableA, TableB
where TableA.matchCol = TableB.matchCol

Build SQL from Excel sheet

Use this Excel function to build insert statements from Excel data:

=”insert into [table name] select max(id)+1 , ‘”&A2&”‘ from [table name]”

SQL Order tables by creation date

order by 2 desc