Need to make Sum IIF query in linq vb.net - vb.net

I am new with the linq and i need to do this query with linq in vb.net from a data table
Every column are setted to VAR CHAR if its useful for the query:
SELECT Sum(IIf([ColumnNameToCountValues] IN
('value1','value2','value3')
And [EmployeId] Like '[IDvalue]'
And [PROJECT] LIKE '[ProjectName]',1,0))
AS Total FROM [DatatableName];
This query should return an integer.

where are your error and what are your question also what have you tried? From what you have said "This query should return an integer." May be your output got error, isn't it?
So, why not just change your database column data type to INT

Related

How to use json array in WHERE IN clause in Postgres

I have a Postgres query like this
SELECT * FROM my_table WHERE status IN (2,1);
This is part of a big query, but I am facing an issue with the WHERE IN part here. I am using this query inside a function and the input parameters are in JSON format. Now the status values I am getting in in the form of a JSON array and it will be like status=[2,1]. I need to use this array in the WHERE clause in the query and not sure how to do that. Currently, I am using like
SELECT * FROM my_table WHERE status IN (array([2,1]));
But this is giving me an error. The status column is of smallint data type. I know this is simple, but I am very much new to Postgres and could not figure out any method to use the JSON array in WHERE IN clause. Any help will be appreciated.

Why The Query Against HashKey returns no records

I am working on a new sql table. The table has a column [varbinary(8000)], where we are storing hash of a certain text. Now, I am trying to retrieve the same record back by using a where clause against the hashkey, but that yields zero records.
I have added a similar query here: http://sqlfiddle.com/#!18/be996/11
Try without the single quotes, like this
SELECT id, description
FROM ForgeRock
where id = 0x94EE059335E587E501CC4BF90613E0814F00A7B08BC7C648FD865A2AF6A22CC2
and you will get the expected result.

Cast and Sum functions

I am writing a macro which pulls data from Oracle and displays in Excel. In Oracle DB we have a custom table with a column Named "Calculated_Quantity". The datatype of this column is BINARY_DOUBLE. However when I write a query in Excel macro to retreive this column, I get the error as "Data Type is not Supported". So I had to use "Cast" function to bypass this error.
Now I need to sum this column. If I write the statement as
Select Id, SUM(CAST(CALCULATED_QUANTITY AS NUMBER(10))) Qty
from DW.SAMPLE
it works fine, but the calculation is wrong.
If I write
Select Id, CAST(SUM(CALCULATED_QUANTITY AS NUMBER(10))) Qty
from DW.SAMPLE
I get an error as missing right parenthesis. The parenthesis seem to be correct. Help please! –
Select Id, CAST(SUM(CALCULATED_QUANTITY) AS NUMBER(10)) Qty
from DW.SAMPLE

VB.NET LINQ to EF Sum Function Error when no records returned

Have seen some solutions for C# but do not know how to solve the issue in VB.NET.
Query:
Dim Query = (From t In myEntities.Bookings
Where(t.Ref = Someid)
Select t.People).Sum()
t.Ref field is an Int and so is t.People.
The SomeId value is the primary key of the related table. This issue is that there will not always be records in the Bookings table with a Ref value of Someid - so the query throws the following error.
I have seen others have got around this problem with catching the error, but from reading up on this and as per the error information it seems there should be a solution (in VB.NET) to cast the query or some of the fields in the query to nullable types?
Error is as follows:
The cast to value type 'Int32' failed because the materialized value is null. Either the result type's generic parameter or the query must use a nullable type.
Your Sum() is dealing with ints, and what you really want is a nullable int.
Dim Query = (From t In myEntities.Bookings Where(t.Ref = Someid) Select t.People).Sum(Function(x) CType(x, Integer?))
See here: Linq query with nullable sum

How to prevent CAST errors on SSIS?

The question
Is it possible to ask SSIS to cast a value and return NULL in case the cast is not allowed instead of throwing an error ?
My environment
I'm using Visual Studio 2005 and Sql Server 2005 on Windows Server 2003.
The general context
Just in case you're curious, here is my use case. I have to store data coming from somewhere in a generic table (key/value structure with history) witch contains some sort of value that can be strings, numbers or dates. The structure is something like this :
table Values {
Id int,
Date datetime, -- for history
Key nvarchar(50) not null,
Value nvarchar(50),
DateValue datetime,
NumberValue numeric(19,9)
}
I want to put the raw value in the Value column and try to put the same value
in the DateValue column when i'm able to cast it to Datetime
in the NumberValue column when i'm able to cast it to a number
Those two typed columns would make all sort of aggregation and manipulation much easier and faster later.
That's it, now you know why i'm asking this strange question.
============
Thanks in advance for your help.
You could also try a Derived Column component and test the value of the potential date/number field or simply cast it and redirect any errors as being the NULL values for these two fields.
(1) If you just simply cast the field every time with a statement like this in the Derived Column component: (DT_DATE)[MYPOTENTIALDATE] - you can redirect the rows that fail this cast and manipulate the data from there.
OR
(2) You can do something like this in the Derived Column component: ISNULL([MYPOTENTIALDATE]) ? '2099-01-01' : (DT_DATE)[MYPOTENTIALDATE]. I generally send through '2099-01-01' when a date is NULL rather than messing with NULL (works better with Cubes, etc).
Of course (2) won't work if the [MYPOTENTIALDATE] field comes through as other things other than a DATETIME or NULL, i.e., sometimes it is a word like "hello".
Those are the options I would explore, good luck!
In dealing with this same sort of thing I found the error handling in SSIS was not specific enough. My approach has been to actually create an errors table, and query a source table where the data is stored as varchar, and log errors to the error table with something like the below. I have one of the below statements for each column, because it was important for me to know which column failed. Then after I log all errors, I do a INSERT where I select those records in SomeInfo that do not have an errors. In your case you could do more advanced things based on the ColumnName in the errors table to insert default values.
INSERT INTO SomeInfoErrors
([SomeInfoId]
,[ColumnName]
,[Message]
,FailedValue)
SELECT
SomeInfoId,
'PeriodStartDate',
'PeriodStartDate must be in the format MM/DD/YYYY',
PeriodStartDate
FROM
SomeInfo
WHERE
ISDATE(PeriodStartDate) = 0 AND [PeriodStartDate] IS NOT NULL;
Tru using a conditional split and have the records where the data is a date go along one path and the other go along a different path where they are updated to nullbefore being inserted.