在 SQL Select 中将列转换为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15299241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert column to string in SQL Select
提问by user1188241
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.
我希望将一列转换为字符串,其中该列是一个选择语句,然后与另一列连接。这就是我在使用 CONVERT 或 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.
我想要只有 4 列,而不是 5。我想要的登录列是 loginCountFailed/LoginCount。例如:3/12。我需要它作为一个字符串,因为你不能除以 0,有时分母是 0。
回答by Serkan Ar?ku?u
For concatanating numbers in MSSQL-2005 you should use CAST
对于 MSSQL-2005 中的连接数字,您应该使用 CAST
CAST(loginsFailed AS VARCHAR(10)) + '/' + CAST(LoginCount AS VARCHAR(10))
loginsFailed and loginCount above is actually your select count distinct
fragments
loginsFailed 和 loginCount 上面实际上是你的select count distinct
片段
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))
回答by Woot4Moo
回答by Praveen Nambiar
You can do the following by using
CAST
orCONVERT
:
您可以使用
CAST
或执行以下操作CONVERT
:
CONVERT(VARCHAR(20), YourIntColumn)
OR
或者
CAST(YourIntColumn AS VARCHAR(20))
回答by Kyle Hale
What you wantto do is have a case statement to handle divide by zero logic, not switch to a string for numeric data.
您想要做的是有一个 case 语句来处理除以零逻辑,而不是切换到数字数据的字符串。
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
回答by PM 77-1
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
回答by Ajo Koshy
I think this will do the trick
我认为这会解决问题
Select column1 + '/' + column2 from table1