Tag Archives | T-SQL

Update consecutive numbering with UPDATE statement – MSSQL

I want to update a table with consecutive numbering starting at 1. The update has a where clause so only results that meet the clause will be renumbered. Can I accomplish this efficiently without using a temp table?

Read full story Comments { 0 }

Find Duplicate Values with SQL Script

Let’s assume that data has been entered in a table and now you want to find if a table column have duplicated values. Here is an SQL script to join data from one table for comparison purpose. The table must have a primary key column (unique column) and the second column for comparison. Here is […]

Read full story Comments { 0 }

FORMAT Function for Transact-SQL – T-SQL

Returns a value formatted with the specified format and optional culture in SQL Server 2016. Use the FORMAT function for locale-aware formatting of date/time and number values as strings. FORMAT ( value, format [, culture ] ) Return Types:  nvarchar or null The length of the return value is determined by the format. Remarks:   FORMAT returns […]

Read full story Comments { 0 }

T-SQL Script to Group Data by Minute

If you are doing a mass data insert into a table and you want to know how fast is inserting data. I have created a script that groups data by minute, based in a DateTime data type field.

Read full story Comments { 0 }

T/SQL Generate Random Numbers

SQL Server has a built-in function that generates a random number, the RAND() mathematical function.  The RAND math function returns a random float value from 0 through 1.  It can take an optional seed parameter, which is an integer expression (tinyint, smallint or int) that gives the seed or start value. To use it, you […]

Read full story Comments { 0 }

T-SQL Comma Delimited Output

One of the common tasks performed when retrieving data from a SQL Server database is returning the result as a comma-delimited output instead of a result set.  This task can be performed by using a CURSOR selecting the column to be concatenated together.  Each row returned by the CURSOR is then concatenated together into a […]

Read full story Comments { 0 }

Find the Server Name or Instance Name using T-SQL

The following instructions are supposed to return the same results,  but sometimes the results can be different. The reason is that SERVERPROPERTY automatically reports changes in the network name of the computer, while @@SERVERNAME don’t. SELECT CONVERT(sysname, SERVERPROPERTY(N’servername’)) GO SELECT @@SERVERNAME When they are run on the default instance they return just the server name, […]

Read full story Comments { 0 }

Query to Find Column From All Tables of Database

Find how many tables in database AdventureWorks have column name like ‘EmployeeID’.

Read full story Comments { 0 }