|
-
Jun 27th, 2005, 03:09 AM
#1
Thread Starter
Frenzied Member
an if clause in a sql query
hi all.
I'm just wondering if I can reduce the recordset size on my web page.
I have a query like:
select Top 50 ID, Field1,field2,field3 from tblRecords where....
Field1 in this example is very large text field, and since there are 50, the page is slow.
In fact, I don't need the text field to be returned, what I need is a true or false value to indicate if a substring is present in the field. And I do need the other fields. So I can't restrict this in the where clause.
What I need is:
Select Top 50 ID, Field2, Field3, (if field1 like '%MyPhrase%' Then True Else False) where....
So the top 50 records are returned, but instead of the big text field of data, a True or False is returned per record.
Is that possible? Cast the field type somehow?
Anybody know this? Thanks.
Wengang
Last edited by wengang; Jun 28th, 2005 at 08:04 PM.
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Jun 27th, 2005, 04:05 AM
#2
Re: an if clause in a sql query
I believe your database is SQL Server, couldn't you use Stored Procedures?
-
Jun 27th, 2005, 04:07 AM
#3
Re: an if clause in a sql query
instr function (not sure if this is in Sql server) but it should do the job you want.
Alternatively there is the iif function.
Not sure about time problems when nusing, but worth a try.
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
Jun 27th, 2005, 04:26 AM
#4
Fanatic Member
Re: an if clause in a sql query
try this
Code:
Select Top 50 ID, Field2, Field3,
(CASE WHEN SUBSTRING(field1, 1, 8) = 'MyPhrase' THEN 'True'
ELSE 'False') where....
syntax :
SUBSTRING ( expression , start , length )
search for CASE and SUBSTRING in BOL for more information
PS : i assume it's in MSSQL, for Access try Ecniv's
regards
-
Jun 27th, 2005, 04:49 AM
#5
Thread Starter
Frenzied Member
Re: an if clause in a sql query
Hey all.
This one is Access, Not SS
I tried with IIF but I guess i worded it wrong. the error is:
Syntax error (missing operator) in query expression '(iif(Instr(ArticleHTML, '<img') > 0,'True','False') as HaveImg)'.
basically the same error when using Case When.
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Jun 27th, 2005, 04:53 AM
#6
Re: an if clause in a sql query
Since the field is text it's value cannot be converted to true or false, perhaps you would need another field for it.
-
Jun 27th, 2005, 05:08 AM
#7
Thread Starter
Frenzied Member
Re: an if clause in a sql query
but this is not really a conversion, it is essentially creating a new field based on another field's value
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Jun 27th, 2005, 05:14 AM
#8
Re: an if clause in a sql query
 Originally Posted by wengang
Hey all.
This one is Access, Not SS
I tried with IIF but I guess i worded it wrong. the error is:
Syntax error (missing operator) in query expression '(iif(Instr(ArticleHTML, '<img') > 0,'True','False') as HaveImg)'.
basically the same error when using Case When.
Close, try this:
Code:
(iif(Instr(1,[ArticleHTML], '<img') > 0,'True','False') as HaveImg)
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
Jun 27th, 2005, 05:15 AM
#9
Frenzied Member
Re: an if clause in a sql query
If its access then you can use IIF dont know about substring though
-
Jun 27th, 2005, 01:11 PM
#10
Re: an if clause in a sql query
The first argument to the Instr function is not required if you want to start at the beginning of the string.
The problem is that the placement of the brackets is incorrect.
(iif(Instr(1,[ArticleHTML], '<img') > 0,'True','False') as HaveImg)
should be
(iif(Instr(1,[ArticleHTML], '<img') > 0,'True','False')) as HaveImg
or Just
iif(Instr(1,[ArticleHTML], '<img') > 0,'True','False') as HaveImg
-
Jun 27th, 2005, 08:40 PM
#11
Thread Starter
Frenzied Member
Re: an if clause in a sql query
yes they both work. The extra parenthesis set was my doing, just for clarity.
Also I changed 'True','False' to True, False. They were coming back as strings instead of boolean values.
oddly, though, the page didn't load faster, it loaded slower, took almost twice as long.
What I was doing before was loading the field ArticleHTML, which in some cases was pretty large, then in ASP doing InStr to see if the article had an image or not.
I thought the page would load faster if I didn't require that enormous data chunk returned in the RS.
Does it seem logical that it slowed down? Maybe a different syntax would be faster.
Thanks for the solution. In either case, it's good to know that SQL supports IIF()
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Jun 27th, 2005, 08:45 PM
#12
Thread Starter
Frenzied Member
Re: an if clause in a sql query
Just another quick note. I changed that to
(iif([ArticleHTML] like '%<img%',True,False)) as HaveImg
and no improvement on speed either, nearly the same
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
-
Jun 28th, 2005, 08:46 AM
#13
Re: an if clause in a sql query
Hmmm well you are checking each records field data each time. Perhaps if you saved a flag that was set as the data was stored, then you wouldn't need to do that check (instr or like) on the records... might speed up the return a little.
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
Jun 28th, 2005, 12:20 PM
#14
Re: an if clause in a sql query
That would be a good idea in terms of speed, as Like with % at each end of the string is the slowest possible comparison you can do.
The awkward part is making sure that the flag field is kept up to date, as i don't think that Access supports Triggers. Basically you would have to make sure it is updated each time a row of data is inserted/updated.
-
Jun 28th, 2005, 08:03 PM
#15
Thread Starter
Frenzied Member
Re: an if clause in a sql query
Geez!
That was the solution to start with.
Okay, nobody look at me!
Wen Gang, Programmer
VB6, QB, HTML, ASP, VBScript, Visual C++, Java
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|