0

How to put alias column name(CO_NUMBER) in where clause. I tried with alias name but its not working, do i need to put whole condition mention in column CO_NUMBER ?

Select  A_TBL.ID, A_TBL.EmpId,
        B_TBL.Tans, 
        DBMS_LOB.SubStr( TXT, 
            DBMS_LOB.InStr(TXT, '"', DBMS_LOB.InStr(TXT, '"CompanyNumber":"') + Length('"CompanyNumber":"'), 1  ) - (DBMS_LOB.InStr(TXT, '"CompanyNumber":"') + Length('"CompanyNumber":"')),
            DBMS_LOB.InStr(TXT, '"CompanyNumber":"') + Length('"CompanyNumber":"')
          ) "COMPANY NUMBER"
From
    A_TBL join B_Tbl on A_TBL.id = B_TBL.id  
Where "COMPANY NUMBER" = 'A1'
VimalSingh
  • 237
  • 2
  • 4
  • 16

1 Answers1

1

To use a named expression in a WHERE clause you need to officially generate it in a subquery. For example:

select *
from ( -- subquery starts here
  select
    ID,
    EmpId,
    DBMS_LOB.SubStr( TXT, 
      DBMS_LOB.InStr(TXT, '"', 
      DBMS_LOB.InStr(TXT, '"CompanyNumber":"') + Length('"CompanyNumber":"'), 
      1  ) - (DBMS_LOB.InStr(TXT, '"CompanyNumber":"') 
      + Length('"CompanyNumber":"')),
      DBMS_LOB.InStr(TXT, '"CompanyNumber":"') +
      Length('"CompanyNumber":"')
    ) as CO_NUMBER
  from A_TBL
) x -- subquery ends here
where CO_NUMBER = 'A1'

Edit -- Adding an alias with extra characters.

You can enclose the alias with double quotes to use it anywhere an alias is valid. For example:

select *
from (
  select a, b, a + b as "A and B" from t
) x
where "A and B" > 6
The Impaler
  • 45,731
  • 9
  • 39
  • 76