Showing posts with label somedata. Show all posts
Showing posts with label somedata. Show all posts

Tuesday, March 20, 2012

CASE in a WHERE clause?

How can I do this?

Select somedata

From sometable

Where somecolumn = somevalue

and

CASE WHEN someparam = 1 THEN somecolumn2 <> somevalue2 ELSE somecolumn2 like '%'

Or this?

Select somedata

From sometable

Where somecolumn = somevalue

IF someparam = 1 AND somecolumn2 <> somevalue2

The error I am getting is Incorrect syntax near '<' using the first statement. Using the second statement I get an 'Error near IF' error or something like that.

Thanks for any help!

Hi !

As you see you can't do it that way, but you probably could use derived table syntax:

select dT.*, case when dt.newcol1>42 then '42t' else cast(dt.newcol2 as varchar(10)) end newcolX

from

( select t.*, case when t.col1='a' then 'A' else '' end newcol1,

case t.col2 when 2 then col3+col4 else col5 end newcol2

-- you can also use nested case...

from sometable t

) dt

where dt.newcol2 like 'text';

Best regards

Bjorn

P.S. there is no meaning in select-stmt, I'm just showing syntax...

|||

1.

Not sure this is the best way but you could generate your sql dynamically as a varchar then use EXEC to run the varchar

2.

Again, not sure this is correct but try:

.....WHERE (somecolumn = somevalue AND someparam = 1 AND somecolumn2 <> somevalue2)

OR

someparam <> 1

Perhaps even a union query could work for number 2

like I said they probably aren't 100% correct but hopefully point you in the right direction

|||hi,

i'm not sure what your requirements really are.. hope you can gets something from this example

declare @.ab varchar(100)

set @.ab = 1

select *
from (
select 'the quick' as a, 'brown fox' as b union all
select 'jumps over' as a, 'tha lazy dog' as b
) ab
where b = 'brown fox'
and a <> (case when @.ab = 1 then 'the quick' else '%' end)

set @.ab = 2

select *
from (
select 'the quick' as a, 'brown fox' as b union all
select 'jumps over' as a, 'tha lazy dog' as b
) ab
where b = 'brown fox'
and a <> (case when @.ab = 1 then 'the quick' else '%' end)

again, it really depends on the conditions that you need to evaluate|||

Use the following query..

Code Snippet

Select

somedata

From

sometable

Where

somecolumn = somevalue

And

(

(someparam = 1 And somecolumn2 <> somevalue2)

Or

(someparam <> 1 And somecolumn2 like '%')

)

|||

Thank you!!

|||

sarolabelle,

A "case" expression returns a value, so you need to compare the value returned by the "case" expression to something, in order to have a valid logic expression.

Select

somedata

From

sometable

Where

somecolumn = somevalue

and

CASE

WHEN someparam = 1 and somecolumn2 <> somevalue2 then 1

WHEN someparam != 1 and somecolumn2 like '%' then 1

else 0

end = 1

go

AMB

|||

Try this one

-- Case in Where

Code Snippet

Select * From EMP

Where (Country = 'India')

AND

(CASE

WHEN Salary < 5000 then 1

WHEN Age > 18 then 1

END = 1)

-- Case & where (individual)

Code Snippet

SELECT Name, Salary,

CASE

WHEN Salary < 5000 THEN 'No Tax'

WHEN Salary > 5000 THEN 'High Tax'

WHEN Salary = 5000 THEN 'Low Tax'

ELSE 'Invalid Data'

END

AS Tax

FROM EMP

WHERE country = 'USA'

for more details logon www.sqltree.com (under Conditional Processing folder)

Thanks

|||SQLTree -> It is not answering the question. Here the requirement is filtering the data.

Wednesday, March 7, 2012

Carriage return /New line in a Text field

Hi
Is there any way to force a carriage return/new line when inserting some
data into a text field?
I'm updating a text field with data from other fields, but in order to make
it look nice on a printout, I'd like to split it onto two lines. I've looked
in BOL, but haven't really been able to find anything that seems to be able
to do it.
Regards
Steen
Hi Steen,
Please check CHAR in SQL BOL.
I believe CHAR(13) is what you are looking for.
Cheers,
Des
"Steen Persson" <SPE@.REMOVEdatea.dk> wrote in message
news:u7O7RBvmEHA.3988@.tk2msftngp13.phx.gbl...
> Hi
> Is there any way to force a carriage return/new line when inserting some
> data into a text field?
> I'm updating a text field with data from other fields, but in order to
> make
> it look nice on a printout, I'd like to split it onto two lines. I've
> looked
> in BOL, but haven't really been able to find anything that seems to be
> able
> to do it.
> Regards
> Steen
>
|||Yes - that looks like what I was looking for.
Thanks a lot...
Regards
Steen
Des FitzGerald wrote:[vbcol=seagreen]
> Hi Steen,
> Please check CHAR in SQL BOL.
> I believe CHAR(13) is what you are looking for.
> Cheers,
> Des
>
> "Steen Persson" <SPE@.REMOVEdatea.dk> wrote in message
> news:u7O7RBvmEHA.3988@.tk2msftngp13.phx.gbl...
|||You may actually need to use a combination of char(13) +
char(10) depending on your settings. Windows apps
sometimes look for both (Carriage return/linefeed). UNIX
based systems generally only look for the one control
character (Carriage return is a carriage return, line
feed feeds a line but doesn't change your horizontal
position).
Just a thought.
Matthew Bando
Mattehw.Bando@.remove csctgi.com
[vbcol=seagreen]
>--Original Message--
>Yes - that looks like what I was looking for.
>Thanks a lot...
>
>Regards
>Steen
>Des FitzGerald wrote:
when inserting[vbcol=seagreen]
fields, but in order[vbcol=seagreen]
two lines. I've[vbcol=seagreen]
that seems to
>
>.
>