I’ve a SQL query that queries an enormous (as in, hundreds of views/tables with hard-to-read names like CMM-CPP-FAP-ADD) database that I don’t need nor want to understand. The result of this query needs to be stored in a staging table to feed a report. I need to create the staging table, but with hundreds of views/tables to dig through to
Tag: sql-server
How to calculate age (in years) based on Date of Birth and getDate()
I have a table listing people along with their date of birth (currently a nvarchar(25)) How can I convert that to a date, and then calculate their age in years? My data looks as follows I would like to see: Answer There are issues with leap year/days and the following method, see the update below: try this: OUTPUT: UPDATE here
How to use DATEDIFF to return year, month and day?
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
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
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
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
How to add a Try/Catch to SQL Stored Procedure
In this proc how can I handle try catch for exception? Answer See TRY…CATCH (Transact-SQL)
Counting DISTINCT over multiple columns
Is there a better way of doing a query like this: SELECT COUNT(*) FROM (SELECT DISTINCT DocumentId, DocumentSessionId FROM DocumentOutputItems) AS internalQuery I need to count the number of …