TechArena Community

TechArena Community (
-   Software Development (
-   -   Problem With SQL Database (

Kusagra 26-03-2009 01:53 PM

Problem With SQL Database
I am not a good programmer in sql programming language. I am creating a program in sql database in order to display a table consisting of two columns.

The requirement for the query code is:
Select Desc,Name from tablename ORDER BY Desc.
I want to display only the Desc data in the UI.
But in case if the Desc is having a null,I will show the "Name" in the UI.

Please suggest some solution regarding this

Praetor 26-03-2009 01:58 PM

Re: Problem With SQL Database
The syntax for an ORDER BY statement is as follows:

SELECT "column_name"
FROM "table_name"
[WHERE "condition"]
ORDER BY "column_name" [ASC, DESC]

The [] means that the WHERE statement is optional. However, if a WHERE clause exists, it comes before the ORDER BY clause. ASC means that the results will be shown in ascending order, and DESC means that the results will be shown in descending order. If neither is specified, the default is ASC.

It is possible to order by more than one column. In this case, the ORDER BY clause above becomes

ORDER BY "column_name1" [ASC, DESC], "column_name2" [ASC, DESC]

Assuming that we choose ascending order for both columns, the output will be ordered in ascending order according to column 1. If there is a tie for the value of column 1, we then sort in ascending order by column 2.

Katty 26-03-2009 02:00 PM

Re: Problem With SQL Database
When ORDER BY is used in the definition of a view, inline function, derived table, or subquery, the clause is used only to determine the rows returned by the TOP clause. The ORDER BY clause does not guarantee ordered results when these constructs are queried, unless ORDER BY is also specified in the query itself.

kelfro 26-03-2009 02:06 PM

Re: Problem With SQL Database
SELECT ProductID, Name FROM Production.Product
WHERE Name LIKE 'Lock Washer%'


Specifies a column on which to sort. A sort column can be specified as a name or column alias, or a nonnegative integer representing the position of the name or alias in the select list. An integer cannot be specified when the order_by_expression appears in a ranking function. A sort column can include an expression, but when the database is in SQL Server (90) compatibility mode, the expression cannot resolve to a constant. Column names and aliases can be qualified by the table or view name. In SQL Server, qualified column names and aliases are resolved to columns listed in the FROM clause. If order_by_expression is not qualified, the value must be unique among all columns listed in the SELECT statement.

Multiple sort columns can be specified. The sequence of the sort columns in the ORDER BY clause defines the organization of the sorted result set.

The ORDER BY clause can include items that do not appear in the select list. However, if SELECT DISTINCT is specified, or if the statement contains a GROUP BY clause, or if the SELECT statement contains a UNION operator, the sort columns must appear in the select list.

Additionally, when the SELECT statement includes a UNION operator, the column names or column aliases must be those specified in the first select list.

All times are GMT +5.5. The time now is 12:12 AM.