sql-server-2012

Differences in distance calculated between javascript function and sql server STD distance

感情迁移 提交于 2019-12-11 18:58:44
问题 DECLARE @orig geography = geography::Point(17, 78, 4326); Select @orig.STDistance(geography::Point(17.001, 78.00001, 4326))/1000 gives 110.674385214845 function calculateDistance(lat1, lon1, lat2, lon2, units) { var R = 6371; // Radius of the earth in km var dLat = deg2rad(lat2 - lat1); // deg2rad below var dLon = deg2rad(lon2 - lon1); var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2); var c = 2 * Math

SQL Server trigger delete or cascade delete

时光毁灭记忆、已成空白 提交于 2019-12-11 18:24:23
问题 First off I haven't done much SQL code before only the basic CRUD, but I'm involved in a project in which I have access to SQL Server and its up to me to write the SQL. I've been busy looking on stackoverflow for a solution but (being new) it does not make any sense to me. I'm using SQL Server 2012. I have the following relationship (with foreign key constraints in place) Client > Order > OrderItems Order Id ClientId OrderItems Id OrderId I'm using EF and when I call my delete method on

Composite primary key sql relationship

南笙酒味 提交于 2019-12-11 18:01:41
问题 I need to create a relationship between ReportID which is part of a composite primary key. This is my code written for Microsoft SQL Server: CREATE TABLE Tableone ( ReportID varchar UNIQUE, Date date, Line ommited PRIMARY KEY (ReportNumber, Date) ) CREATE TABLE Tabletwo ( Line omitted Line ommited ReportID varchar UNIQUE FOREIGN KEY REFERENCES Tableone(ReportID), Line ommited PRIMARY KEY (XX, XXX, ReportID) ) UPDATE: We have specifically been asked to set both ReportID and Date as the primary

SQL subquery based on row values with unrelated table

那年仲夏 提交于 2019-12-11 18:00:01
问题 I need to get a count of records in an unrelated table, based on the row values in a query with some moderately complex joins. All data is on one server in a single SQL 2012 database, on several different tables. I am recreating ticket movement history for a single ticket at a time, from audit records and need to calculate business days for the spans in rows created by the joins. Tickets are moved around between areas (ASSIGNMENT), and there are guidelines on how long it should be at any one

Selecting records in SQL Server

廉价感情. 提交于 2019-12-11 17:52:29
问题 I have table with two columns with this sample data: Column1 Column2 ------------------------ A B A C A D R B R D S E If I pass the input value of Column2='B', it will display these records: Column1 Column2 ------------------------- A B A C A D R B R D because Column2 'D' Contains 'A' in column1 and 'R' in Column1. So it fetches all the records which contains 'A' and 'R' Suppose if I pass an input if Column2='C', it will display these records Column1 Column2 ------------------------- A B A C

How to loop over the result (system.Data.DataSet object) of SQL run from PowerShell

瘦欲@ 提交于 2019-12-11 17:37:04
问题 I am trying to run a SQL from Power Shell(which is on my windows 7 64 bit desktop) and the remote database host is MS SQL Server 2012. I am running SQL1 by calling function Get-ODBC-Data which will give me single column of type string. It can have unknown number of rows (up to 20). Then I am using each of these column values as parameter ($var1) to the second function Get-ODBC-Data-Count. The SQL2 in this function Get-ODBC-Data-Count will give me count using $var1 in where clause. The code is

How to account for double entries in a card swipe table?

六眼飞鱼酱① 提交于 2019-12-11 17:16:17
问题 How do I account for accidental swipes in a card swipe table? I tried selecting the max(time_cst) by grouping them on entry. It did not solve the problem. http://www.sqlfiddle.com/#!18/06cc8/2 EmpID Enter/Exit Time 2999 Entry 06:00AM 2999 Entry 06:01AM 2999 Exit 12:00PM 2999 Entry 01:00PM 2999 Exit 03:00PM 2999 Entry 04:00PM 2999 Exit 06:00PM This is how I want it to show up. http://www.sqlfiddle.com/#!18/5cfbb/2 EmpID EntryTime ExitTime 2999 06:01AM 12:00PM 2999 01:00PM 03:00PM 2999 04:00PM

query to select count of records for each year

你离开我真会死。 提交于 2019-12-11 17:05:19
问题 sorry for this newbie question ... im trying to get number of rows per year of my table..... this is my try: select count(*) from tbl_fact where stat = 1 and id = 16 group by (year(fact_date)) go and select count(*) from tbl_fact where stat = 1 and id = 16 and year(fact_date) in (select Distinct(year(fact_date)) from tbl_fact) group by (year(fact_date)) go i have records that taged with dates that for now i have dates from 2017 and 2018 so i need counts for each year. but the id=16 has only

TRANSLATE function in SQL SERVER

我的未来我决定 提交于 2019-12-11 16:46:43
问题 I read that there is a function equivalent to the standard function TRANSLATE under DB2 under SQL Server 2017. But how to do under earlier versions? For definition of function : here 回答1: EDITED: I'm feeling dumb - MatBailie correctly pointed out that my original solution was incorrect. I actually always thought that TRANSLATE('abc', 'abc', 'bcd') was supposed to return ddd but, after testing SQL Server 2017's TRANSLATE I see that 'bcd' would be the correct answer. You can see my original

Rounding of a value in a varchar column in MS SQL Server 2012?

风流意气都作罢 提交于 2019-12-11 15:33:48
问题 I have a strange requirement to round a value in a varchar column in SQL Server 2012. I know it is bad practice to hold numeric values in a varchar . i.e. the column is a varchar but holds a string representing a number. I have string values like 834.78330000000005 and 831.06669999999997 and 797.45000000000005 but I want to update these to string values like 834.7833 and 831.0667 and 797.45 (trimming the trailing zeros not too important but desirable). This seems to be close, are there better