简体   繁体   English

在 AWS redshift 中替换 Function STUFF (SQL Server)

[英]Substitute for Function STUFF (SQL Server) in AWS redshift

I have to replace first 3 digits of a column to a fix first 3 digits (123) Working SQL Server code.我必须将列的前 3 位数字替换为固定的前 3 位数字 (123) 工作 SQL 服务器代码。 (Not working on AWS RedShift) (不适用于 AWS RedShift)

Code:代码:

Select 
Stuff (ColName,1,3,'123')as NewColName
From DataBase.dbo.TableName

eg 1 -Input --- 8010001802000000000209092396---output -1230001802000000000209092396 eg 2 -Input --- 555209092396- --output -123209092396例如 1 -输入 --- 8010001802000000000209092396---输出 -1230001802000000000209092396 例如 2 -输入 --- 555209092396- --输出 -123209092396

it should replace the first 3 digits to 123 irrespective of its length.无论其长度如何,它都应将前 3 位数字替换为 123。

Please advice anything that is supported in AWS Redshift.请告知 AWS Redshift 支持的任何内容。

yet trying using substring and repalce.但尝试使用 substring 和 repalce。

Got this and worked --Using Substring and concat得到这个并工作——使用 Substring 和 concat

Select cast('123'+substring(ColName,4,LEN(ColName)) as numeric (28)) as NewColName From DataBase.dbo.TableName Select cast('123'+substring(ColName,4,LEN(ColName)) as numeric (28)) as NewColName From DataBase.dbo.TableName

I see that AWS RedShift was based on an old version of Postgres, and I looked up the SUBSTRING function for you (https://docs.aws.amazon.com/redshift/latest/dg/r_SUBSTRING.html ), which is pretty forgiving of its argument values.我看到 AWS RedShift 基于旧版本的 Postgres,我为您查找了 SUBSTRING function (https://docs.aws.amazon.com/redshift/latest/dg/r_SUBSTRING.html ),这很漂亮宽容其参数值。

In this sample in Transact-SQL, and as documented for RedShift, the third argument of SUBSTRING can be much longer than the actual strings without causing an error.在 Transact-SQL 中的这个示例中,以及 RedShift 的文档中,SUBSTRING 的第三个参数可以比实际字符串长得多而不会导致错误。 In Transact-SQL, even the second argument is "forgiving" if it starts after the end of the actual string:在 Transact-SQL 中,如果第二个参数在实际字符串结束之后开始,那么即使第二个参数也是“宽容的”:

;
WITH RawData AS
     (SELECT * FROM (VALUES ('8010001802000000000209092396'),
                            ('555209092396'),
                            ('AB')
                    ) AS X(InputString)
     )
     SELECT InputString, '123' + SUBSTRING(InputString, 4, 1000) AS OutputString
            FROM RawData

InputString                     OutputString
8010001802000000000209092396    1230001802000000000209092396
555209092396                    123209092396
AB                              123

As it appears that the concatenation operator in Redshift is ||, I think your expression will be very close to:看起来 Redshift 中的连接运算符是 ||,我认为您的表达式将非常接近:

'123' || SUBSTRING(InputString, 4, 1000) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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