sql-server-2008-r2

Get yesterday's rows startign from 10AM

落花浮王杯 提交于 2019-12-12 01:39:23
问题 For previous day I used use the below expression . DATE_INSERTED >=DATEADD(day, DATEDIFF(day,0,GETDATE())-1,0) AND DATE_INSERTED < DATEADD(day, DATEDIFF(day,0,GETDATE()),0) How to get rows from yesterday 10Am to today 10AM 回答1: -- yesterday at midnight: DECLARE @yesterday DATETIME = DATEADD(DAY,DATEDIFF(DAY,1,GETDATE()),0); SELECT ... WHERE DATE_INSERTED >= DATEADD(HOUR, 10, @yesterday) -- 10 AM yesterday AND DATE_INSERTED < DATEADD(HOUR, 34, @yesterday); -- 10 AM today 回答2: Instead of using

complex query join checking that value does not exist

青春壹個敷衍的年華 提交于 2019-12-12 01:25:37
问题 I am struggling with a great challenge of a query. I have two tables, first has Tb1 drID schedDate rteID Second has: Tb2 drID FName LName Active Tb1 drID must be checked for Null or blank and match on schedDate and drID can not have any values that match Tb2.drID for date selected, checking for Null and '' essentially do this. SELECT drID, schedDate, rteID FROM Tb1 WHERE (drID IS NULL OR drID = '') AND (schedDate = 11 / 1 / 2012) From all of this I need to return from TB2 drID, Fname, LName

Transposing a a PIVOT query

对着背影说爱祢 提交于 2019-12-12 01:23:12
问题 A query that uses PIVOT is it transposable? IN this question i understood how to use PIVOT but there are cases in which it would be better to have the columns as records. THis is achievable by transposing the query resultset. Is it possible? 来源: https://stackoverflow.com/questions/18660921/transposing-a-a-pivot-query

How to return a single transaction per customer

心不动则不痛 提交于 2019-12-12 01:12:46
问题 I have created a Crystal Report that uses a stored procedure on my SQL Server to return all cards that make a transaction within a given time-scale. I intend to only identify if a card has been used, and then return the last row (transaction) that the card has made. This should mean that I only return one row per card, regardless of how many transactions are made. However, when I create a report my output looks like this: My stored procedure looks like this: ALTER PROCEDURE [dbo].

RowNumber() and Partition By performance help wanted

徘徊边缘 提交于 2019-12-12 00:49:40
问题 I've got a table of stock market moving average values, and I'm trying to compare two values within a day, and then compare that value to the same calculation of the prior day. My sql as it stands is below... when I comment out the last select statement that defines the result set, and run the last cte shown as the result set, I get my data back in about 15 minutes. Long, but manageable since it'll run as an insert sproc overnight. When I run it as shown, I'm at 40 minutes before any results

Find Row Changes and Output to Table

荒凉一梦 提交于 2019-12-12 00:42:55
问题 I have an SQL Server table of the following structure: id TransDate PartType ====================================== 1 2016-06-29 10:23:00 A1 2 2016-06-29 10:30:00 A1 3 2016-06-29 10:32:00 A2 4 2016-06-29 10:33:00 A2 5 2016-06-29 10:35:00 A2 6 2016-06-29 10:39:00 A3 7 2016-06-29 10:41:00 A4 I need a SELECT statement that will output a table that finds the changes in PartType , and that looks like this (for SSRS purposes): PartType StartTime EndTime =============================================

Get substring between “\” where multiple “\”

浪尽此生 提交于 2019-12-12 00:11:46
问题 Found this solution to get substring after slash () character DECLARE @st1 varchar(10) SET @st1 = 'MYTEST\aftercompare' SELECT @st1 ,SUBSTRING(@st1, CHARINDEX('\', @st1) + 1, LEN(@st1)) http://social.msdn.microsoft.com/Forums/sqlserver/en-US/5c3a5e2c-54fc-43dd-b12c-1a1f6784d7d8/tsql-get-substring-after-slash-character But is there a way to get substring after second slash or even more? DECLARE @st1 varchar(50) --Added more slashes SET @st1 = 'MYTEST\aftercompare\slash2\slash3\slash4' SELECT

How to filter pivot results

*爱你&永不变心* 提交于 2019-12-11 23:30:03
问题 Is it possible to exclude some vaules from the PIVOT results. Referencing this question i would like to know if it is posible to exclude the columns in the Pivot table that has 0 value. Imagine there is a count of 0 for EventType Meeting, is it possible not to show it at all? 回答1: i hope you have implemented following solution from the question DECLARE @cols AS NVARCHAR(MAX), @query AS NVARCHAR(MAX) select @cols = STUFF((SELECT distinct ',' + QUOTENAME(EventType) from dbo.testTable FOR XML

Only allow one of two columns to be set

本小妞迷上赌 提交于 2019-12-11 22:34:18
问题 I'm looking for a constraint in an SQL table that will only allow one of two nullable columns to be set per row. Both columns are foreign keys that relate to different tables. As an example, say each table row describes a section of a journey. I have a separate table for Cars and Planes . Without redesigning my whole database to use a Vehicles table, how can I ensure that each journey row is done by either a CarID or a PlaneID , but never both at the same time, and never neither? 回答1: Add a

XML parsing, illegal character in the end of the string

自闭症网瘾萝莉.ら 提交于 2019-12-11 20:28:42
问题 I'm getting very strange error when I trying to convert a string to XML in MS SQL Server: Msg 9420, Level 16, State 1, Line 5 XML parsing: line 1, character 8071, illegal xml character If I check the string in some text editor, I can see that its length is 8070. Why is it complaining about character 8071 if it does not exist? This is how I'm converting string to XML: CAST(REPLACE(SUBSTRING( REPLACE(REPLACE(REPLACE(ResponseData,'ä','a'),'ö','o'),'å','a'), PATINDEX('%<?xml%',ResponseData),