SQL Server 我尝试在T-SQL中同时编写2个函数,以获取创建ContractID值所需的字符串[已关闭]

eqoofvh9  于 2023-02-15  发布在  其他
关注(0)|答案(1)|浏览(97)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

7天前关闭。
Improve this question
我似乎不能让ReplaceLeft函数一起工作,以消除破折号'-'没有错误,我需要两者来实现我正在尝试做的事情。

SELECT DISTINCT 
    Subscriber_Id, REPLACE(LEFT(Subscriber_Id, 3), Subscriber_Id, '-') AS ContractID
FROM 
    EDW_ODS.dbo.ODS_LABCORP_LABS
WHERE 
    ContractId IS NULL

我也尝试过这个,但得到了一个新的列,当我尝试将它们嵌套在一起时,我只是错误。

SELECT DISTINCT 
    Subscriber_Id, 
    LEFT(Subscriber_ID, 3), REPLACE(Subscriber_ID, '-', '') AS ContractID
FROM 
    EDW_ODS.dbo.ODS_LABCORP_LABS
WHERE 
    ContractId IS NULL

gev0vcfq

gev0vcfq1#

如果你只想得到第一个“-”之后的东西,你可以这样做:

select stuff(Subscriber_Id, 1, 3, '') AS ContractIDVersion1
, RIGHT(Subscriber_Id, LEN(Subscriber_Id) - 3) AS ContractIDWithRight
, REPLACE(Subscriber_id, LEFT(Subscriber_Id, 3), '') AS ContractIDByReplace
, CASE WHEN CharIndex('-', Subscriber_id) > 0 THEN SUBSTRING(Subscriber_id, CharIndex('-', Subscriber_id) + 1, LEN(Subscriber_id)) ELSE Subscriber_id END AS ContractIDByCharIndex
FROM 
    EDW_ODS.dbo.ODS_LABCORP_LABS
WHERE 
    ContractId IS NULL

相关问题