Is there an ANSI SQL compliant version of SQL SERVER’s SELECT TOP n?
Tag: sql-server
Dynamic Pivot Columns in SQL Server
I have a table named Property with following columns in SQL Server: there are some property in this table that certain object in other table should give value to it. I want to make a pivot table like below that has one column for each property I’ve declared in 1’st table: I want to know how can I …
T-SQL get number of working days between 2 dates
I want to calculate the number of working days between 2 given dates. For example if I want to calculate the working days between 2013-01-10 and 2013-01-15, the result must be 3 working days (I don’t take into consideration the last day in that interval and I subtract the Saturdays and Sundays). I have …
sp_change_users_login ‘auto-fix’, ”
How can I execute the sp_change_users_login so that it will auto-fix all local sql accounts? in other words I can run the command to view all local accounts: I now want those list of users to be used in the sp_change_users_login procedure. Answer You could use a cursor to get the list of names, and then loop …
Execute SQL Asynchronously or change locking from Trigger
I have a complex unit of work from an application that might commit changes to 10-15 tables as a single transaction. The unit of work executes under snapshot isolation. Some of the tables have a trigger which executes a stored procedure to log messages into a queue. The message contains the Table Name, Key an…
Division with Aggregate Functions in SQL Not Behaving as Expected
I’m trying to do some crosstabs in SQL Server 2008 R2. That part is alright, however, if I try to get percentages for each cell, I run into a problem. Here is a distilled use case: A survey where people give their favorite color and their favorite fruit. I’d like to know how many like a given frui…
SQL Server datatypes nvarchar and varchar are incompatible error
I’ve inherited a C# app which I’ve converted to vb. I get one error which as far as I can see has nothing to do with the conversion. I have a SQL statement which is…. SELECT ResolverID AS …
SQL Select Value From Comma Delimited List
I have a field in my database called “Notes”. It contains a comma delimited list of values. I want to perform a SELECT Query that only returns the first value in the “Notes” field. This query would …
Optimal way to concatenate/aggregate strings
I’m finding a way to aggregate strings from different rows into a single row. I’m looking to do this in many different places, so having a function to facilitate this would be nice. I’ve tried solutions using COALESCE and FOR XML, but they just don’t cut it for me. String aggregation w…
SQL Copy row in a table with relations
Hope some one can help me, I would like to copy rows in same table and that table has relation to another table that I have to copy related row accordingly: Table1 I copied rows with table0Id = 3 Table1 I would like to do the same to Table2 depending to the Table1 Ids like this: Table2 Table2 As you