Sam OZ
asked on
Sql with switch
I am looking for query for followng ( I need the query for both Sql server and Oracle)
There are Project and Document tables
Project has projectName, projectNo fileds
Document has DocuemntNo, DocRevision, projectNo
The tables join with Project.ProjectNo = Document.ProjectNo
I need to get the ProjectName, DocumentNo, VersionNo
But if versionNo = 0 return Blank as VersionNo else return "\Ver" & VersionNo padded with five zeros
Example result rows are
VersionNo = 0
Proj1, Doc1 , Blank
VersionNo = 1
Proj1,Doc1, \Ver00001
There are Project and Document tables
Project has projectName, projectNo fileds
Document has DocuemntNo, DocRevision, projectNo
The tables join with Project.ProjectNo = Document.ProjectNo
I need to get the ProjectName, DocumentNo, VersionNo
But if versionNo = 0 return Blank as VersionNo else return "\Ver" & VersionNo padded with five zeros
Example result rows are
VersionNo = 0
Proj1, Doc1 , Blank
VersionNo = 1
Proj1,Doc1, \Ver00001
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
CASE and LPAD functions work for both databases. Assuming DocRevision is being tested as VersionNo:
So now the question is, if DocRevision is not being tested, where is VersionNo coming from? Is it a variable? More info is needed in this case.
SELECT
p.ProjectNo
, d.DocumentNo
, CASE d.DocRevision
WHEN 0 THEN NULL
ELSE ('\Ver' + LPAD(d.DocRevision, 5, '0'))
END VersionNo
FROM Project p
LEFT JOIN Document d
ON p.ProjectNo = d.ProjectNo
So now the question is, if DocRevision is not being tested, where is VersionNo coming from? Is it a variable? More info is needed in this case.
using DECODE:
Open in new window
using CASE:
Open in new window