Add IDENTITY or Unique number to a table – T/SQL

I want to update my table records. However, my existing table does not have any unique columns. So, I need to append Identity column and update all the records based on that Identity column. For example, If my temp table has 1000 records without any unique column values. I need to number all these 1000 records and update the values. There is no need to do an UPDATE. The identity column is going to be populated when it is created….

Read More

MSSQL – Query to Find Column From All Tables of Database

How many tables in the database AdventureWorks have column names like ‘EmployeeID’? It was quite an interesting question and I thought if there are scripts that can do this would be great. I quickly wrote down the following script which will go return all the tables containing specific columns along with their schema name.

Read More

SQL Time Zone Conversion Functions

I did a lot of research on Microsoft’s website, and the only time zone conversion functions that I could find were relative to analysis services, and would not provide me with a solution to my problem. So, my options were to pay someone to modify our application, or create a process in the database environment to perform the conversions as data is inserted into the common database environment.

Read More

How can I speed up queries on a large table?

Any process or action that requires querying the database and getting results can be optimized by adding database indexes. An index is used to speed up the performance of queries. It does this by reducing the number of database data pages that must be visited/scanned. In SQL Server, a clustered index determines the physical order of data in a table. There can be only one clustered index per table (the clustered index IS the table) What is an index?So,…

Read More

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 ) Return Types:  nvarchar or null The length of the return value is determined by the format. Remarks:   FORMAT returns NULL for errors other than a culture that is not valid. For example, NULL is returned if the value specified in format is not…

Read More

T/SQL Splint String/Text

One common task performed in database applications is given a full name, how can this be split into the first name and last name.  In this article, three different methods are discussed to perform this task using three different functions in SQL Server.  It is assumed in this article that the full name is just made up of one first name and one last name. Using SUBSTRING Function The first method is the use of the SUBSTRING string function,…

Read More

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 can simply do a simple SELECT, as follows: SELECT RAND() AS The result generated by this SELECT statement is as follows (note that…

Read More