简体   繁体   中英

Converting SQL Server query to DB2

I am trying to convert the following SQL server query to DB2 or mysql...Can you please help me here..

SELECT 
    *,
    CASE 
        WHEN PATINDEX('%S%LD%', orderstatus) > 0 
            THEN 'Sold'
        WHEN PATINDEX('%STOCK%', orderstatus) > 0 
             OR PATINDEX('%STK%[0-9]/[0-9]%', orderstatus) > 0 
            THEN 'Stock' 
        ELSE'' 
    END AS comment,
    CASE 
        WHEN PATINDEX('%[0-9]/[0-9]%', orderstatus) > 0 
             OR CHARINDEX('*', orderstatus) > 0 
             OR CHARINDEX('BAM', orderstatus) > 0
            THEN 'BAM' 
            ELSE'' 
    END AS BAMYN,
    CASE 
        WHEN PATINDEX('%[0-9]/[0-9]%', orderstatus) > 0 
            THEN CAST(SUBSTRING(orderstatus, CHARINDEX('/', orderstatus) - 2, 5) + '/2022' AS DATE)
            ELSE orddate 
    END AS soldorstockdate 
FROM 
    input

Wondering how CHARINDEX and PATINDEX would be replaced in DB2 or mysql?

Query Tried:

SELECT a.*, CASE WHEN REGEXP_COUNT(ORDERSTATUS,'SLD')>0 THEN 'Sold' 
  WHEN REGEXP_COUNT ('%STOCK%',ORDERSTATUS)>0 OR REGEXP_COUNT ('STK[0-9]/[0-9]',ORDERSTATUS)>0 THEN 'Stock' ELSE'' END AS comment 
         ,CASE WHEN REGEXP_COUNT('[0-9]/[0-9]',ORDERSTATUS)>0 OR LOCATE('*',ORDERSTATUS)>0 OR LOCATE('BAM',ORDERSTATUS)>0
               THEN 'BAM' ELSE'' END AS BAMYN 
         ,CASE WHEN REGEXP_COUNT('[0-9]/[0-9]',ORDERSTATUS)>0 
               THEN CAST(SUBSTRING(ORDERSTATUS,LOCATE('/',ORDERSTATUS)-2,5) AS DATE)
               ELSE ORDDATE  END AS soldorstockdate 


FROM input  a

I am getting the following error (AS 400)

SQL Error [2201S]: [SQ20558] Regular expression string for function REGEXP_COUNT not valid.

Run the following as is.
If this returns wrong result, then provide some data example in the same form and the result desired.

SELECT 
  a.*
, CASE 
    WHEN REGEXP_COUNT (ORDERSTATUS, 'S.*LD') <> 0 
      THEN 'Sold' 
    WHEN REGEXP_COUNT (ORDERSTATUS, 'STOCK|STK[0-9]{2}') <> 0 
      THEN 'Stock' 
    ELSE '' 
  END AS comment 
, CASE 
    WHEN REGEXP_COUNT (ORDERSTATUS, '[0-9]/[0-9]|\*|BAM') <> 0
      THEN 'BAM' 
    ELSE '' 
  END AS BAMYN 
, COALESCE (DATE (TO_DATE (REGEXP_SUBSTR (ORDERSTATUS, '[0-9]{2}/[0-9]{2}') || '/2022', 'MM/DD/YYYY')), CURRENT DATE)  
      AS soldorstockdate 
FROM 
(
VALUES 'SooLD', 'STK12', 'STOCK', '1/2', 'BAM', 'ABC01/31DEF'
) A (ORDERSTATUS)
ORDERSTATUS COMMENT BAMYN SOLDORSTOCKDATE
SooLD Sold 2022-05-27
STK12 Stock 2022-05-27
STOCK Stock 2022-05-27
1/2 BAM 2022-05-27
BAM BAM 2022-05-27
ABC01/31DEF BAM 2022-01-31

In Db2-LUW, the nearest to CHARINDEX may be LOCATE .

You might also use REGEXP_EXTRACT if your Db2-server-platform and Db2-version (which you always need to know, z/os, i-series, linux/unix/windows/cloud) supports that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM