Tag Archives: SQL Server

List out all the table names & it’s info of selected Database using SQL Server


–Select YourDatabase

USE AdventureWorks

Go

–Query to list out all the Tables in the selected DB

SELECT * FROM sys.Tables

 

In Above result, all rows are not ordered on any basis. You can given more specific query to sort out / ordered by any column. For example, now we considered the column “Name” to display the result in ordered.

 

–Same Result which is Alphabetically ordered by the Column “Name”

SELECT * FROM sys.Tables ORDER BY [name]

 

You can observe in the above result. All rows are ordered by the column “Name”. See it’s very right??? Like this simple & twisting, we can see a lot unless you get a situation.

 

Happy Coding….

 

How to Remove Last n Letter / Character from the given String using SQL Server


Here ‘m going to remove 3 letter / character from the given string. You can consider any number of letter to remove.

When i came to need it, i searched a lot. Yes Ofcourse i got lot of answers in many logic way. But among all the answers finally i got this solution as very easy to understand & easy to fix. its just a small piece of code to put in b/w query.

Let us see…

— Remove last 3 letter from the String

DECLARE @String VARCHAR(50)

SET @String = ‘TESTSTRING123AB’

— Chop off the end character

SET @String = LEFT(@String, LEN(@String) – 3) — can specify any number

SELECT @String AS Result

Please let me know, if you have any questions….

Happy Coding…