sql-server-2008-r2

How can I generate a hierarchy path in SQL that leads to a given node?

痴心易碎 提交于 2019-12-17 17:02:52
问题 In my MS SQL 2008 R2 database I have this table: TABLE [Hierarchy] [ParentCategoryId] [uniqueidentifier] NULL, [ChildCategoryId] [uniqueidentifier] NOT NULL I need to write a query that will generate all paths that lead to a given Node. Lets say it I have the following tree: A -B --C -D --C Which would be stored as: NULL | A A | B A | D B | C D | C When asking for the Paths for C, I would like to get back two paths (written more or less like this): A > B > C, A > D > C 回答1: Here is my

How do I sort string alphabetically

这一生的挚爱 提交于 2019-12-17 16:59:19
问题 I am new to SQL Server as well as to Stack overflow. Please excuse for my mistakes. Is it possible to sort a value in a column aphabetically? Here is my table NAME ...... XZYVW EBACD My result should look like the below NAME ...... VWXYZ ABCDE Any idea to get this? 回答1: Create a User Defined Scalar Function. CREATE FUNCTION dbo.Alphaorder (@str VARCHAR(50)) returns VARCHAR(50) BEGIN DECLARE @len INT, @cnt INT =1, @str1 VARCHAR(50)='', @output VARCHAR(50)='' SELECT @len = Len(@str) WHILE @cnt

Combine consecutive date ranges

青春壹個敷衍的年華 提交于 2019-12-17 16:37:13
问题 Using SQL Server 2008 R2, I'm trying to combine date ranges into the maximum date range given that one end date is next to the following start date. The data is about different employments. Some employees may have ended their employment and have rejoined at a later time. Those should count as two different employments (example ID 5). Some people have different types of employment, running after each other (enddate and startdate neck-to-neck), in this case it should be considered as one

How to remove white space characters from a string in SQL Server

感情迁移 提交于 2019-12-17 15:28:31
问题 I'm trying to remove white spaces from a string in SQL but LTRIM and RTRIM functions don't seem to work? Column: [ProductAlternateKey] [nvarchar](25) COLLATE Latin1_General_CS_AS NULL Query: select REPLACE(ProductAlternateKey, ' ', '@'), LEN(ProductAlternateKey), LTRIM(RTRIM(ProductAlternateKey)) AS LRTrim, LEN(LTRIM(RTRIM(ProductAlternateKey))) AS LRLen, ASCII(RIGHT(ProductAlternateKey, 1)) AS ASCIIR, ASCII(LEFT(ProductAlternateKey, 1)) AS ASCIIL, ProductAlternateKey from DimProducts where

SSIS LookUp is not dealing with NULLs like the docs say it should

别说谁变了你拦得住时间么 提交于 2019-12-17 14:49:02
问题 I have an SSIS data flow that uses a lookup. Sometimes the value to be looked up (in my stream, not in the lookup table) is null. The MSDN Docs say: consider using full caching, which supports lookup operations on null values. I am using Full Caching (that is the default). But when I run I get this error on my null rows: Row yielded no match during lookup If I change the result to ignore no-matches then it works fine. But that ignores all no-matches. I just want to allow nulls through (as

Connecting to MS SQL Server with Windows Authentication using Python?

两盒软妹~` 提交于 2019-12-17 10:23:31
问题 How do I connect MS SQL Server using Windows Authentication, with the pyodbc library? I can connect via MS Access and SQL Server Management Studio, but cannot get a working connection ODBC string for Python. Here's what I've tried (also without 'Trusted_Connection=yes' ): pyodbc.connect('Trusted_Connection=yes', driver='{SQL Server}', server='[system_name]', database='[databasename]') pyodbc.connect('Trusted_Connection=yes', uid='me', driver='{SQL Server}', server='localhost', database='

Using IF ELSE statement based on Count to execute different Insert statements

只愿长相守 提交于 2019-12-17 09:41:09
问题 While I am searching through my database, I run an INSERT statement if I find that a particular item does not exist, and I run a different INSERT statement if I find one or more of this item. I am not entirely sure how to use the IF ELSE expressions. What I have so far is a statement that will count the number of times the target data appears; it will print TRUE if it is greater than 0, if not, it will print FALSE. I can't find any examples to help me understand how I can use this to run two

Cannot use UPDATE with OUTPUT clause when a trigger is on the table

你离开我真会死。 提交于 2019-12-17 08:54:36
问题 I'm performing an UPDATE with OUTPUT query: UPDATE BatchReports SET IsProcessed = 1 OUTPUT inserted.BatchFileXml, inserted.ResponseFileXml, deleted.ProcessedDate WHERE BatchReports.BatchReportGUID = @someGuid This statement is well and fine; until a trigger is defined on the table. Then my UPDATE statement will get the error 334: The target table 'BatchReports' of the DML statement cannot have any enabled triggers if the statement contains an OUTPUT clause without INTO clause Now this problem

How to get last 7 days data from current datetime to last 7 days in sql server

妖精的绣舞 提交于 2019-12-17 07:30:12
问题 Hi I am loading table A data from sql server to mysql using pentaho when loading data i need to get only last 7 days data from sql server A table to mysql In sql server createddate column data type is like datetime AND In mysql created_on column datatype is timestamp Here I used below query but i am getting only 5 days data Please help me in this issue select id, NewsHeadline as news_headline, NewsText as news_text, state, CreatedDate as created_on from News WHERE CreatedDate BETWEEN GETDATE(

Create a view with ORDER BY clause

半世苍凉 提交于 2019-12-17 07:23:08
问题 I'm trying to create a view with an ORDER BY clause. I have create it successfully on SQL Server 2012 SP1, but when I try to re-create it on SQL Server 2008 R2, I get this error: Msg 102, Level 15, State 1, Procedure TopUsers, Line 11 Incorrect syntax near 'OFFSET'. The code to create the view is CREATE View [dbo].[TopUsersTest] as select u.[DisplayName] , sum(a.AnswerMark) as Marks From Users_Questions us inner join [dbo].[Users] u on u.[UserID] = us.[UserID] inner join [dbo].[Answers] a on