Replaces Nulls with 0 without using a function? [closed] - sql

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to replace null values in a table but without using a function such as isnull because its dealing with a large amount of data and slowing it down.
everywhere online says isnull and coalesce but is there any way without using such functions.
I need this because the query
OPENING_OTHER + OPENING_FEE + OPENING_INT AS TOTAL_BALANCE
If one value is NULL then the total balance is always null
Cheers

No, how can you do something without doing anything?
You could permanenetly replace the NULL values with 0 but that would waste a lot of storage.
Transforming your data in a SELECT statement is not terribley costly if you use built in functions designed for that purpose.
The use of coalesce will be the quickest, most effiecient and expediant way to do this.
coalesce(OPENING_OTHER, 0) + coalesce(OPENING_FEE, 0) +
coalesce(OPENING_INT, 0) AS TOTAL_BALANCE
In fact, I'd suggest the actual cost of coalesce is so small that its hard to measure.

Related

Declaration of variables improve or decrease performance [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
So I am trying to improve my SQL, one topic that arose, does declaration help with performance or not so like for instance lets say you want to do a if else statement but you need to know if the count is higher than 0 for example right
SELECT #COUNTER = COUNT(ID) FROM tblSome WHERE NUMBER > TOTAL
IF(#COUNTER > 0)
OR would it be better than something like this
IF((SELECT #COUNTER = COUNT(ID) FROM tblSome WHERE NUMBER > TOTAL)>0)
I am just trying to minimize the time it takes, but also it would be nice to know
For now I cannot really find a difference with the small amounts of data I am using and I am not sure how to test it further or go test it to the next level
Use of variables can help or hinder performance dependent on the exact circumstances. There isn't a single rule.
In this case the use of the separate variable assignment step can be harmful.
It gives the optimiser no choice but to count all the rows as it doesn't look ahead to how you use that variable in future statements.
using an IF (SELECT COUNT(*) ...) > 0 allows the optimiser to see that it is true as long as the count is >=1 and can sometimes be optimised to an IF EXISTS (semi join) and stop reading after a single row.
But you are better off just writing it as EXISTS anyway rather than relying on that.
(The discussion on this internet archived blog post has more about the circumstances where this optimisation might happen)

using mid function in bigquery [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a column that has data like this:
TEXT000612
TEXT721
TEXT8
Expected output:
000612
721
8
The column only has a 4 letter text text and its only at the beginning at the cell. But the numbers can vary in length. Also, I want to make sure the numbers are string.
There is no mid function in BQ.
Also, if you dont like my question or think it needs to be improve please give me a chance to improve it before flagging it.
Few options for BigQuery Standard SQL
#standardSQL
SELECT col,
SUBSTR(col, 5),
REGEXP_REPLACE(col, r'^TEXT', '')
FROM `project.dataset.table`
If you just want the numbers, use substr():
substr(col, 5)
You can cast() this to a number if you want.

Find Record in Recordset SQL Server [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am using classic asp to produce a website and am using the below SQL statement, the database is SQL Server 2000.
SELECT * from dbo.PDBproductview where product LIKE '" & partnumbersearch &"%';"
However we now also require forward and back buttons to move forward and backwards by part number - not sure how to achieve this - my initial thought was to run another sql query and somehow get the placement of the part in the part table (product) then to pick out the part before and after it, is it possible to do this ?
You don't say what version of SQL Server you are using. However, if you are using SQL Server 2012 or higher, the LEAD and LAG functions will allow you to achieve what you want to do.
Here is a pretty good article you can use as a guide. Essentially it looks something like this:
SELECT LAG(p.FirstName) OVER (ORDER BY p.BusinessEntityID) PreviousValue,
p.FirstName, LEAD(p.FirstName) OVER (ORDER BY p.BusinessEntityID) NextValue
FROM Person.Person p
With the LEAD and LAG functions, you can indicate how far back or forward you want to look.

Fill with zero to complete a defined number in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I need to complete cards numbers in sql. I have the prefix =11111 and the number of the card which is variable, therefore it could be '25' or '2130' but at the end I must have 14 numbers. So I need to fill spaces with zeros.
I've read about 'LPAD' but I don't understand very well this method.
You could use lpad, but if you're starting with a number you could use a 9-digit format model instead, and concatenate that onto your prefix:
select '11111' || to_char(25, 'FM000000000') from dual;
11111000000025
The FM format modifier stops Oracle adding a space for a potential +/- sign indicator.
SQL Fiddle demo
Use the ZEROFILL attribute.
But your database should only be responsible for saving data and not changing it before saving.
The best way would be to send the zerofilled data to the database server.

Display endless amount of data in sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
If I have an endless amount of data, can I display all of it in sql?
I know there is obviously select *, but then it will never complete.
Is there a command for this?
You can use TOP to select subset of total records
SELECT TOP 100 * from table
This selects top 100 records.
By using Order By clause , you can specify the basis on which subset of records is returned.
Now if you are asking about limits of Sql Server database management system then please see this link - Maximum Capacity Specification of Sql Server
Eg
Max Databases per instance of SQL Server ( both 32 bit and 64 bit ) = 32,767
Usually, you will prefer to use some kind of paging, since you cannot actually show "endless amount of data" in user-friendly way on application.