将两列数据串联到新列中

时间:2019-08-21 11:49:58

标签: sql sql-server

将DesireMinSalary和DesireMaxSalary列数据合并到新列中,即ConcateSalary并从“答案”列中删除货币符号,小数点,然后将值与ConcateSalary比较(如果相同,则返回true,否则返回false

select  UserID_FK, DesireMinSalary,DesireMaxSalary,Answer,
case when answer = CONVERT(VARCHAR(2), DesireMinSalary)  then 'True'else 'False' end as reslut
from TAL_TBL_Candidate
 inner join tal_tbl_users
 on Userid = UserID_FK
  inner join [AocJobs_Migration].[dbo].[AoCJobsJobseekerProfiles]
 on email = susername
 WHERE ProfileQuestion='What is your desired annual salary?'

当前输出:

+-----------+-----------------+-----------------+-------------------+--------+
| UserID_FK | DesireMinSalary | DesireMaxSalary | Answer            | result |
+-----------+-----------------+-----------------+-------------------+--------+
| 823       | 20000           | 24999           | £20,000 - £24,999 | true   |
+-----------+-----------------+-----------------+-------------------+--------+
| 557       | 12300           | 85000           | £30,000 - £40,000 |false   |
+-----------+-----------------+-----------------+-------------------+--------+

预期输出:

+-----------+-----------------+-----------------+--------------+-------------------+--------+
| UserID_FK | DesireMinSalary | DesireMaxSalary | ConcatSalrye | Answer            | result |
+-----------+-----------------+-----------------+--------------+-------------------+--------+
| 823       | 20000           | 24999           | 20000-24999  | £20,000 - £24,999 | true   |
+-----------+-----------------+-----------------+--------------+-------------------+--------+
| 557       | 12300           | 85000           | 12300-85000  | £30,000 - £40,000 | false  |
+-----------+-----------------+-----------------+--------------+-------------------+--------+

2 个答案:

答案 0 :(得分:1)

您可以尝试一下。正如您所说的那样,当前输出已经到来,只需合并两个列值即可添加一列。

select  UserID_FK, DesireMinSalary,DesireMaxSalary, (cast(DesireMinSalary as varchar(15)) + '-' + cast(DesireMaxSalary as varchar(15)) as  ConcatSalrye ,   Answer,
case when answer = CONVERT(VARCHAR(2), DesireMinSalary)  then 'True'else 'False' end as reslut
from TAL_TBL_Candidate
 inner join tal_tbl_users
 on Userid = UserID_FK
  inner join [AocJobs_Migration].[dbo].[AoCJobsJobseekerProfiles]
 on email = susername
 WHERE ProfileQuestion='What is your desired annual salary?'

答案 1 :(得分:1)

您可以使用一些STRING到INT的转换来检查Answer中带有“ DesireMinSalary”和“ DesireMaxSalary”列的最低和最高工资,如下所示-

SELECT
..... 
CASE
    WHEN CAST(REPLACE(SUBSTRING(Answer,2,CHARINDEX(' - ',Answer,0)-2),',','') AS INT) = CAST(DesireMinSalary AS INT)
        AND CAST(REPLACE(SUBSTRING(Answer,(CHARINDEX(' - ',Answer,0))+4,LEN(Answer)),',','') AS INT) = CAST(DesireMaxSalary AS INT) 
        THEN 'True'
    ELSE 'False'
END AS reslut
....