Convert column to string in SQL Select

2020-08-23 08:52发布

问题:

I am looking to convert a column to string where the column is a select statment and then concat with another column. This is where my confusion occurs when using CONVERT or CAST.

Example:

 SELECT employeeID
    ,name
    ,location
    ,(SELECT COUNT(DISTINCT loginsFailed)
     FROM users
     WHERE (users.employeedID = userDetails.employeeID)
        AND (users.startdate = 01-01-2013) as LoginCountFailed
  ,(SELECT COUNT(DISTINCT logins)
   FROM users
   WHERE (users.employeedID = userDetails.employeeID)
       AND (users.startdate = 01-01-2013) as LoginCount
 FROM userDetails

Now, this query works perfect in that is provides the correct number of logins and failed as integers. However, I want to use these integer as a string so i can one column. There is a reason why this needs to be one column as string.

I want to have only 4 columns, not 5. The login column I want to have is loginCountFailed/LoginCount. For example: 3/12. I need it as a string because you cannot divide by a 0 and there are times where the denominator is 0.

回答1:

For concatanating numbers in MSSQL-2005 you should use CAST

CAST(loginsFailed AS VARCHAR(10)) + '/' + CAST(LoginCount AS VARCHAR(10))

loginsFailed and loginCount above is actually your select count distinct fragments

I hope that this works

CAST ((SELECT COUNT(DISTINCT loginsFailed) FROM users WHERE users.employeedID = userDetails.employeeID AND users.startdate = 01-01-2013) AS VARCHAR(10))
+ '/' +
CAST ((SELECT COUNT(DISTINCT logins) FROM users WHERE users.employeedID = userDetails.employeeID AND users.startdate = 01-01-2013) AS VARCHAR(10))


回答2:

DECLARE @i int
SET @i=98235

--Method 1 : Use CAST function
SELECT CAST(@i as varchar(10))

--Method 2 : Use CONVERT function
SELECT CONVERT(varchar(10),@i)

--Method 3 : Use STR function
SELECT LTRIM(STR(@i,10))

Source



回答3:

You can do the following by using CAST or CONVERT:

CONVERT(VARCHAR(20), YourIntColumn)

OR

CAST(YourIntColumn AS VARCHAR(20))


回答4:

What you want to do is have a case statement to handle divide by zero logic, not switch to a string for numeric data.

SELECT employeeID
    ,name
    ,location
    ,(SELECT CASE WHEN COUNT(DISTINCT logins) = 0 then 0 ELSE
COUNT(DISTINCT loginsFailed) / COUNT(DISTINCT logins)
END
     FROM users
     WHERE (users.employeedID = userDetails.employeeID)
        AND (users.startdate = 01-01-2013) ) as LoginFailRatio
 FROM userDetails


回答5:

I think this will do the trick

Select column1 + '/' + column2 from table1


回答6:

I think you can just create an outer query here:

SELECT u.employeeID, u.name, u.location,  
       CONVERT(varchar(10), u.LoginCountFailed) + '/' + CONVERT(varchar(10), u.LoginCount) "Ratio" 
    FROM
        (
         SELECT employeeID
            ,name
            ,location
            ,(SELECT COUNT(DISTINCT loginsFailed)
             FROM users
             WHERE (users.employeedID = userDetails.employeeID)
                AND (users.startdate = 01-01-2013) as LoginCountFailed
          ,(SELECT COUNT(DISTINCT logins)
           FROM users
           WHERE (users.employeedID = userDetails.employeeID)
               AND (users.startdate = 01-01-2013) as LoginCount
         FROM userDetails
        ) u