11

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
  • 1
    What is the ultimate output you want? A string such as '3/12'? Or the actual result of that division, i.e. 4? Do you only want a string to avoid a divide by zero error? Commented Mar 8, 2013 at 16:57

6 Answers 6

12

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))
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I know to use cast. However, I cannot get the syntax correct when using the sub selects.
This works. I can see where I was making the mistake. I was casting the sub select statment wrong with the alias being in there. This was the confusing part for me.
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

1 Comment

I am familar with CONVERT and CAST when selecting a column, however I do not know how to CONVERT or CAST a sub Select statement and then concat the two.
2

You can do the following by using CAST or CONVERT:

CONVERT(VARCHAR(20), YourIntColumn)

OR

CAST(YourIntColumn AS VARCHAR(20))

Comments

2

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

2 Comments

This looks like it will work if login and loginsfailed where database columns. How would this work using my example were these are aliased column names built from sub selects?
These values are still built using the subselect. It just combines the COUNTs() into a single subselect and then does the division conditionally on having some logins. Did you run it?
0

I think this will do the trick

Select column1 + '/' + column2 from table1

Comments

0

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

1 Comment

@user1188241 - Sorry about that. Could you try now (I changed it a bit) and post the exact error (if it still has problems)?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.