Friday 31 October 2014

Pad leading Zero's in SQL

This example would pad a 3 character string with a leading zero:

SELECT RIGHT('000'+ISNULL(field,''),3)

NULL values will show as 000

If you have an Integar Value that you want to add leading zero's then:

 
SELECT RIGHT('000'+CAST(field AS VARCHAR(3)),3)

Find Duplicate Fields in a table

select field1,field2,field3, count(*)
  from table_name
  group by field1,field2,field3
  having count(*) > 1