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
2条答案
按热度按时间liwlm1x91#
You can get incremented ids using
row_number()
, and adding leading zeros to those integers usingFORMAT()
function :In this query, I assumed you had a unique column called
USERID
, but you can order by any column you want.Demo here
ccrfmcuu2#
You want to use the same order by in both locations, and cast the integer to varchar:
use a select to verify first:
If OK, then:
fiddle