Showing posts with label Basic SQL Queries. Show all posts
Showing posts with label Basic SQL Queries. Show all posts

Sunday, December 13, 2015

TRIM Function in SQL Server



For trim the spaces SQL Server has no function which can do trimming from both end instead it has function LTRIM and RTRIM.
You can create your own function and use it in your code or query whenever required. It will be termed as UDF.

CREATE FUNCTION dbo.TRIM(@string VARCHAR(MAX))
RETURNS VARCHAR(MAX)
BEGIN
RETURN LTRIM(RTRIM(@string))
END
GO

This function will take input as string which need to be trimmed.

Tuesday, July 14, 2015

Query for finding the table exists in definition or codes of objects.

Query for finding the table exists in different definition or codes of objects.

For example if you want to delete or obsolete any table you will first check if exist in
any object code. If you wont correct that code that object will throw error after
deleting the table.


SELECT DISTINCT obj.name, obj.type
FROM syscomments COM
INNER JOIN sys.objects OBJ ON com.id=obj.object_id
WHERE COM.TEXT LIKE '%Tablename%'

You can create the sp using this code which will return list of object having table

name in code.

Check out Video below...

Sunday, February 08, 2015

Total Records for Each table in a database

;WITH CTE_DATABASETABLESIZE (DATABASENAME, SCHEMANAME, TABLENAME, TOTALROWS)
AS
(
SELECT 
 DB_NAME(), SCHEMA_NAME(SO.UID), SO.NAME, SI.ROWS
FROM 
 SYSOBJECTS SO
 INNER JOIN SYSINDEXES SI
 ON SO.ID = SI.ID
WHERE
 TYPE = ‘U’ AND SI.INDID IN (0,1)
)
SELECT * FROM CTE_DATABASETABLESIZE
/*ORDER BY SCHEMANAME
    ORDER BY TOTALROWS ASC*/
ORDER BY TOTALROWS DESC

Saturday, January 03, 2015

Query Analyzer ShortCuts

Shortcuts Use
ALT+F1 Database object information
ALT+F4 Exit
CTRL+A Select all
CTRL+B Move the splitter
CTRL+C Copy
CTRL+D Display results in grid format
CTRL+Delete Delete through the end of the line
CTRL+E Execute query
CTRL+F Find
CTRL+F2 Insert/remove bookmark
CTRL+F4 Disconnect
CTRL+F5 Parse query and check syntax
CTRL+G Go to line
CTRL+H Replace
CTRL+I Index Tuning Wizard
CTRL+K Display/hide execution plan
CTRL+L Display execution plan
CTRL+N New query window
CTRL+O Connect
CTRL+P Print
CTRL+R Show/Hide results pane
CTRL+S Save
CTRL+SHIFT+0 Show options
CTRL+SHIFT+C Comment out code
CTRL+SHIFT+DEL Clear the active Editor pane
CTRL+SHIFT+F Save results to file

Thursday, November 07, 2013

How to check SQL Server Version


Below are the queries which can be used to check the version of sql server on which you are running the query
copy these query in query analyzer window and chaeck the version.

SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY ('productlevel'), SERVERPROPERTY ('edition')

SELECT @@VERSION

Query to check table Uses in other DB Objects

In the requirement where user want to find out that the particular table used or exist in which all db objects like SP, Views etc.
This is necessary like when you want ot purge the table or change any column of table you should check the the objects using that table shoulnd stop working.
Below is the query which will provide this details

SELECT b.name, b.xtype
FROM syscomments A
INNER JOIN sysobjects B ON A.id=B.id
WHERE A.TEXT LIKE '%Department%'