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
According to Sql Server, Any computed column can be indexed only when it is deterministic.
ie., consider columns a,b, and c are of INT datatype and c = a+b. Now column C can be indexed but **when column a or b holds the largest Integer value it will throw the Arithmetic Error, is there any solution ?
Thanks.
create table TA (
ID int not null primary key,
a int not null,
b int not null,
c as a+b
)
go
create index IX_TA_c on TA (c)
go
insert into TA(ID,a,b) values (1,1,2147483647)
The insert gets:
Msg 8115, Level 16, State 2, Line 1
Arithmetic overflow error converting expression to data type int.
The statement has been terminated.
If you want the calculation to always work, change the calculation so that at least one of the columns is forced to be a bigint (and so the maths and the resultant type of c are all bigint also):
c as CAST(a as bigint)+b
Related
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 1 year ago.
Improve this question
I want to sum value of col3 based on col1 and col2
here is the link of dummy data sheet in which I have shown sample table according to which I want sum of col 3 please guys check the sheet and suggest me some solution for it.
https://docs.google.com/spreadsheets/d/1_MeiySJHI8OD84BPDOj_z57My-TXbs2ey4AOkQP3zug/edit?usp=sharing
use QUERY:
=QUERY(A:C; "select A,B,sum(C) where A is not null group by A,B label sum(C)''")
update:
={"result"; INDEX(IF(COUNTIFS(A2:A&" "&B2:B, A2:A&" "&B2:B, ROW(A2:A), "<="&ROW(A2:A))=1,
IFNA(VLOOKUP(A2:A&" "&B2:B,
QUERY({A2:A&" "&B2:B, C2:C},
"select Col1,sum(Col2) where Col1 is not null group by Col1 label sum(Col2)''"), 2, 0)), ))}
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 1 year ago.
Improve this question
How do I split a csv string into this format in SQL Server?
Initial String value (A, B, C, D) into :
A-B
B-C
C-D
You can try using string_split in conjunction with lead()
select value + '-' + lead(value) over (order by value) new_value
from string_split('A,B,C,D',',')
SQL FIDDLE:
http://sqlfiddle.com/#!18/0a28f/2607
Grab a copy of NGrams8K then you could simply do this.
DECLARE #string VARCHAR(100) = 'A, B, C, D';
SELECT TheString = CONCAT(ng.Token,'-',ng.Nxt)
FROM
(
SELECT ng.Token, Nxt = LEAD(ng.Token,1) OVER (ORDER BY ng.Position)
FROM dbo.ngrams8k(#string,1) AS ng
WHERE ng.Token LIKE '%[a-z]%'
) AS ng
WHERE ng.Nxt IS NOT NULL;
Returns:
TheString
---------------------
A-B
B-C
C-D
Order is guaranteed without a sort in the execution plan.
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 4 years ago.
Improve this question
I am trying to use ISNULL (), but in the expression part of the function, instead of having a column name as the argument,I have the sum of four columns, and it should return a 0 when all of the value in the column is NULL, else the sum. for e.g., if three columns have values and corresponding value in the other column is Null, it should still return the sum of the three values. This is how I have written my query:
Select ISNULL([FY18 P1]+[FY18 P2]+[FY18 P3]+[FY18 P4],0) as [Previous YTD]
from TableA
This calculated column inside the ISNULL function is not working. Can anybody help me rewrite this expression so that it will work. What i mean when it is not working is that, it is returning a NULL when only one column is NULL but the rest of the columns have a value. Basically it should return the sum and not NULL in this case.
If any column in your concatenation IS NULL then the result will be NULL.
You need to wrap each column in IS NULL to make this column value 0 so that your addition doesn't return NULL.
SELECT ISNULL([FY18 P1],0) +ISNULL([FY18 P2],0) + ISNULL([FY18 P3],0) + ISNULL([FY18 P4],0)
This is because anything + NULL returns NULL
select 1 + 2 + 3 + NULL --returns `NULL`
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 4 years ago.
Improve this question
I have the PEN_TIPO column of a table, this column can have values 0 and 2, and in the report depending on the filter I apply the condition as follows:
declare #PEN_TIPO int = 0
(A.PEN_TIPO = #PEN_TIPO OR #PEN_TIPO = 0)
However, it will have a condition that I do not need to filter this field, ie I have to get 0 and 2 from the PEN_TIPO column.
How can I apply this filter?
It sounds like you're describing, what is commonly referred to as an optional parameter.
If the user enters a parameter value, they want to filter based on that, if not, ignore it altogether.
It would typically look something like this:
DECLARE #PEN_TIPO INT;
(A.PEN_TIPO = #PEN_TIPO OR #PEN_TIPO IS NULL)
OPTION(RECOMPILE);
Please note that I added "OPTION(RECOMPILE)" to the end of the query.
You'll want to add this to you query too, so that the optimizer can create an optimized plan based on the chosen parameter value.
Are you trying to do this ?
DECLARE #PEN_TIPO INT = NULL
SELECT *
FROM TableName
WHERE
A.PEN_TIPO = ISNULL(#PEN_TIPO, PEN_TIPO)
When #PEN_TIPO = NULL then A.PEN_TIPO = A.PEN_TIPO which will bring everything.
This is usually handled with a similar conditional where clause using NULL, but you code would if you defaulted the value to 1 or another value.
In the below proc, we default the parameter to NULL. It will remain NULL if your report / application doesn't pass in a value.
If it remains null, all rows are returned.
If a value is passed in that is 0 or 2, the filter is applied.
If a value is passed in that isn't 0 or 2, an error is raised.
Here's the proc.
create proc myProc (#PEN_TIPO int = NULL)
as
if (#PEN_TIPO IS NOT NULL) or (#PEN_TIPO NOT IN (0,2))
begin
raiserror('Invalid parameter',16,1)
end
SELECT A.*
FROM SomeTable A
WHERE (A.PEN_TIPO = #PEN_TIPO OR #PEN_TIPO IS NULL)
Aaron Bertrand has an in-depth post on these Kitchen Sink type queries.
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 search for number format "abcabc" in SQL Server
Results can be: 324324,567567, ...
I can do it? What is the Solution?
Please help me, thanks!
1. In the simple case where a value is assumed to be stored as an integer and be in the range of [100,000; 999,999], you can just compare the result of Value / 1000 (which would be an integral division, because both operands are integral) with the result of Value % 1000. The query would look like this:
SELECT Value
FROM dbo.atable
WHERE Value / 1000 = Value % 1000
;
2. If a value can be larger than 999,999 and you want to determine if its decimal representation contains a sequence of digits matching the ABCABC pattern at any position, you could first produce a list each item of which is the result of division of the initial value by a power of 10, the power starting from 0 and going on as long as the quotient is equal to or greater than 100,000. To illustrate that by an example, the following list would be produced for the number 123,456,789:
123456789
12345678
1234567
123456
Next, for each item you would find the result of Item % 1000000, which would be a value with the number of digits no more than 6.
Finally, you would apply to the obtained result the test as in the first case, i.e. Result / 1000 = Result % 1000. A value for which such a match could be found would be included into the output.
To code all the above in Transact-SQL, I would employ a numbers table that includes a 0 and use it like this:
SELECT Value
FROM dbo.atable AS t
WHERE EXISTS (
SELECT *
FROM dbo.Numbers AS n
CROSS APPLY (SELECT t.Value / POWER(CAST(10 AS bigint), n.Number)) AS i (Item)
CROSS APPLY (SELECT i.Item % 1000000) AS r (Result)
WHERE n.Number BETWEEN 0 AND 13 -- 13 is enough to cover the range of a bigint
AND i.Item >= 100000
AND r.Result / 1000 = r.Result % 1000
);
Try this
declare #t table (i int)
insert into #t values (123456),(123654),(0),(null)
select i
from #t
where SUBSTRING(cast(i as varchar),1,1)+1=SUBSTRING(CAST(i as varchar),2,1)
and SUBSTRING(cast(i as varchar),2,1)+1=SUBSTRING(CAST(i as varchar),3,1)
and SUBSTRING(cast(i as varchar),4,1)+1=SUBSTRING(CAST(i as varchar),5,1)
and SUBSTRING(cast(i as varchar),5,1)+1=SUBSTRING(CAST(i as varchar),6,1)