How can I use DATEDIFF to return the difference between two dates in years, months and days in SQL Server 2005 How to result that: 2 year 3 month 10 day Can anyone complete this t-sql? Answer Here’s my solution to Eric’s function: Good call on the use of ABS to handle if the start date is after the end
SELECT SUM as field
Suppose i have this table table (a,b,c,d). Datatypes are not important. I want to do this select a as a1,b as b1,c as c1, (select sum(d) from table where a=a1 and b=b1) as total from table …
Determine a table’s primary key using TSQL
I’d like to determine the primary key of a table using TSQL (stored procedure or system table is fine). Is there such a mechanism in SQL Server (2005 or 2008)?
Selecting COUNT(*) with DISTINCT
In SQL Server 2005 I have a table cm_production that lists all the code that’s been put into production. The table has a ticket_number, program_type, program_name and push_number along with some other columns. GOAL: Count all the DISTINCT program names by program type and push number. What I have so far is: This gets me partway there, but it’s counting
Is a commit needed on a select query in DB2?
I have a vendor reporting product executing queries to pull report data, no inserts, no updates just reading data. We have double our heap size 3 times and are now at 1024 4k pages, The app will run …
SQL Server: Optional variable in a stored procedure
I would like to know if there is anyway I can set one of my stored procedure parameter as optional. Answer Providing a default value to the stored procedure parameter will make it optional. EDIT: CREATE PROC [ EDURE ] [ owner. ] procedure_name [ ; number ] [ { @parameter data_type } [ VARYING ] [ = default ]
How to implement a do-while loop in tsql
I’m trying to figure how to implement this in TSQL The only iterative control flow sentence provided by Transact-SQL is while (condition) sentences that first evaluates the condition and if that condition is true then execute the sentence. I’m thinking in a scenario like execute a UPDATE statement over a table until some condition triggered y the last UPDATE executed
How to make a SQL query for last transaction of every account?
Say I have a table “transactions” that has columns “acct_id” “trans_date” and “trans_type” and I want to filter this table so that I have just the last transaction for each account. Clearly I could …
How to count occurrences of a column value efficiently in SQL?
I have a table of students: I want to query for all students, and an additional column that counts how many students are of the same age: What’s the most efficient way of doing this? I fear that a sub-query will be slow, and I’m wondering if there’s a better way. Is there? Answer This should work: If you need
T-SQL – Aliasing using “=” versus “as” [closed]
Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing this post. Closed 3 years ago. This post was edited and submitted for review last month and failed to reopen the post: Original close reason(s) were not resolved Improve this