sql-server-2008-r2

Compute MD5 hash of a UTF8 string

筅森魡賤 提交于 2019-12-17 06:51:47
问题 I have an SQL table in which I store large string values that must be unique. In order to ensure the uniqueness, I have a unique index on a column in which I store a string representation of the MD5 hash of the large string. The C# app that saves these records uses the following method to do the hashing: public static string CreateMd5HashString(byte[] input) { var hashBytes = MD5.Create().ComputeHash(input); return string.Join("", hashBytes.Select(b => b.ToString("X"))); } In order to call

Should every User Table have a Clustered Index?

倖福魔咒の 提交于 2019-12-17 06:36:24
问题 Recently I found a couple of tables in a Database with no Clustered Indexes defined. But there are non-clustered indexes defined, so they are on HEAP. On analysis I found that select statements were using filter on the columns defined in non-clustered indexes. Not having a clustered index on these tables affect performance? 回答1: It's hard to state this more succinctly than SQL Server MVP Brad McGehee: As a rule of thumb, every table should have a clustered index. Generally, but not always,

WHERE Clause vs ON when using JOIN

ぐ巨炮叔叔 提交于 2019-12-17 02:49:08
问题 Assuming that I have the following T-SQL code: SELECT * FROM Foo f INNER JOIN Bar b ON b.BarId = f.BarId; WHERE b.IsApproved = 1; The following one also returns the same set of rows: SELECT * FROM Foo f INNER JOIN Bar b ON (b.IsApproved = 1) AND (b.BarId = f.BarId); This might not be the best case sample here but is there any performance difference between these two? 回答1: No, the query optimizer is smart enough to choose the same execution plan for both examples. You can use SHOWPLAN to check

Calculating distance between two points (Latitude, Longitude)

爷,独闯天下 提交于 2019-12-17 02:06:28
问题 I am trying to calculate the distance between two positions on a map. I have stored in my data: Longitude, Latitude, X POS, Y POS. I have been previously using the below snippet. DECLARE @orig_lat DECIMAL DECLARE @orig_lng DECIMAL SET @orig_lat=53.381538 set @orig_lng=-1.463526 SELECT *, 3956 * 2 * ASIN( SQRT( POWER(SIN((@orig_lat - abs(dest.Latitude)) * pi()/180 / 2), 2) + COS(@orig_lng * pi()/180 ) * COS(abs(dest.Latitude) * pi()/180) * POWER(SIN((@orig_lng - dest.Longitude) * pi()/180 / 2)

Calculating distance between two points (Latitude, Longitude)

泄露秘密 提交于 2019-12-17 02:06:16
问题 I am trying to calculate the distance between two positions on a map. I have stored in my data: Longitude, Latitude, X POS, Y POS. I have been previously using the below snippet. DECLARE @orig_lat DECIMAL DECLARE @orig_lng DECIMAL SET @orig_lat=53.381538 set @orig_lng=-1.463526 SELECT *, 3956 * 2 * ASIN( SQRT( POWER(SIN((@orig_lat - abs(dest.Latitude)) * pi()/180 / 2), 2) + COS(@orig_lng * pi()/180 ) * COS(abs(dest.Latitude) * pi()/180) * POWER(SIN((@orig_lng - dest.Longitude) * pi()/180 / 2)

Select query to remove non-numeric characters

淺唱寂寞╮ 提交于 2019-12-16 22:51:12
问题 I've got dirty data in a column with variable alpha length. I just want to strip out anything that is not 0-9. I do not want to run a function or proc. I have a script that is similar that just grabs the numeric value after text, it looks like this: Update TableName set ColumntoUpdate=cast(replace(Columnofdirtydata,'Alpha #','') as int) where Columnofdirtydata like 'Alpha #%' And ColumntoUpdate is Null I thought it would work pretty good until I found that some of the data fields I thought

The data types varchar and int are incompatible in the concat operator

梦想的初衷 提交于 2019-12-16 18:03:36
问题 I've been stuck for the past two days execute it give the error: The data types varchar and int are incompatible in the concat operator Here is my table: create table salestable ( id int identity(1,1) not null primary key, empid char(5), datesold date, monthonly varchar(50), amount money ) This code inserts the dummy record into dbo.salestable for working in salestable, debug and step into the code that give the debug and understanding code declare @outercounter int = 1 declare @innercounter

Dynamic query to Union multiple databases

耗尽温柔 提交于 2019-12-14 04:18:31
问题 Say I have the following databases in SQL Server 2008 R2 db1, db2, db3, db4, db5......dbn each database has a table A which contains the columns C1,C2,C3 I can write the following Select statement on two databases to get the data across them: Select C1,C2,C3 FROM db1.dbo.A UNION ALL Select C1,C2,C3 FROM db2.dbo.A However if I have 50 databases on the same server I don't want to write a UNION ALL for each. Can someone give me a script to do this? I can modify the script to exclude system

SQL Server 2008 R2: Dynamic query for pivot table with where and having clause

笑着哭i 提交于 2019-12-14 03:53:16
问题 Note : This post is slightly change with previous post. I have the following table with the details as shown below in the example. Example : Table: test create table test ( cola varchar(10), colb varchar(10), colc varchar(10) ); Insertion : insert into test values('101','1234','A1'); insert into test values('101','4321','A2'); insert into test values('201','5678','A3'); insert into test values('301','8765','A4'); insert into test values('401','9877','A1'); insert into test values('101','9997'

SQL query working fine in SQL Server 2012, but failed to execute in SQL Server 2008 R2

点点圈 提交于 2019-12-14 03:52:45
问题 I have a table called MyTextstable (myTextsTable_id INT, myTextsTable_text VARCHAR(MAX)) . This table has around 4 million records and I am trying to remove any instance of the ASCII characters in the following range(s) the VARCHAR(MAX) column myTextsTable_text . 00 - 08 11 - 12 14 - 31 127 I have written the following SQL query, which is taking under 10 minutes on SQL Server 2012, but failed to execute on SQL Server 2008 R2 even after two hours (so I stopped the execution). Please note I