How to query for Xml values and attributes from table in SQL Server?

Actually you’re close to your goal, you just need to use nodes() method to split your rows and then get values: select s.SqmId, m.c.value(‘@id’, ‘varchar(max)’) as id, m.c.value(‘@type’, ‘varchar(max)’) as type, m.c.value(‘@unit’, ‘varchar(max)’) as unit, m.c.value(‘@sum’, ‘varchar(max)’) as [sum], m.c.value(‘@count’, ‘varchar(max)’) as [count], m.c.value(‘@minValue’, ‘varchar(max)’) as minValue, m.c.value(‘@maxValue’, ‘varchar(max)’) as maxValue, m.c.value(‘.’, ‘nvarchar(max)’) as Value, … Read more

Calculating distance between two points (Latitude, Longitude)

Since you’re using SQL Server 2008, you have the geography data type available, which is designed for exactly this kind of data: DECLARE @source geography = ‘POINT(0 51.5)’ DECLARE @target geography = ‘POINT(-3 56)’ SELECT @source.STDistance(@target) Gives ———————- 538404.100197555 (1 row(s) affected) Telling us it is about 538 km from (near) London to (near) Edinburgh. … Read more

Could not load file or assembly Microsoft.SqlServer.management.sdk.sfc version 11.0.0.0

Problem: (Sql server 2014) This issue happens when assembly Microsoft.SqlServer.management.sdk.sfc version 12.0.0.0 not found by visual studio. Solution: just go to http://www.microsoft.com/en-us/download/details.aspx?id=42295 and download: ENU\x64\SharedManagementObjects.msi for X64 OS or ENU\x86\SharedManagementObjects.msi for X86 OS, then install it, and restart visual studio. PS: You may need install DB2OLEDBV5_x64.msi or DB2OLEDBV5_x86.msi too. Problem: (Sql server 2012) This issue … Read more

SQL Server – Create a copy of a database table and place it in the same database?

Use SELECT … INTO: SELECT * INTO ABC_1 FROM ABC; This will create a new table ABC_1 that has the same column structure as ABC and contains the same data. Constraints (e.g. keys, default values), however, are -not- copied. You can run this query multiple times with a different table name each time. If you … Read more