Thursday, March 13, 2014

Sample Sql Server Queries for Daily Use

Leave a Comment
In this article I am listing some basic and very important Sql queries which we use in our daily development tasks as Database Administrator or as Front-End Developer.

1. Sql Query to Select Day from Given Date:

Query:
 select DAY('2014-02-07 11:48:48.020') as DAYOFDATE

Output:

2. Sql Query to Select Month from Given Date:

Query
Select MONTH('2014-02-07 11:48:48.020') as MonthOfDate

Output: 

3. Sql Query to get Year from Given Date:

Query:
select YEAR('2014-02-07 11:48:48.020') as YearofDate

Output:
sql query to select year from date

4. Sql Query to Get the Length of String or Text

Query
Select DATALENGTH('Name') as LegthofName

Output:
sql query to get length of string

5. Sql Query to get the version of Sql Server Installed:

Query:
Select @@VERSION As SQLVERSIONInstalled

Output:
Sql Query to Get Sql Server version

6. Sql Query to Get Position(Index) of a character in a String:
Query:
Select CHARINDEX('a','bd,ab,cd') AS CharacterIndex

Here in this query a is the character whose index is to be found in thee string 'bd,ab,cd'

Output:
get charindex in sql server


7. Sql Query to Get the length of the Column in a table:

Query:
Select COL_LENGTH('name of table','column name')As LengthofColumn

8. Sql Query to select the random records from a table:

Query:
Select  * from name_of_table order by NEWID()

9. Sql Query to select specific number of character from the Right Side of String:

Query:
Select RIGHT('this is test',9) as textFromRight

Output:
sql right function

10. Sql Query to select specified number of characters from the left side of the string:

Query:
Select LEFT('This is testing website',9) as textFromLeft

Output:
sql left function


0 comments:

Post a Comment