A way to check if foreign key exists in SQL 2005

You can use this script: IF EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_NAME]’) AND parent_object_id = OBJECT_ID(N'[dbo].[MyTable]’)) BEGIN — do stuff END This can be done if you expand out the table and right click on an existing FK and choose script key as “DROP TO” and then you will get a generated … Read more

Subtract minute from DateTime in SQL Server 2005

SELECT DATEADD(minute, -15, ‘2000-01-01 08:30:00′); The second value (-15 in this case) must be numeric (i.e. not a string like ’00:15′). If you need to subtract hours and minutes I would recommend splitting the string on the : to get the hours and minutes and subtracting using something like SELECT DATEADD(minute, -60 * @h – … Read more