将两个字符串连接成一个没有重复的字符串

时间:2018-11-21 06:57:58

标签: sql-server sql-server-2008-r2

我有两个字符串:

using UnityEngine;
using System.Collections;

public class Slime: MonoBehaviour
{
    Transform player;        // Ref to the player's position.
    NavMeshAgent nav;        // Ref to the nav mesh agent.

    void Awake ()
    {
        // Set up the references.
        player = GameObject.FindGameObjectWithTag ("Player").transform;
        nav = GetComponent <NavMeshAgent> ();
    }


    void Update ()
    {
        //Here it would be nice to add a stop condition, like when the player is dead or when it is out of range
        ChasePlayer();

    } 

    void ChasePlayer() {
        nav.SetDestination (player.position);
        Debug.Log ("Chasing player");
    }
}

想要将两个字符串连接成一个没有重复的字符串。

预期输出:

float dist = Vector3.Distance(player.position, transform.position)
if(dist < 10.0)
{
        ChasePlayer();
}

1 个答案:

答案 0 :(得分:3)

您可以使用 STRING_SPLIT() 功能和DISTINCT作为

DECLARE @str1 varchar(max) = '[First Name],[Last Name],[Middle Name]';
DECLARE @str2 varchar(max) = '[First Name],[Pin Code],[Address],[Last Name]';

SELECT DISTINCT *
FROM STRING_SPLIT(@Str1 +','+ @Str2, ',');

DECLARE @str1 varchar(max) = '[First Name],[Last Name],[Middle Name]';
DECLARE @str2 varchar(max) = '[First Name],[Pin Code],[Address],[Last Name]';

SELECT DISTINCT *
FROM STRING_SPLIT(CONCAT(@Str1, ',', @Str2), ',');

将其作为一行

declare @result varchar(max) = '';

SELECT @result =  @result + value
FROM STRING_SPLIT(CONCAT(@Str1, ',', @Str2), ',')
group by value;

SELECT @result;

Demo

并且由于您正在使用SQL Server 2008,因此需要创建自己的函数,例如this one here