Как я могу изменить ПУСТОЙ УКАЗАТЕЛЬ на 0 при получении единственного значения от функции SQL?

Вы можете создать new Date(/*...*/) на основе извлеченных данных из $http.get, например:

$scope.date = new Date('2013', '10', '28'); // for example

В любом случае вы можете увидеть эту демонстрацию в Plunker KBD>.

Надеюсь, это поможет вам.

56
задан Taryn 6 June 2012 в 16:08
поделиться

5 ответов

Most database servers have a COALESCE function, which will return the first argument that is non-null, so the following should do what you want:

SELECT COALESCE(SUM(Price),0) AS TotalPrice
FROM Inventory
WHERE (DateAdded BETWEEN @StartDate AND @EndDate)

[edit]

Just to clarify things since there seems to be a lot of discussion about "COALESCE/ISNULL will still return NULL if no rows match", try this query you can copy-and-paste into SQL Server directly as-is:

SELECT coalesce(SUM(column_id),0) AS TotalPrice 
FROM sys.columns
WHERE (object_id BETWEEN -1 AND -2)

Note that the where clause excludes all the rows from sys.columns from consideration, but the 'sum' operator still results in a single row being returned that is null, which coalesce fixes to be a single row with a 0.

I hope this helps explain it.

98
ответ дан 26 November 2019 в 17:11
поделиться
SELECT ISNULL(SUM(Price), 0) AS TotalPrice 
FROM Inventory
WHERE (DateAdded BETWEEN @StartDate AND @EndDate)

That should do the trick.

14
ответ дан 26 November 2019 в 17:11
поделиться

Edit: Looks like everyone else beat me to it haha

Found the answer.

ISNULL() determines what to do when you have a null value.

In this case my function returns a null value so I needed specify a 0 to be returned instead.

SELECT ISNULL(SUM(Price), 0) AS TotalPrice 
FROM Inventory
WHERE (DateAdded 
BETWEEN @StartDate AND @EndDate)
6
ответ дан 26 November 2019 в 17:11
поделиться
SELECT 0+COALESCE(SUM(Price),0) AS TotalPrice
FROM Inventory
WHERE (DateAdded BETWEEN @StartDate AND @EndDate)
11
ответ дан 26 November 2019 в 17:11
поделиться

You could use

SELECT ISNULL(SUM(ISNULL(Price, 0)), 0).

I'm 99% sure that will work.

4
ответ дан 26 November 2019 в 17:11
поделиться
Другие вопросы по тегам:

Похожие вопросы: