Converting DB2 SQL query to SQL Server causing 'right function requires 2 argument(s)' error

yhqotfr8  于 2023-05-28  发布在  DB2
关注(0)|答案(2)|浏览(183)

I need the T-SQL version of this query. This is in DB2 version and I want to convert this to work in SQL Server.

UPDATE USERTEST
SET LOGIN = '5717-9889-' || LPAD(row_number() over(), 4, '0' ) || '@xxxx.com',
    EMAIL = '5717-9889-' || LPAD(row_number() over(), 4, '0' ) || '@xxxx.com'
WHERE LOGIN IN ('advw@12233318007262',
                    'caeqveewe',
                    'zxy@vdv',
                    'zstclair',
                    'zpasigna',
                    'zoe_tadvadv',
                    'zmadvadvadv',
                    'zadvadvadv',
                    'zielinsm@advadvadv.com',
                    'zhouxinhy',
                    'ZHEJIANG@BBAGGS',
                    'ZHE@avadvadv');

I have tried to write this using the RIGHT function. Then I got this error

The function 'row_number' must have an OVER clause with ORDER BY

Then added the ORDER BY clause as shown here:

LOGIN = '5717-9889-' + RIGHT(ROW_NUMBER() OVER (ORDER BY LOGIN), 4, '0') + '@xxxx.com',
EMAIL = '5717-9889-' + RIGHT(ROW_NUMBER() OVER (ORDER BY EMAIL), 4, '0') + '@xxxx.com'

And after doing this, I got this error:

The right function requires 2 argument(s)

I'm not exactly sure what is the correct approach of writing this in SQL Server.

Any help would be appreciated

Below is the expected results I want in the table

LOGIN                         EMAIL 
-----                         -----
5717-9889-0001@xxxx.com       5717-9889-0001@xxxx.com
5717-9889-0002@xxxx.com       5717-9889-0002@xxxx.com
5717-9889-0003@xxxx.com       5717-9889-0003@xxxx.com
5717-9889-0004@xxxx.com       5717-9889-0004@xxxx.com
5717-9889-0005@xxxx.com       5717-9889-0005@xxxx.com
5717-9889-0006@xxxx.com       5717-9889-0006@xxxx.com
5717-9889-0007@xxxx.com       5717-9889-0007@xxxx.com
liwlm1x9

liwlm1x91#

You can get incremented ids using row_number() , and adding leading zeros to those integers using FORMAT() function :

In this query, I assumed you had a unique column called USERID , but you can order by any column you want.

UPDATE u
SET u.LOGIN = '5717-9889-' + FORMAT(s.rn, '0000' ) + '@xxxx.com',
    u.EMAIL = '5717-9889-' + FORMAT(s.rn, '0000' ) + '@xxxx.com'
FROM USERTEST u
INNER JOIN (
  SELECT LOGIN, row_number() over (order by USERID) AS rn
    FROM USERTEST
    WHERE LOGIN IN ('advw@12233318007262',
                    'caeqveewe',
                    'zxy@vdv',
                    'zstclair',
                    'zpasigna',
                    'zoe_tadvadv',
                    'zmadvadvadv',
                    'zadvadvadv',
                    'zielinsm@advadvadv.com',
                    'zhouxinhy',
                    'ZHEJIANG@BBAGGS',
                    'ZHE@avadvadv')
) as s on s.LOGIN = u.LOGIN

Demo here

ccrfmcuu

ccrfmcuu2#

You want to use the same order by in both locations, and cast the integer to varchar:

use a select to verify first:

WITH CTE AS (
  SELECT LOGIN, ROW_NUMBER() OVER (ORDER BY LOGIN) AS RowNumber
  FROM USERTEST
  WHERE LOGIN IN ('advw@12233318007262', 'caeqveewe', 'zxy@vdv', 'zstclair', 'zpasigna', 'zoe_tadvadv', 
                  'zmadvadvadv', 'zadvadvadv', 'zielinsm@advadvadv.com', 'zhouxinhy', 'ZHEJIANG@BBAGGS', 
                  'ZHE@avadvadv')
)
SELECT
      LOGIN = '5717-9889-' + RIGHT('000' + CAST(CTE.RowNumber AS VARCHAR(4)), 4) + '@xxxx.com'
    , EMAIL = '5717-9889-' + RIGHT('000' + CAST(CTE.RowNumber AS VARCHAR(4)), 4) + '@xxxx.com'
FROM USERTEST U
JOIN CTE ON U.LOGIN = CTE.LOGIN

If OK, then:

WITH CTE AS (
  SELECT LOGIN, ROW_NUMBER() OVER (ORDER BY LOGIN) AS RowNumber
  FROM USERTEST
  WHERE LOGIN IN ('advw@12233318007262', 'caeqveewe', 'zxy@vdv', 'zstclair', 'zpasigna', 'zoe_tadvadv', 
                  'zmadvadvadv', 'zadvadvadv', 'zielinsm@advadvadv.com', 'zhouxinhy', 'ZHEJIANG@BBAGGS', 
                  'ZHE@avadvadv')
)
UPDATE U
SET
     LOGIN = '5717-9889-' + RIGHT('000' + CAST(CTE.RowNumber AS VARCHAR(4)), 4) + '@xxxx.com'
   , EMAIL = '5717-9889-' + RIGHT('000' + CAST(CTE.RowNumber AS VARCHAR(4)), 4) + '@xxxx.com'
FROM USERTEST U
JOIN CTE ON U.LOGIN = CTE.LOGIN
USERIDLOGINEMAIL
11AA
125717-9889-0001@xxxx.com5717-9889-0001@xxxx.com
135717-9889-0002@xxxx.com5717-9889-0002@xxxx.com

fiddle

相关问题