Showing posts with label sort. Show all posts
Showing posts with label sort. Show all posts

Friday, March 9, 2012

is there any way to sort the SELECT statement?

hi,

i have a stored procedure

SELECT PictureID,Left(Name, 16) +'...'AS ShortNameFROM Pictures

some pictures has their name lower that 16 characters, and when i show on the page is something like "Sunrise..."

how can i 'sort' the Select so to return the ShortName if the name is greater than 16 characters or return Name if is lower

i hope you understand what i mean...

thanks

Hi,

You have to use the Case Statement

SELECT PictureId, Case When Len(Name)>16 THEN Left(Name,16)+'...' ELSE Name End AS ShortName From Pictures.

Hope this helps.

|||

thank you v v much

|||

You are welcome. I am glad I could help.

Monday, February 20, 2012

Is there an equivalent to an MSAccess IIF statement when creating Store Procs

I'm creating a stored proc and need to use some sort of IF or IIF statement. Is theresome sort of equivalent statement i can use?T-SQL supports an IF and CASE statment. you can read up on both in Books Online.

IF...ELSE
Imposes conditions on the execution of a Transact-SQL statement. The Transact-SQL statement following an IF keyword and its condition is executed if the condition is satisfied (when the Boolean expression returns TRUE). The optional ELSE keyword introduces an alternate Transact-SQL statement that is executed when the IF condition is not satisfied (when the Boolean expression returns FALSE).

Syntax
IF Boolean_expression
{ sql_statement | statement_block }
[ ELSE
{ sql_statement | statement_block } ]

CASE
Evaluates a list of conditions and returns one of multiple possible result expressions.

CASE has two formats:

The simple CASE function compares an expression to a set of simple expressions to determine the result.

The searched CASE function evaluates a set of Boolean expressions to determine the result.
Both formats support an optional ELSE argument.

Syntax
Simple CASE function:

CASE input_expression
WHEN when_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
]
END

Searched CASE function:

CASE
WHEN Boolean_expression THEN result_expression
[ ...n ]
[
ELSE else_result_expression
]
END|||IF(<CHECK CONDITION>)
BEGIN
<CODE>
END
ELSE
BEGIN
< CODE>
END|||IIF doesn't exists in SQL but you can always use the old fashion way:

IF <your_condition>
BEGIN
:
:
END
ELSE
BEGIN
:
:
END

Originally posted by Sammy_S
I'm creating a stored proc and need to use some sort of IF or IIF statement. Is theresome sort of equivalent statement i can use?