sql-server-2012

SQL Query to fill missing gaps across time and get last non-null value

做~自己de王妃 提交于 2020-01-03 03:02:48
问题 I have the following table in my database: Month|Year | Value 1 |2013 | 100 4 |2013 | 101 8 |2013 | 102 2 |2014 | 103 4 |2014 | 104 How can I fill in "missing" rows from the data, so that if I query from 2013-03 through 2014-03, I would get: Month|Year | Value 3 |2013 | 100 4 |2013 | 101 5 |2013 | 101 6 |2013 | 101 7 |2013 | 101 8 |2013 | 102 9 |2013 | 102 10 |2013 | 102 11 |2013 | 102 12 |2013 | 102 1 |2014 | 102 2 |2014 | 103 3 |2014 | 103 As you can see I want to repeat the previous Value

SQL Server CLR : how to call WCF Service In CLR SQL stored procedure in Visual Studio 2013 database project

爱⌒轻易说出口 提交于 2020-01-02 21:47:07
问题 I have Visual Studio 2013. I created a database project. I added a CLR stored procedure to that project, and I want to call a WCF service from it. Can anyone help me? I want be able to change service address without changing my assembly, similar to what I can do in web.config with the endpoint section of my WCF client config. 回答1: I found this after a lot of searching and spend very much time on this in VS 2014 Create Database Project Called "CLR_Test" Create Library For WCF Client "CLR

How to track changes in records in SQL Server?

*爱你&永不变心* 提交于 2020-01-02 15:12:26
问题 I have the following table which has tracking records of all students. |==========================================| | ID | Department | Date | |==========================================| | 001 | English | Feb 3 2017 | | 001 | English | Feb 4 2017 | | 001 | Science | Mar 1 2017 | | 001 | Science | Apr 2 2017 | | 001 | Maths | Apr 7 2017 | | 002 | Maths | Feb 1 2017 | | 002 | Maths | Apr 7 2017 | | 003 | Maths | Apr 3 2017 | | 004 | Science | Feb 1 2017 | | 004 | Maths | Apr 7 2017 | |========

How scale is defined when decimal and bigint are divided?

馋奶兔 提交于 2020-01-02 12:49:32
问题 I have value A of type DECIMAL(19,8) - the scale is 8 , so the number of decimal digits that will be stored to the right of the decimal point is 8 . Now, I am dividing A on B , where B is BIGINT . For, example: SELECT CAST(3 AS DECIMAL(19, 8)) / CAST(27 AS BIGINT) -- 0.111111111111111111111111111 ,CAST(300 AS DECIMAL(19, 8)) / CAST(27 AS BIGINT) -- 11.111111111111111111111111111 ,CAST(75003 AS DECIMAL(19, 8)) / CAST(13664400 AS BIGINT) -- 0.005488934750153684025643277 the output values are

How To check minimum Date in each observation and update flag dynamically

穿精又带淫゛_ 提交于 2020-01-02 10:26:53
问题 I have to check for minimum date each time and add 30 days to it and set the field accordingly. For Example: My Table: ID StartDate EndDate 1 2017-01-01 2017-02-01 1 2017-01-09 2017-01-28 1 2017-04-01 2017-04-30 1 2017-04-05 2017-05-20 1 2017-04-20 2017-06-12 2 2017-06-02 2017-06-20 2 2017-06-14 2017-07-31 2 2017-06-14 2017-07-31 2 2017-06-19 2017-07-31 2 2017-06-19 2017-07-31 so, here min(startdate) is 2017-01-01. Now I add 30 days to it and my look up date is 2017-01-31. for any record

How to update inserted field in trigger

做~自己de王妃 提交于 2020-01-02 08:22:30
问题 Ok, my situation is like this: I have a table named Company, and I want to add a trigger after every record I add in this table that checks if the column Name does not end with 'LTD' then add 'LTD' at the end of the Name. I get an error saying Incorrect syntax near ')'. How would I do this? Create Trigger [Add_LTD] on Company After Insert As Update Company Set Name = Name + ' LTD' If Exists (Select Name From Inserted Where Name Not Like '% LTD') 回答1: You would need something like this: CREATE

How to update inserted field in trigger

五迷三道 提交于 2020-01-02 08:21:10
问题 Ok, my situation is like this: I have a table named Company, and I want to add a trigger after every record I add in this table that checks if the column Name does not end with 'LTD' then add 'LTD' at the end of the Name. I get an error saying Incorrect syntax near ')'. How would I do this? Create Trigger [Add_LTD] on Company After Insert As Update Company Set Name = Name + ' LTD' If Exists (Select Name From Inserted Where Name Not Like '% LTD') 回答1: You would need something like this: CREATE

How to figure out what object is represented by associatedObjectId during blocking?

霸气de小男生 提交于 2020-01-02 05:29:08
问题 I got report from another team about blocking in SQL Server. Looking at results of Exec sp_who2 and query from blog by Glenn Berry SELECT blocking.session_id AS blocking_session_id ,blocked.session_id AS blocked_session_id ,waitstats.wait_type AS blocking_resource ,waitstats.wait_duration_ms ,waitstats.resource_description ,blocked_cache.text AS blocked_text ,blocking_cache.text AS blocking_text FROM sys.dm_exec_connections AS blocking INNER JOIN sys.dm_exec_requests blocked ON blocking

SQL: Count Users with Activity in the Past Week

五迷三道 提交于 2020-01-02 05:13:30
问题 I am trying to count the number of users who have had at least two sessions within 7 days of OR ten in 30 days of all dates. My data is as follow: Date UserID SessionID 1/1/2013 Bob1234 1 2/1/2013 Bob1234 2 2/2/2013 Bob1234 3 2/3/2013 Cal5678 4 Which would result in the following table (only select dates shown) Date CountActiveUsers 1/1/2013 1 1/15/2013 0 2/2/2013 1 2/3/2013 2 The real data set has values for all dates in a continuous data range and the results table should have an entry for

Converting a numeric value to date time

流过昼夜 提交于 2020-01-02 04:46:28
问题 I am working in SQL Server 2012. My date column in a data set looks like this: 41547 . The column is in nvarchar (255) . I want to convert it to something like this: yyyy-mm-dd hh:mm:ss (Example: 2013-09-14 12:23:23.98933090 ). But I can not do this. I am using following code: select convert(datetime, date_column, 6) But this is giving following error: Msg 241, Level 16, State 1, Line 1 Conversion failed when converting date and/or time from character string. What am I doing wrong? 回答1: Your