如何在SQL Server 2005中连接两个字符串

时间:2022-03-24 15:52:00

I want to concatenate the words "dummy's" and "dock".

我想把“假人”和“码头”这两个词连接起来。

How can I concatenate them in SQL Server 2005? Does it support double quotes?

如何在SQL Server 2005中连接它们?它支持双引号吗?

6 个解决方案

#1


29  

Try this:

试试这个:

DECLARE @COMBINED_STRINGS AS VARCHAR(50); -- Allocate just enough length for the two strings.

SET @COMBINED_STRINGS = 'rupesh''s' + 'malviya';
SELECT @COMBINED_STRINGS; -- Print your combined strings.

Or you can put your strings into variables. Such that:

或者你可以把字符串放到变量中。这样:

DECLARE @COMBINED_STRINGS AS VARCHAR(50),
        @STRING1 AS VARCHAR(20),
        @STRING2 AS VARCHAR(20);

SET @STRING1 = 'rupesh''s';
SET @STRING2 = 'malviya';
SET @COMBINED_STRINGS = @STRING1 + @STRING2;

SELECT @COMBINED_STRINGS; 

Output:

输出:

rupesh'smalviya

rupesh 'smalviya

Just add a space in your string as a separator.

在字符串中添加一个空格作为分隔符。

#2


14  

so if you have a table with a row like:

如果你有一个表格,比如:

firstname lastname
Bill      smith

you can do something like

你可以做一些类似的事情。

select firstname + ' ' + lastname from thetable

and you will get "Bill Smith"

你会看到"比尔·史密斯"

#3


7  

Try something like

试着像

SELECT 'rupesh''s' + 'malviya'

+ (String Concatenation)

+(字符串连接)

#4


3  

To concatenate two strings in 2008 or prior:

在2008年或之前连接两个字符串:

SELECT ISNULL(FirstName, '') + ' ' + ISNULL(SurName, '')

good to use ISNULL because "String + NULL" will give you a NULL only

使用ISNULL很好,因为“String + NULL”只会给你一个NULL

One more thing: Make sure you are concatenating strings otherwise use a CAST operator:

还有一件事:确保您正在连接字符串,否则使用强制转换操作符:

SELECT 2 + 3 

Will give 5

会给5

SELECT '2' + '3'

Will give 23

将23

#5


2  

DECLARE @COMBINED_STRINGS AS VARCHAR(50),
        @STRING1 AS VARCHAR(20),
        @STRING2 AS VARCHAR(20);

SET @STRING1 = 'rupesh''s';
SET @STRING2 = 'malviya';
SET @COMBINED_STRINGS = @STRING1 + @STRING2;

SELECT @COMBINED_STRINGS;

SELECT '2' + '3';

I typed this in a sql file named TEST.sql and I run it. I got the following out put.

我在一个名为TEST的sql文件中输入了这个。sql和我运行它。我得到了下面的答案。

+-------------------+
| @COMBINED_STRINGS |
+-------------------+
|                 0 |
+-------------------+
1 row in set (0.00 sec)

+-----------+
| '2' + '3' |
+-----------+
|         5 |
+-----------+
1 row in set (0.00 sec)

After looking into this issue a bit more I found the best and sure sort way for string concatenation in SQL is by using CONCAT method. So I made the following changes in the same file.

在进一步研究这个问题之后,我发现SQL中字符串连接的最佳和可靠的排序方式是使用CONCAT方法。所以我在同一个文件中做了如下修改。

#DECLARE @COMBINED_STRINGS AS VARCHAR(50),
 #       @STRING1 AS VARCHAR(20),
 #       @STRING2 AS VARCHAR(20);

SET @STRING1 = 'rupesh''s';
SET @STRING2 = 'malviya';
#SET @COMBINED_STRINGS = @STRING1 + @STRING2;
SET @COMBINED_STRINGS = (SELECT CONCAT(@STRING1, @STRING2));

SELECT @COMBINED_STRINGS;

#SELECT '2' + '3';
SELECT CONCAT('2','3');

and after executing the file this was the output.

在执行文件之后,这是输出。

+-------------------+
| @COMBINED_STRINGS |
+-------------------+
| rupesh'smalviya   |
+-------------------+
1 row in set (0.00 sec)

+-----------------+
| CONCAT('2','3') |
+-----------------+
| 23              |
+-----------------+
1 row in set (0.00 sec)

SQL version I am using is: 14.14

我正在使用的SQL版本是:14.14

#6


0  

I got a easy solution which will select from database table and let you do easily.

我得到了一个简单的解决方案,它将从数据库表中选择并让您轻松地完成。

SELECT b.FirstName + b.LastName FROM tbl_Users b WHERE b.Id='11'

You can easily add a space there if you try

如果你尝试,你可以很容易地在那里添加一个空间。

