Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 25th, 2008, 02:51 PM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default [Resolved] Using Substring in the Where clause

I have a stored procedure where I would like to use substring in the where clause, if possible.

Code:
CREATE PROCEDURE GetJobList
@PlantId char(3)

Select ......
From ......
WHERE     (dbo.Source.CompanySourceId = 'PROD' and dbo.Batch.ReportDate >= @DateFrom and dbo.Batch.ReportDate <= @DateTo and (if @PlantId <> ' '  then SUBSTRING(dbo.Job.CompanyJobId, 1,3) =  @PlantId))
I get syntax error "Incorrect syntax near 'if, incorrect sysntax near 'then'....
 
Old January 26th, 2008, 01:58 PM
Friend of Wrox
 
Join Date: Oct 2007
Posts: 130
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via AIM to urtrivedi
Default

Select ......
From ......
WHERE (dbo.Source.CompanySourceId = 'PROD' and dbo.Batch.ReportDate >= @DateFrom and dbo.Batch.ReportDate <= @DateTo and (case @PlantId <> ' ' then SUBSTRING(dbo.Job.CompanyJobId, 1,3) else '' end = @PlantId))




urt
 
Old January 28th, 2008, 09:22 AM
Friend of Wrox
 
Join Date: Mar 2007
Posts: 205
Thanks: 4
Thanked 0 Times in 0 Posts
Default

It worked. Thank you.

 
Old February 1st, 2008, 07:50 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

And if you want to use any present index, try
Code:
Select    ...
From    ...
WHERE    dbo.Source.CompanySourceId = 'PROD'
    and dbo.Batch.ReportDate >= @DateFrom
    and dbo.Batch.ReportDate <= @DateTo
    and dbo.Job.CompanyJobId like nullif(@PlantId, '') + '%'







Similar Threads
Thread Thread Starter Forum Replies Last Post
substring in c# sudhirbharti C# 1 February 21st, 2008 01:29 PM
Substring-before with Regex bonekrusher XSLT 6 November 27th, 2007 01:16 PM
Substring-after collation bonekrusher XSLT 2 May 11th, 2007 09:31 AM
instead of substring what should we use in MSACCES miruthula SQL Language 1 February 9th, 2007 12:13 PM
SubString prasanta2expert Access VBA 1 November 17th, 2006 10:04 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.