Tag: tsql

How to calculate difference in hours (decimal) between two dates in SQL Server?

I have to calculate the difference in hours (decimal type) between two dates in SQL Server 2008. I couldn’t find any useful technique to convert datetime to decimal with ‘CONVERT’ on MSDN. Can anybody help me with that? UPDATE: To be clear, I need the fractional part as well (thus decimal type). So from 9:00 […]

What's the best way to select the minimum value from several columns?

Given the following table in SQL Server 2005: ID Col1 Col2 Col3 — —- —- —- 1 3 34 76 2 32 976 24 3 7 235 3 4 245 1 792 What is the best way to write the query that yields the following result (i.e. one that yields the final column – a […]

How to check if a stored procedure exists before creating it

I have a SQL script that has to be run every time a client executes the “database management” functionality. The script includes creating stored procedures on the client database. Some of these clients might already have the stored procedure upon running the script, and some may not. I need to have the missing stored procedures […]

Dynamic update statement with variable column names

We’re looking to do an update in several SQL Server databases to change all NULL values in a certain table to be empty strings instead of NULL. We’re potentially going to be doing this across hundreds of databases. The table name will always be the same, but the column names are variable based on how […]

varbinary to string on SQL Server

How to convert a column value from varbinary(max) to varchar in human-readable form?

Why do you create a View in a database?

When and Why does some one decide that they need to create a View in their database? Why not just run a normal stored procedure or select?

How do I use the CONCAT function in SQL Server 2008 R2?

I was looking for a CONCAT function in SQL Server 2008 R2. I found the link for this function. But when I use this function, it gives the following error: Msg 195, Level 15, State 10, Line 7 ‘CONCAT’ is not a recognized built-in function name. Does the CONCAT function exists in SQL Server 2008 […]

Multiple Column Pivot in T-SQL

I am working with a table where there are multiple rows that I need pivoted into columns. So the pivot is the perfect solution for this, and works well when all I need is one field. I am needing to return several fields based upon the pivot. Here is the pseudo code with specifics stripped […]

Pivot using SQL Server 2000

I put together a sample scenario of my issue and I hope its enough for someone to point me in the right direction. I have two tables Products Product Meta I need a result set of the following

Is there a performance difference between CTE , Sub-Query, Temporary Table or Table Variable?

In this excellent SO question, differences between CTE and sub-queries were discussed. I would like to specifically ask: in what circumstance is each of the following more efficient/faster? CTE Sub-Query Temporary Table Table Variable Traditionally, I’ve used lots of temp tables in developing stored procedures – as they seem more readable than lots of intertwined […]

MS SQL Server is a Microsoft SQL Database product, include sql server standard, sql server management studio, sql server express and so on.