Showing posts with label case. Show all posts
Showing posts with label case. Show all posts

Sunday, March 11, 2012

Custom Order By...Revisited

Hi


I would like to return rows in a custom order, similar to this post. However, in my case I do not have a fixed number of 'sort by' items. The order I would like to have returned from Col2 is:

    'A' Numeric Values in numeric order 0-99 All other alpha values
For example:
'A', '0', '1', '3', '65', 'MyValue1', 'MyValue2'

How could I achieve this?

thanks

Richard

Here You Go...

Code Snippet

Create Table #sorttest (

[Col2] Varchar(100)

);

Insert Into #sorttest Values('A');

Insert Into #sorttest Values('12');

Insert Into #sorttest Values('1');

Insert Into #sorttest Values('3');

Insert Into #sorttest Values('5');

Insert Into #sorttest Values('6');

Insert Into #sorttest Values('78');

Insert Into #sorttest Values('100');

Insert Into #sorttest Values('MyValue1');

Insert Into #sorttest Values('MyValue3');

Insert Into #sorttest Values('MyValue2');

Select

*

From

#sorttest

Order By

Case When Col2 Like '[A-Z]' Then 1

When Isnumeric(Col2)=1 Then 2

Else 3 End,

Case When Isnumeric(Col2)=1 Then Convert(float,Col2) End,

Col2

/*

::Output

Col2

-

A

1

3

5

6

12

78

100

MyValue1

MyValue2

MyValue3

*/

|||hi, try this

SELECT MyMixedField
FROM MyTable
WHERE PATINDEX('%[0-9]%', MyMixedField) = 0
UNION ALL
SELECT MyMixedField
FROM MyTable
WHERE ISNUMERIC(MyMixedField) = 1
UNION ALL
SELECT MyMixedField
FROM MyTable
WHERE PATINDEX('%[0-9]%', MyMixedField) > 0
and ISNUMERIC(MyMixedField) <> 1|||i think manivannan's solution is much better,

@.manivannan..

it think its better to use patindex('%[0-9]%', col2) = 0 for alpha valued records

Order By

Case When patindex('%[0-9]%', col2) = 0 Then 1

When Isnumeric(Col2)=1 Then 2

Else 3 End,

Case When Isnumeric(Col2)=1 Then Convert(float,Col2) End,

Col2|||thank you both very much for your quick and accurate responses!

Works perfectly
|||

It seems that manivannan's suggested use of

Like '[A-Z]'

is most likely the best option to sort on single letter alpha. Don't you think that the

patindex('%[0-9]%', col2) = 0

suggestion 'might' include things like an asterisk, period, @. symbol, $ symbol, etc.? (-As well as allowing multi-character alpha entries on the first sort level...)

However, I would caution that using manivannan's suggestion to use isnumeric() 'could' cause problems. For example, the following evaluates to TRUE

SELECT isnumeric( '$' )

But is it a number? Should it sort with the alphas?

|||Hi

Good point, but actually in this case we don't allow non-alphanumeric characters in this field, so it's not really an issue!

thanks everyone for help on this

Friday, February 24, 2012

Custom code using case statement

My report contains a field which shows a '# of days on Hand' column. This
column is the result of a datediff calc. I am not familar with vb.net but I
need to add custom code logic so that if the value in '# of days on hand" is
null that it shows a hardcoded text like "inv" or "onhand". The rest of the
value are >= 0 and I would just want to show there values as-is. Can anyone
show me what the a sample of code would look like to do this and how I call
this in my report?
ANY HELP IS MUCH APPRECIATEDAn expression would work well in this situation
=iif(Fields!column1.Value < 0,"inv",Fields!column1.Value)
I haven't tested this but it should work. If the expression evaluates to
true, i.e. If the value of the field is less than zero, then the string
'inv' is returned, if false, the field's value is returned as-is.
Put this in your column and replace Fields!column1 with whatever your field
is called.
HTH
"stacey" wrote:
> My report contains a field which shows a '# of days on Hand' column. This
> column is the result of a datediff calc. I am not familar with vb.net but I
> need to add custom code logic so that if the value in '# of days on hand" is
> null that it shows a hardcoded text like "inv" or "onhand". The rest of the
> value are >= 0 and I would just want to show there values as-is. Can anyone
> show me what the a sample of code would look like to do this and how I call
> this in my report?
> ANY HELP IS MUCH APPRECIATED

Tuesday, February 14, 2012

cursors and tempdb

Do dynamic cursors utilise tempdb for processing? I can't seem to figure
if that's the case.
It does appear that they use memory. In fact, do all cursor types use
tempdb and/or memory?
TIA
Dave
Dodo Lurker wrote:
> Do dynamic cursors utilise tempdb for processing? I can't seem to figure
> if that's the case.
> It does appear that they use memory. In fact, do all cursor types use
> tempdb and/or memory?
> TIA
> Dave
Dynamic cursors don't use Tempdb. Static and keyset ones do. Any
operation that reads data will utilise RAM and cache.
Attach standard cursor disclaimer: At least 99.99% of the time cursors
are a bad choice for solving problems in SQL. Usually there are better,
faster, simpler solutions that don't require cursors.
David Portas
SQL Server MVP

cursors and tempdb

Do dynamic cursors utilise tempdb for processing? I can't seem to figure
if that's the case.
It does appear that they use memory. In fact, do all cursor types use
tempdb and/or memory?
TIA
DaveDodo Lurker wrote:
> Do dynamic cursors utilise tempdb for processing? I can't seem to figure
> if that's the case.
> It does appear that they use memory. In fact, do all cursor types use
> tempdb and/or memory?
> TIA
> Dave
Dynamic cursors don't use Tempdb. Static and keyset ones do. Any
operation that reads data will utilise RAM and cache.
Attach standard cursor disclaimer: At least 99.99% of the time cursors
are a bad choice for solving problems in SQL. Usually there are better,
faster, simpler solutions that don't require cursors.
David Portas
SQL Server MVP
--

cursors and tempdb

Do dynamic cursors utilise tempdb for processing? I can't seem to figure
if that's the case.
It does appear that they use memory. In fact, do all cursor types use
tempdb and/or memory?
TIA
DaveDodo Lurker wrote:
> Do dynamic cursors utilise tempdb for processing? I can't seem to figure
> if that's the case.
> It does appear that they use memory. In fact, do all cursor types use
> tempdb and/or memory?
> TIA
> Dave
Dynamic cursors don't use Tempdb. Static and keyset ones do. Any
operation that reads data will utilise RAM and cache.
Attach standard cursor disclaimer: At least 99.99% of the time cursors
are a bad choice for solving problems in SQL. Usually there are better,
faster, simpler solutions that don't require cursors.
--
David Portas
SQL Server MVP
--