Showing posts with label SQL Server. Show all posts
Showing posts with label SQL Server. Show all posts

Wednesday, November 21, 2012

Sort table rows randomly in SQL Server

 

SELECT * FROM table
ORDER BY NEWID()


In the above example NEWID() creates a unique value of type uniqueidentifier for each row and sort the rows accordingly.


Below is the example for retrieving a single random record from a table


SELECT TOP 1 * FROM table
ORDER BY NEWID()


Happy coding…

Monday, August 27, 2012

How to generate row number in sql server

 

SELECT
ROW_NUMBER() OVER( ORDER BY ColumnName),
ColumnName1, ColumnName2,...
FROM TableName


Happy coding…

Tuesday, August 21, 2012

How to replace a string in a SQL Server Table Column

 

Updating SQL Server table column using REPLACE function

UPDATE TABLENAME
SET COLUMNNAME = REPLACE(COLUMNNAME, 'Old String', 'New String')
WHERE CONDITION


Happy coding…

Tuesday, July 17, 2012

Using REPLACE in an UPDATE statement

The REPLACE function is easy to use and very handy with an UPDATE statment.

Replace searches for certain characters in a string and replaces them with other characters.

Syntax:

UPDATE <table name> SET <column name> = REPLACE(<COLUMN Name>, 'oldstring', 'newstring') WHERE <Condition>
Happy Coding…

Wednesday, August 17, 2011

IDENT_CURRENT–Retrieve last inserted identity of record

SELECT IDENT_CURRENT(‘<tablename>’)


It returns the last IDENTITY value produced in a table, regardless of the connection that created the value, and regardless of the scope of the statement that produced the value.
IDENT_CURRENT is not limited by scope and session; it is limited to a specified table. IDENT_CURRENT returns the identity value generated for a specific table in any session and any scope.

Happy Coding…

Wednesday, June 1, 2011

Convert DateTime to Indian Standard Time format in SQL Server

DATEADD() function would be used to convert UTC to IST in SQL Server.

Below is the sample code for that.

DECLARE @UTCTime As DATETIME;
SET @UTCTime = GETUTCDATE();
SELECT DATEADD(MI, 330, @UTCTime) AS IST

Happy Coding…