Querying Data in SQL Server

Leave a comment (2) Go to comments

In this chapter we will learn how to query a database in SQL Server, I am using sql server 2008 & our database name would be HECTOR & the table name would be table1. First we will learn how to query the whole content & columns in a table. First we have to enter the database by using use command & the command for the above query would be as follows

USE HECTOR 
SELECT * FROM TABLE1

Querying Data in SQL Server sql server tutorials Querying data in sql server customizing views in sql server concatenating columns in sql server

 

It is showing all the columns & contents of it, if you want to query on one column only you have to write the name of the column instead of * in the above statement i.e. SELECT SNAME FROM TABLE1.

CUSTOMIZING VIEW

Now we will learn how to customize the display, suppose I want to name STDNAME as NEWNAME, STDID as NEWID & STDADD as NEWADD. I have three ways to do it

SELECT 'NEWNAME'= STDNAME,'NEWID'=STDID,'NEWADD'=STDADD FROM TABLE1


SELECT STDNAME 'NEWNAME', STDID 'NEWID', STDADD 'NEWADD' FROM TABLE1

SELECT STDNAME AS 'NEWNAME', STDID AS 'NEWID', STDADD AS 'NEWADD' FROM TABLE1

The result will be same after using anyone of the above statement.

Querying Data in SQL Server sql server tutorials Querying data in sql server customizing views in sql server concatenating columns in sql server

CONCATENATING THE TEXT VALUES IN OUTPUT

Suppose you want to see the text of two columns in a single column only, for this you have to concatenate the required columns. For example I want to concatenate two columns (STDNAME, STDADD) in table1, the command would be as follows

OUTPUT =STDNAME+'  '+STDADD FROM TABLE1

Querying Data in SQL Server sql server tutorials Querying data in sql server customizing views in sql server concatenating columns in sql server

And now if you want to add any text in every row of your specified column you just have to add you text next to the column name in ‘YOUR TEXT HERE’.

SELECT OUTPUT =STDNAME+'is the part of STDNAME'+ STDADD+'is the part of STDadd' FROM TABLE1

The output would be as follows

Querying Data in SQL Server sql server tutorials Querying data in sql server customizing views in sql server concatenating columns in sql server

We will learn how to do calculations on a user defined Column in our Next Post. So please STAY TUNED with us to learn more about SQL Server.

EOF - Querying Data in SQL Server, SQL Server 2012 , 2008 R2 , 2008 , 2005 , 2000
Leave a comment

2 Comments.

Leave a Reply

Your email address will not be published. Required fields are marked *


*


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of followup comments via e-mail. You can also subscribe without commenting.

Trackbacks and Pingbacks: