sql-server

How do I make a copy of an SQL Server database and connect to it?

你离开我真会死。 提交于 2021-02-18 19:41:42
问题 I'm working on vb.NET 2013 and SQL server 2008R2. I have this situation to resolve: I have a database "DB1". I want to create a copy of this database on the same SQL server with another name "DB2", and after make this database ready to connect. How can I do this through code from Vb.NET? 回答1: Check out this tech net link for restore with new file names. http://technet.microsoft.com/en-us/library/ms190447(v=sql.105).aspx. Take a backup of database one (DB1). Verify backup is valid. Get correct

SQL Server Combine multiple rows to one row with multiple columns

夙愿已清 提交于 2021-02-18 19:35:33
问题 I have a database in which I have the following rows: ID | Date start | Date end ---------------------------------- a | 01-01-1950 | 30-01-1951 a | 01-01-1948 | 31-12-1949 a | 31-01-1951 | 01-06-2000 b | 01-01-1980 | 01-08-2010 c | 01-01-1990 | 31-12-2017 c | 31-01-1985 | 31-12-1989 What I got Multiple rows per person One start and end date per row In a not chronological order Select query which I want to return the following: ID | Date start 1 | Date end 1 | Date start 2 | Date end 2 | Date

GROUP BY for specific rows

ε祈祈猫儿з 提交于 2021-02-18 18:58:41
问题 I have a sql server result which groups by person. And SUMS's their daily figure and monthly figure. Person Team Daily Figure Month To Date Tony Team 1 53 635 Dan Team 2 47 172 Tom Team 3 17 232 Simon Team 2 16 655 Ben Team 3 17 232 Fred Team 2 16 655 How do i group these results just by team 2? For example i still want the others individual results to show but i want just team 2 to be grouped together. Example result Person Team Daily Figure Month To Date Tony Team 1 53 635 Tom Team 3 17 232

Replace all non numeric characters in MSSQL with an empty string

拈花ヽ惹草 提交于 2021-02-18 18:04:16
问题 My current MSSQL table has a "phone" column which is a varchar. Unfortunately, the phone numbers that are already in the database are not in standard format. For example, 888-888-8888 OR 888/888/8888 OR (888)8888888 OR 8888888888. I want to get all the rows that are equivalent to 88888888, i.e it should match with 888-888-8888, (888)888888 etc. I have tried using REPLACE() but there are certain rows where entries have other alphabetic characters like "e", "ex", "ext", etc. So I would like to

How to pass byte[] from C# as string to SQL Server stored procedure and convert into varbinary(MAX)

笑着哭i 提交于 2021-02-18 17:55:47
问题 In this project, there is a class which wraps ADO.NET for common data access like ExecuteDataReader , ExecuteScalar , etc.. When using these methods to call a stored procedure, it allows you to pass a Dictionary<string, string> of parameters (string key, string value), which are then added to the SqlCommand object as SqlParameter . There is a case where we have to save a document in the database. The document is a byte[] and the corresponding column in the database is varbinary(MAX) . We've

How to make repeating custom expressions in SQL queries maintainable

天大地大妈咪最大 提交于 2021-02-18 17:50:06
问题 Imagine the following simple query, where I get a list of all users that were created within the last 3 days, the logic or example is not important SELECT ... , DATEDIFF(DAY, U.DateCreated, GETUTCDATE()) ... FROM dbo.AspNetUsers U WHERE DATEDIFF(DAY, U.DateCreated, GETUTCDATE()) < 3 I've repeated some code DATEDIFF(DAY, U.DateCreated, GETUTCDATE()) < 3 which, when I have much more complicated examples of the above I don't want to maintain twice or however many times I need that logic. How

Not able to build Visual Studio Database Project targeting Azure SQL Database when using AT TIME ZONE

房东的猫 提交于 2021-02-18 17:09:31
问题 We have a Visual Studio Database Project that we are trying to move to Azure SQL Database. Some of our views use AT TIME ZONE which is supported in Azure SQL Database, but when I switch the database Target Platform to Microsoft Azure SQL Database in the project properties, it fails to build with error: Error: SQL46010: Incorrect syntax near TIME. I have connected directly to the database in Azure and I can use AT TIME ZONE , but I cannot create or publish a DACPAC file from my project. This

Determine on time between 1 to 0 transitions

依然范特西╮ 提交于 2021-02-18 12:12:14
问题 My table indicates a pump ON/OFF status as follows Value timestamp 1 2013-09-01 00:05:41.987 0 2013-09-01 00:05:48.987 1 2013-09-01 00:05:59.987 0 2013-09-01 00:06:15.987 1 2013-09-01 00:06:34.987 etc etc. I need a MSSQL query that can take a months worth of these and tell me the number of minutes ON (1) and number in minutes OFF (0) i.e. duty cycle 回答1: Using CTE and RowNumber() function Fiddle demo : declare @date date = '20130925' ;with cte as ( select value, timestamp, row_number() over

Using SqlDependency with named Queues

别说谁变了你拦得住时间么 提交于 2021-02-18 10:41:13
问题 I have the following code that uses SqlDependency to monitor changes in one of my databases It works great, except every run it generates its own Queue/Service/Route with a guid in the database: Class: class SqlWatcher { private string connectionString; private string sqlQueue; private string listenerQuery; private SqlDependency dependency; public SqlWatcher(string connectionString, string sqlQueue, string listenerQuery) { this.connectionString = connectionString; this.sqlQueue = sqlQueue;

SQL Queries, execution plans and “Parallelism”

梦想的初衷 提交于 2021-02-18 10:41:10
问题 So I'm (still) going through some slow legacy sql views used to do calculate some averages and standarddeviations on a (sometimes) large set of data. What I end up with are views joining views joining views etc. So I though I would review the execution plan for my query. And it immediately suggested a missing index, which I then implemented. But it's still unbearably slow (so slow it times out the VB6 app querying it for data ;) ) So upon studying the execution plan further, I see that what