sql-server-2008-r2

How To Separate Data Into Two Columns [duplicate]

邮差的信 提交于 2019-12-04 17:08:58
This question already has an answer here: Attendance IN and OUT 3 answers Consider the following table structure and sample data - EmpID InputDateTime StatusINOUT ------------------------------------- 1 2018-05-26 08:44 1 1 2018-05-26 08:44 2 2 2018-05-28 08:44 1 2 2018-05-28 12:44 2 1 2018-05-21 08:44 1 1 2018-05-21 10:44 2 2 2018-05-23 08:44 1 2 2018-05-23 08:44 2 Now I want to separate column InputDateTime into two columns i.e., INTIME(1) and OUTTIME(2) . The logic behind this is the date for which StatusInOut is 1 will be InTime and for StatusInOut is 2 that date value will be OUTTIME(2) .

History of commands on SQL Server

本秂侑毒 提交于 2019-12-04 17:04:07
This query gives me the history of commands executed on SQL Server: Select * From ( SELECT deqs.last_execution_time AS [Time], dest.TEXT AS [Query] FROM sys.dm_exec_query_stats AS deqs CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest ) x When I added Where x.Query LIKE '%Insert%' I get bad results (I think that this is because of the cross join): Select * From ( SELECT deqs.last_execution_time AS [Time], dest.TEXT AS [Query] FROM sys.dm_exec_query_stats AS deqs CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) AS dest ) x Where x.Query LIKE '%Insert%' How do I get the cross join to

In SQL Server Management Studio 2014, the New Trigger menu option is disabled

安稳与你 提交于 2019-12-04 16:56:12
问题 I want to add a new trigger to my table. As seen in the picture, New Trigger button is not active. new index, new column, new contraints, new statics is active. I do not understand what is the problem. 回答1: You don't need to use the menu item to create a trigger. Just open up a query window and write the create trigger statement there. To get some help with the syntax you can use a snippet in the editor. Right click on the surface of the query editor and select Insert Snippet and then select

Creating Users on secondary server in log shipping

倾然丶 夕夏残阳落幕 提交于 2019-12-04 16:33:33
I have a production Server say ServerA I have setup log shipping to ServerB which is left in read-only mode. The purpose of this log shipping is to lower the load on production server for some expensive queries (painful reports). Now if I have to create some logins using our domain accounts. I cannot do this because the secondary database is in standby mode . I thought if I create these logins on Primary server it will be copied over to secondary server then the logs are restored there but this isnt the case. I have done a lot of research online finding a way around to this. I found the

INSERT IF NOT EXISTS but return the identity either way

…衆ロ難τιáo~ 提交于 2019-12-04 15:47:10
问题 I have 3 tables: audioFormats, videoFormats, and fileInfo. I have a transaction such that when I insert into the fileInfo table, that insert includes an FK from audioFormats and videoFormats. An insertion into the latter tables takes place if the audio format or video format are not already in those tables, then the generated (or existing) ID value is inserted into fileInfo. How do I efficiently insert a value only if that value does not exist, but get the ID of the value whether it already

Search XML with a LIKE or similar full search operation

て烟熏妆下的殇ゞ 提交于 2019-12-04 15:36:04
问题 I want to search an XML valued column to see if a contains a string. I don't know the schema, I want to know if the string is contained anywhere at all. I don't know if XPATH would work in this situation. The equivalent of Select s.Name, ts.ValueXML from table t (nolock) join table2 ts (nolock) on t.key_Id = ts.key_Id join table3 s (nolock) on ts.key_Id=s.key_Id where s.Name like '%Lab%' and ts.ValueXML like '%PreviewDateRange%' ERROR: Argument data type xml is invalid for argument 1 of like

how to find the duration between two dates

若如初见. 提交于 2019-12-04 15:07:30
I want to find the duration between the two date columns. For this i used the DATEDIFF function to find number years and months separately but wanted both results in single column. The two columns are given below. start_dt | end_dt 06-Oct-2009 15-Jul-2011 Result which needed Duration(years.months) 2.3 I think there is no out-of-the-box API to provide the result in the format you mentioned. You need to use the DATEDIFF function to get the difference in the least denomination you need and then divide the result with appropriate value to get the duration in the format required. Something like

with(nolock) or (nolock) - Is there a difference?

偶尔善良 提交于 2019-12-04 14:58:11
问题 Everything is based on the assumption that with(nolock) is entirely appropriate for the situtation. There are already plenty of questions out there debating whether or not to use with(nolock). I've looked around and haven't been able to find if there is an actual difference between using with(nolock) : select customer, zipcode from customers c with(nolock) or just (nolock) : select customer, zipcode from customers c (nolock) Is there a functional difference between the two? Stylistic? Is one

How can I retrieve second last row?

只谈情不闲聊 提交于 2019-12-04 14:23:03
I have a table with many records and I want to know only the record which I have created at second last. For ex: I have a table customer in which customerID are random numbers. Now I want to select second last row. customerID customer_name cont_no --------------------------------------- 7 david sam 5284 1 shinthol 1 11 lava 12548 2 thomas 1 3 peeter 1 4 magge 1 5 revas 1 6 leela 123975 Output row : customerID customer_name cont_no 5 revas 1 I don't want second highest... I want second last row. As you asked I can give you example. Imagine, that you have full bag of apples. How can you take

Issue with Window Function in MSSQL 2008 R2

半腔热情 提交于 2019-12-04 14:15:08
I am receving execution error in following sql script. Select TOP 1 Percent a.accode,a.voucherdate,a.credit,a.Debit, SUM(a.Debit) Over (Order By [a.accode],[a.voucherdate] ) as rdr from VoucherMain as a Order By a.accode,a.voucherdate Error Message Incorrect syntax near 'order'. Can anyone tell me what's wrong with the script syntext? The problem is that you need SQL Server 2012 and above. Okay, I added the "and above" for future visitors, but compare 2008 OVER CLAUSE with 2012 OVER CLAUSE . The 2008 version has this important note: When used in the context of a ranking window function, <ORDER