我需要如果子查询结果为null,那么它将替换为' - '。我试过这个
REPLACE ( string_expression , string_pattern , string_replacement )
REPLACE((SELECT [StandNo] FROM [dbo].[BusStand] where id=b.[ReturnStand]), char(0), '-')
但没有解决方案,因为我认为它将子查询作为string_expression。
(SELECT isnull([StandNo],'-') FROM [dbo].[BusStand] where id=b.[ReturnStand])
以下的解决方案也无效。
对不起家伙我发现问题不在于子查询b.[ReturnStand]
是空的,因为它选择零行并在结果中放置null。我的查询是这样的。
SELECT b.[Id],
b.[Date],
(SELECT [BusId]
FROM [dbo].[Bus]
WHERE id = [breakDownBusNo]) AS Bus,
(SELECT [RouteNo]
FROM [dbo].[Route]
WHERE id = bl.[routeNo]) AS [Route No],
(SELECT [StandName]
FROM [dbo].[BusStand]
WHERE id = b.[stand]) AS [Breakdown Stand],
b.[DeadKm] AS [Distance From Depo],
(SELECT COALESCE([StandNo], 0)
FROM [dbo].[BusStand]
WHERE id = b.[ReturnStand]) [On Route Stand],
COALESCE([ReturnKm], 0) AS [Distance of on route place],
( b.[DeadKm] + COALESCE([ReturnKm], 0) ) AS Total
FROM [dbo].[BreakDown] AS b
INNER JOIN [dbo].[Bus Log] AS bl
ON b.BusLogId = bl.Id
INNER JOIN [dbo].[DriverAttendance] AS da
ON da.Id = b.DrvrAttnDnceIdLog
请不要进入查询复杂性我只需要显示我在哪里使用它。现在我如何在路线展台上用' - '替换null。
答案 0 :(得分:0)
您的语法错误您需要在string_expression
SELECT REPLACE(isnull([StandNo],0),'0', '-') FROM [dbo].[BusStand]
where id=b.[ReturnStand]
或更好的方式
无需使用Replace
....IN
( SELECT isnull([StandNo],'-') FROM [dbo].[BusStand]
where id=b.[ReturnStand] )
答案 1 :(得分:0)
请尝试:
(SELECT ISNULL([StandNo] , '-') FROM [dbo].[BusStand] where id=b.[ReturnStand])
答案 2 :(得分:0)
使用COALESCE功能
SELECT COALESCE((SELECT [StandNo] FROM [dbo].[BusStand] where id=b.[ReturnStand]) , '-')
FROM [dbo].[tableA] AS b;
答案 3 :(得分:0)
你可以直接这样做
SELECT b.[Id],
b.[Date],
(SELECT [BusId]
FROM [dbo].[Bus]
WHERE id = [breakDownBusNo]) AS Bus,
(SELECT [RouteNo]
FROM [dbo].[Route]
WHERE id = bl.[routeNo]) AS [Route No],
(SELECT [StandName]
FROM [dbo].[BusStand]
WHERE id = b.[stand]) AS [Breakdown Stand],
b.[DeadKm] AS [Distance From Depo],
**** (SELECT isnull([StandNo], '-')
FROM [dbo].[BusStand]
WHERE id = b.[ReturnStand]) [On Route Stand], ****
COALESCE([ReturnKm], 0) AS [Distance of on route place],
( b.[DeadKm] + COALESCE([ReturnKm], 0) ) AS Total
FROM [dbo].[BreakDown] AS b
INNER JOIN [dbo].[Bus Log] AS bl
ON b.BusLogId = bl.Id
INNER JOIN [dbo].[DriverAttendance] AS da
ON da.Id = b.DrvrAttnDnceIdLog