I’ll use a concrete, but hypothetical, example. Each Order normally has only one line item: Orders: OrderGUID OrderNumber ========= ============ {FFB2…} STL-7442-1 {3EC6…} MPT-…
Tag: sql-server
How to return empty groups in SQL GROUP BY clause
I have a query to return how much is spent on-contract and off-contract at each location, that returns something like this: The problem is, I only get one row for locations that are all ad-hoc or all contracted expenses. I’d like to get two rows back for each location, like this: Is there any way I can accomplish this through
Saving changes after table edit in SQL Server Management Studio
If I want to save any changes in a table, previously saved in SQL Server Management Studio (no data in table present) I get an error message: Saving changes is not permitted. The changes you have made require the following tables to be dropped and re-created. You have either made changes to a table that can’t be re-created or enabled
Which is the most common ID type in SQL Server databases, and which is better?
Is it better to use a Guid (UniqueIdentifier) as your Primary/Surrogate Key column, or a serialized “identity” integer column; and why is it better? Under which circumstances would you choose one over …
Use a LIKE statement on SQL Server XML Datatype
If you have a varchar field you can easily do SELECT * FROM TABLE WHERE ColumnA LIKE ‘%Test%’ to see if that column contains a certain string. How do you do that for XML Type? I have the following …
sql while loop with date counter
I need to do something like this in sql: How can I do the above correctly in SQL? Basically, my startdate and enddate are strings and not datetimes because my business logic is referencing string column in another table with a date as the name of the column-But I need to loop through a bunch of columns, each column having
Reset or Update Row Position Integer in Database Table
I am working on a stored procedure in SQL Server 2008 for resetting an integer column in a database table. This integer column stores or persists the display order of the item rows. Users are able to drag and drop items in a particular sort order and we persist that order in the database table using this “Order Rank Integer”.
Trunc(sysdate) in SQL Server
What is the equivalent of: …in SQL Server 2005? Answer Recommended: This is another alternative, but it’s risky because of casting to a FLOAT. It’s also been demonstrated to not scale performance as well as the DATEADD/DATEDIFF approach.
How do I compare two columns for equality in SQL Server?
I have two columns that are joined together on certain criteria, but I would also like to check if two other columns are identical and then return a bit field if they are. Is there a simpler solution than using CASE WHEN? Ideally I could just use: Answer What’s wrong with CASE for this? In order to see the result,
SQL : BETWEEN vs =
In SQL Server 2000 and 2005: what is the difference between these two WHERE clauses? which one I should use on which scenarios? Query 1: Query 2: (Edit: the second Eventdate was originally missing, so the query was syntactically wrong) Answer They are identical: BETWEEN is a shorthand for the longer syntax in the question that includes both values (EventDate