sql-server-2012

T-SQL UPDATE statement affects more records than select statement

本小妞迷上赌 提交于 2019-12-11 03:14:58
问题 The following is driving me nuts, please help me! As a check before running an UPDATE statement (T-SQL; SQL Server 2012) I run: select * from Treatment_Day42 where td42pinit = 'J M' and td42pid = 'ADA'; and I get 1 row returned, as I expect. However, running update Treatment_Day42 set td42pid = 'ADA252' where td42pinit = 'J M' and td42pid = 'ADA'; affects four records, not one as in the select statement with the exact same WHERE clause. I get the row returned by the select plus 3 other rows

Convert day of year to datetime in SQL Server

蓝咒 提交于 2019-12-11 03:12:53
问题 In a database table (SQL Server 2012), I have dates saved in the yyyyxxx format, where xxx is the day of the year. For example 2015-08-11 is 2015223 . How can I efficiently convert this format to DATETIME ? How about converting DATETIME to this format? 回答1: Get the Year part, convert to datetime + add days DECLARE @var NVARCHAR(100) = '2015223'; SELECT CAST(LEFT(@var, 4) AS DATETIME) + CAST(RIGHT(@var, 3) AS INT) - 1; Or: DECLARE @var NVARCHAR(100) = '2015223'; SELECT DATEADD(dd,CAST(RIGHT(

SQl server duplicate joins issue

旧巷老猫 提交于 2019-12-11 03:08:38
问题 can anyone please help: I tried to join with duplicate values but it is not coming as I wanted. CREATE TABLE #TestTable1 ([No] varchar(50),[Value1] float,[Desc] varchar(50)) insert into #TestTable1 ([No],[Value1],[Desc]) Values (N'123953',427.2,N'Basic Hours') ,(N'123953',106.8,N'Basic Hours') ,(N'123953',213.6,N'Basic Hours') ,(N'123953',213.6,N'Basic Hours') ,(N'123953',213.6,N'Basic Hours') ,(N'123953',213.6,N'Basic Hours') ,(N'123953',105.6,N'Basic Hours') CREATE TABLE #TestTable2 ([No]

How to return a table variable from a function (UDF)?

℡╲_俬逩灬. 提交于 2019-12-11 03:06:53
问题 I'm using SQL Server 2012 and I have been trying lots of different approaches to return a table variable from inside a function, but I cannot get it to work. I've tried moving the variable declaration to different places, etc. Here are the guts of the sql. If you could please wrap the guts in a UDF function that actually compiles and returns the @Financials table variable I would appreciate it. The sql works great, no problems. But when I try to wrap it in a UDF it throws errors when I try to

FOR XML PATH in SQL server and [text()]

孤人 提交于 2019-12-11 03:06:02
问题 I found some article on internet this url Then I code query like that and I get same result But when I change AS [text()] to [name] the result contain XML tag like this So My question is What is [text()] in this code Thank you. 回答1: The other current answers don't explain much about where this is coming from, or just offer links to poorly formatted sites and don't really answer the question. In many answers around the web for grouping strings there are the copy paste answers without a lot of

What is the default password for SQL Server 2012 if I didn't put a password in the setup?

北战南征 提交于 2019-12-11 02:59:28
问题 I installed SQL Server 2012 without putting a password . What is the default password for SQL Authentication , where the login is sa ? Login : sa and Password : blank , didn't do the trick ... Thanks 回答1: If you install SQL Server with Windows Authentication mode and want to change it, you need to do 2 different things: Go to SQL Server Properties/Security tab and change the mode to SQL Server authentication mode Go to security/logins, open SA login properties a. Uncheck "Enforce password

Use default value of a column in stored procedures

天涯浪子 提交于 2019-12-11 02:56:59
问题 I am using SQL Server 2012 and I have 2 tables with the following definition CREATE TABLE t1 (id INT PRIMARY KEY, value NVARCHAR(10)) CREATE TABLE t2 (id INT PRIMARY KEY, value BIT DEFAULT 1) ALTER TABLE t2 WITH CHECK ADD CONSTRAINT FK FOREIGN KEY(id) REFERENCES t1 (id) I inserted the following columns for the current example: INSERT INTO t1 VALUES (1, 'a') INSERT INTO t1 VALUES (2, 'b') INSERT INTO t1 VALUES (3, 'c') INSERT INTO t2 VALUES (1, 1) INSERT INTO t2 VALUES (3, 0) I am running this

SQL Server: difference in days for two dates in separate rows

不想你离开。 提交于 2019-12-11 02:37:35
问题 I am using SQL Server 2012 and working an a report currently that is asking me to find the difference in days between two dates. Basically, for a particular ReportID , I'm trying to find the difference in days between the ( ReportCompletedDate when the ReportType = 'PaperReceived' ) - ( ReportCompletedDate when the ReportType = 'Form Completed' ) I tried to give some records below... ReportID ReportType ReportCompletedDate ------------------------------------------------- 450 PaperReceived 9

How to fix Microsoft SQL Server, Error: 262?

一曲冷凌霜 提交于 2019-12-11 01:58:36
问题 I'm opening the SQL Server Management Studio with run as administrator ... And there is no problem with Windows Authentication Connection. I'm choosing "New Database" by right-click the Databases folder. And giving name 'BTS' for the Database. When i'm choosing OK, i get the error as, How to fix it? 回答1: From this blog post To add a Windows user that has the login “machinename\Administrator” to the sysadmin fixed server role Log on to the computer using the credentials for the machinename

Is it possible to do an Insert Into stored procedure?

天大地大妈咪最大 提交于 2019-12-11 01:58:24
问题 Basically I have a table of data and I would like to take the values of each row and feed it to the stored procedure. In my perfect world, I'd do this insert into StoredProcA @var1 @var2 select testdesc, testoption from tableA where testoption = 1 Well, I don't think that's going to work. So how, if possible, can I take the all data from a table/query and pass it to the stored procedure? EDIT : The stored procedure already exists and does quite a bit of processing to the incoming data. The