SELECT b.FirstName +' '+ b.LastName FROM Users b WHERE b.Id='23'

Here you can combine as much as your table have.

在这里,您可以合并尽可能多的您的表。

#1


29  

Try this:

试试这个:

DECLARE @COMBINED_STRINGS AS VARCHAR(50); -- Allocate just enough length for the two strings.

SET @COMBINED_STRINGS = 'rupesh''s' + 'malviya';
SELECT @COMBINED_STRINGS; -- Print your combined strings.

Or you can put your strings into variables. Such that:

或者你可以把字符串放到变量中。这样:

DECLARE @COMBINED_STRINGS AS VARCHAR(50),
        @STRING1 AS VARCHAR(20),
        @STRING2 AS VARCHAR(20);

SET @STRING1 = 'rupesh''s';
SET @STRING2 = 'malviya';
SET @COMBINED_STRINGS = @STRING1 + @STRING2;

SELECT @COMBINED_STRINGS; 

Output:

输出:

rupesh'smalviya

rupesh 'smalviya

Just add a space in your string as a separator.

在字符串中添加一个空格作为分隔符。

#2


14  

so if you have a table with a row like:

如果你有一个表格,比如:

firstname lastname
Bill      smith

you can do something like

你可以做一些类似的事情。

select firstname + ' ' + lastname from thetable

and you will get "Bill Smith"

你会看到"比尔·史密斯"

#3


7  

Try something like

试着像

SELECT 'rupesh''s' + 'malviya'

+ (String Concatenation)

+(字符串连接)

#4


3  

To concatenate two strings in 2008 or prior:

在2008年或之前连接两个字符串:

SELECT ISNULL(FirstName, '') + ' ' + ISNULL(SurName, '')

good to use ISNULL because "String + NULL" will give you a NULL only

使用ISNULL很好,因为“String + NULL”只会给你一个NULL

One more thing: Make sure you are concatenating strings otherwise use a CAST operator:

还有一件事:确保您正在连接字符串,否则使用强制转换操作符:

SELECT 2 + 3 

Will give 5

会给5

SELECT '2' + '3'

Will give 23

将23

#5


2  

DECLARE @COMBINED_STRINGS AS VARCHAR(50),
        @STRING1 AS VARCHAR(20),
        @STRING2 AS VARCHAR(20);

SET @STRING1 = 'rupesh''s';
SET @STRING2 = 'malviya';
SET @COMBINED_STRINGS = @STRING1 + @STRING2;

SELECT @COMBINED_STRINGS;

SELECT '2' + '3';

I typed this in a sql file named TEST.sql and I run it. I got the following out put.

我在一个名为TEST的sql文件中输入了这个。sql和我运行它。我得到了下面的答案。

+-------------------+
| @COMBINED_STRINGS |
+-------------------+
|                 0 |
+-------------------+
1 row in set (0.00 sec)

+-----------+
| '2' + '3' |
+-----------+
|         5 |
+-----------+
1 row in set (0.00 sec)

After looking into this issue a bit more I found the best and sure sort way for string concatenation in SQL is by using CONCAT method. So I made the following changes in the same file.

在进一步研究这个问题之后,我发现SQL中字符串连接的最佳和可靠的排序方式是使用CONCAT方法。所以我在同一个文件中做了如下修改。

#DECLARE @COMBINED_STRINGS AS VARCHAR(50),
 #       @STRING1 AS VARCHAR(20),
 #       @STRING2 AS VARCHAR(20);

SET @STRING1 = 'rupesh''s';
SET @STRING2 = 'malviya';
#SET @COMBINED_STRINGS = @STRING1 + @STRING2;
SET @COMBINED_STRINGS = (SELECT CONCAT(@STRING1, @STRING2));

SELECT @COMBINED_STRINGS;

#SELECT '2' + '3';
SELECT CONCAT('2','3');

and after executing the file this was the output.

在执行文件之后,这是输出。

+-------------------+
| @COMBINED_STRINGS |
+-------------------+
| rupesh'smalviya   |
+-------------------+
1 row in set (0.00 sec)

+-----------------+
| CONCAT('2','3') |
+-----------------+
| 23              |
+-----------------+
1 row in set (0.00 sec)

SQL version I am using is: 14.14

我正在使用的SQL版本是:14.14

#6


0  

I got a easy solution which will select from database table and let you do easily.

我得到了一个简单的解决方案,它将从数据库表中选择并让您轻松地完成。

SELECT b.FirstName + b.LastName FROM tbl_Users b WHERE b.Id='11'

You can easily add a space there if you try

如果你尝试,你可以很容易地在那里添加一个空间。

SELECT b.FirstName +' '+ b.LastName FROM Users b WHERE b.Id='23'

Here you can combine as much as your table have.

在这里,您可以合并尽可能多的您的表。