How to convert some records ins SQL Server

房东的猫 提交于 2019-12-14 03:29:16

问题


I have a column with values like this:

## codel ##
1-829549-305-117
1-1196585-305-119
119.305.1983984.1 // this record
1-224594-305-121
1-1999987-305-121
122.306.113416.1 // this record
1-158059-305-122
1-1083888-305-126

Code for convert 119.305.1983984.1 to 1.1983984.305.119 is :

DECLARE @myvar varchar(20);  
SET @myvar = '119.305.1983984.1';  

SELECT 
    CONCAT(PARSENAME(@myvar, 1), '-',
           PARSENAME(@myvar, 2), '-',
           PARSENAME(@myvar, 3), '-',
           PARSENAME(@myvar, 4)) 

The output should be:

## codel ##
1-829549-305-117
1-1196585-305-119
1-1983984-305-119 // this record has changed
1-224594-305-121
1-1999987-305-121
1-113416-306-122 // this record has changed
1-158059-305-122

回答1:


You just need to add this code

 SELECT [Codel],
case WHEN  (CHARINDEX('.',[Codel]) > 0) then 

    CONCAT(PARSENAME([Codel], 1), '-',
           PARSENAME([Codel], 2), '-',
           PARSENAME([Codel], 3), '-',
           PARSENAME([Codel], 4))  
           else [Codel] end
            true_template

  FROM [TestDB].[dbo].[CodelTbl]



回答2:


Just replace the - with ., because PARSENAME() works only with '.', not '-'

WITH TBL AS
(
  SELECT       '1-829549-305-117' Str
  UNION SELECT '1-1196585-305-119'
  UNION SELECT '119.305.1983984.1' 
  UNION SELECT '1-224594-305-121'
  UNION SELECT '1-1999987-305-121'
  UNION SELECT '122.306.113416.1' 
  UNION SELECT '1-158059-305-122'
  UNION SELECT '1-1083888-305-126'
),
CTE AS
(
  SELECT Str,
         CASE WHEN CHARINDEX('.', Str) > 0 THEN
                   Str
              ELSE REPLACE(Str, '-', '.')
         END Str1
         ,
         CASE WHEN CHARINDEX('.', Str) > 0 THEN '.' ELSE '-' END Sep
  FROM TBL
)
SELECT Str,
       CONCAT(PARSENAME(Str1,1), Sep,
              PARSENAME(Str1,2), Sep,
              PARSENAME(Str1,3), Sep,
              PARSENAME(Str1,4)
              ) Result

FROM CTE;

Returns:

+-------------------+-------------------+
|        Str        |      Result       |
+-------------------+-------------------+
| 1-1083888-305-126 | 126-305-1083888-1 |
| 1-1196585-305-119 | 119-305-1196585-1 |
| 1-158059-305-122  | 122-305-158059-1  |
| 119.305.1983984.1 | 1.1983984.305.119 |
| 1-1999987-305-121 | 121-305-1999987-1 |
| 122.306.113416.1  | 1.113416.306.122  |
| 1-224594-305-121  | 121-305-224594-1  |
| 1-829549-305-117  | 117-305-829549-1  |
+-------------------+-------------------+

Live Demo


If you have SQL Server 2017, then you can use CONCAT_WS() as

SELECT Str,
       CONCAT_WS(Sep, 
              PARSENAME(Str1,1), 
              PARSENAME(Str1,2), 
              PARSENAME(Str1,3), 
              PARSENAME(Str1,4)
              ) Result

FROM CTE;

If you want the separator to be always '-'then no need to Sep, just directly '-'




回答3:


Here is how you can split your string based on "." and use the values separately:

create table #temp (rn int identity(1,1), num int)

Declare @products varchar(200) = '119.305.1983984.1'
Declare @individual varchar(20) = null

WHILE LEN(@products) > 0
BEGIN
    IF PATINDEX('%.%', @products) > 0
    BEGIN
        SET @individual = SUBSTRING(@products,
                                    0,
                                    PATINDEX('%.%', @products))
        insert into #temp
        SELECT @individual

        SET @products = SUBSTRING(@products,
                                  LEN(@individual + '.') + 1,
                                  LEN(@products))
    END
    ELSE
    BEGIN
        SET @individual = @products
        SET @products = NULL

        insert into #temp
        SELECT @individual
    END
END
select * from #temp

Ref: How do I split a string so I can access item x?



来源:https://stackoverflow.com/questions/54224713/how-to-convert-some-records-ins-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!