Set Null DateTime in InsertQuery - datetime

I have a dataset and it has a InsertQuery(String Name,String Surname,DateTime BDate)
Now I can wirte code like this,
_t.InsertQuer("Alper","AYDIN", null);
it can record data OK,
But I want to do like this,
_t.InsertQuery("Alper","AYDIN", dtBDate.IsEmpty==true?null:dtBDate.Value);
But when I Depoly it is give error like this;
Type of conditional expression cannot be determined because there is no implicit conversion between '' and 'System.DateTime'
How Can I set null ?

The conditional operator needs to be able to return a single data type. Cast the null value to the null version of the other type:
_t.InsertQuery("Alper","AYDIN", dtBDate.IsEmpty?(DateTime?)null:dtBDate.Value);

Have you tried like this:
_t.InsertQuery("Alper","AYDIN", dtBDate);
where dtBDate is Nullable<DateTime>.
Also notice that you cannot pass null if the InsertQuery method takes DateTime instead of Nullable<DateTime> as last parameter.

Related

ibm bpm - execute sql statement return type

how to manage the result of a query that returns an integer "select count(*) from table"?
1) I've tried to bind the output of a SQL Execute Statement service to an integer variable and doesn't work. (type mistmatch)
2) i've tried to use types like 'SQLResult', SQLResultRow, SQLResultColumn as well but they dont work:
Caused by: com.lombardisoftware.core.TeamWorksException: Type ismatch the value "[Element: ]" must be and instance of type atructured IBM BPM Java Class found: org.jdom.Element
3) i've tried to bind the output to a XMLElement variable and i've got this value
< resultSet recordCount=\"1\" columnCount=\"1\">5< /columnn>< /record>< /resultSet>
so now... how can I access the recordCount attribute of this node?
anyway, I don't like so manipulate a variable of XMLType, when are the types SQLResult, SQLResultRow, SQLResultColumn used?
****** EDITED *******
even if i get a result as XMLElement.. i can't manipulate it.
methods like: tw.local.result[0].rows[0].column[0].getText() don't work (the intellisense as well)
the XMLElement as an attribute "recordCount" but i don't know how to get his value..
Anyway, the only workaround that i found is to change the query in order to return a normal set of records(not a scalar value)
select field from table instead of select count(field) from table
so i could to map the output value to a list of objects and than count its length...
ugly and dirty :-(
anyone know how manipulate the XMLElement in a script block?
Please try this.
Bind the output variable from sql execute statement as 'ANY' type.
variable name - result (ANY)
Query - select count(field) as COUNTVAL from table
tw.local.totalCount = tw.local.result[0].rows[0].indexedMap.COUNTVAL;
Use Return type as XMLElement then bind a XMLElement in output mapping.
For eg: If you are using tw.local.output as ouput mapping (of type XMLElement) then,
log.info("Count "+tw.local.output.xpath('/resultSet/record/column').item(0).getText());
This will print the count
If you want to get "recordCount" Attribute then use
tw.local.output.getAttribute("recordCount");

Capture only month and year (or null) - then convert that to a date

I'm currently working with a client that has a VB.NET web application that was developed internally. They've got everything storing to an Access database which they cannot alter or change for their own reasons. I'm not familiar with any of these technologies, so I'm hoping you may have a solution.
The client has a date field that they are only capturing mm/yyyy or blank. They need this information to save to a datetime field in the database. I'm trying to work up a statement that will automatically take the date entered and convert from mm/yyyy to mm/01/yyyy if the date is provided, or 01/01/1970 if the field was left blank. Can anyone assist?
If we are talking about MS Access functions DateSerial is what you are looking for. The basic syntax is below. If the stored value is text you will need to use the Mid function to parse the text into the year and month and you can use use a hard coded 1 for the day.
DateSerial ( year, month, day )
This function can be used in a select or update. Additional logic will be required to provide a default value for the blank result. Typically in Access this type of logic is done with an IIF.
You can use a combination of the IIf,IsNull and CDate functions, like so:
IIf(IsNull([YourDateFField]),#1/1/1970#,CDate([YourDateFField]))
This tests if your field is null and if yes it returns 1/1/1970, if no it will convert your date string to an actual date (e.g. CDate("04/2014") will return 4/1/2014)
This (MSAccess/VBA) function will do what you are asking. If you pass-in a string like mm/yyyy, it will return a datetime like mm/01/yyyy. However, if the string does not fit that pattern (or equiv), the function will return a date time of 1/1/1970, like you asked.
'in MSAccess:
Public Function mmyyyyToDate(mmyyyy As String) As Datetime
If IsDate(Replace(mmyyyy, "/", "/01/")) Then
Return CDate(Replace(mmyyyy, "/", "/01/"))
Else
Return #1/1/1970#
End If
End Function
It would be more efficient to run it in MSAccess, but if you want to run it in VB.net instead, the syntax is different:
'in VB.NET
Public Function mmyyyyToDate(mmyyyy As Object, Optional defaultDate As DateTime = "1/1/1970") As DateTime
Dim re As DateTime
If Convert.IsDbNull(mmyyyy)
return defaultDate
ElseIf DateTime.TryParse(Replace(mmyyyy, "/", "/01/"), re) Then
Return re
Else
Return defaultDate
End If
End Function
Example of running it:
'MSAccess query syntax
INSERT INTO NewDateTable (NewDateColumn)
SELECT mmyyyyToDate(oldColumn) FROM OldTable
If you can't add a new function to the MSAccess DB, you could turn this function into an inline statement (by using an IIF), but it looks pretty ugly:
'MSAccess query syntax
INSERT INTO NewDateTable (NewDateColumn)
SELECT IIF(IsDate(Replace(oldColumn, "/", "/01/")), Replace(mmyyyy, "/", "/01/"), #1/1/1970#)
FROM OldTable

array must be declare error in pl sql

I want to return a array in a function as my function looks like below,
CREATE OR REPLACE FUNCTION TEST
RETURN t_array
IS
strings t_array;
BEGIN
--do something
RETURN strings;
END:
But it gives a error t_array must be declare. I want to know where to declare it and how can i declare it?
When you are using a custom type, you have to declare that type first. For example:
CREATE OR REPLACE TYPE string_array IS TABLE OF varchar2(50);
Which creates a new type named string_array that is a table of varchars.
For more information check the official oracle dokumentation here

LINQ - 'Could not translate expression' with previously used and proven query condition

I am fairly new to LINQ and can't get my head around some inconsistency in behaviour. Any knowledgeable input would be much appreciated. I see similar issues on SO and elsewhere but they don't seem to help.
I have a very simple setup - a company table and an addresses table. Each company can have 0 or more addresses, and if > 0 one must be specified as the main address. I'm trying to handle the cases where there are 0 addresses, using an outer join and altering the select statement accordingly.
Please note I'm currently binding the output straight to a GridView so I would like to keep all processing within the query.
The following DOES work
IQueryable query =
from comp in context.Companies
join addr in context.Addresses on comp.CompanyID equals addr.CompanyID into outer // outer join companies to addresses table to include companies with no address
from addr in outer.DefaultIfEmpty()
where (addr.IsMain == null ? true : addr.IsMain) == true // if a company has no address ensure it is not ruled out by the IsMain condition - default to true if null
select new {
comp.CompanyID,
comp.Name,
AddressID = (addr.AddressID == null ? -1 : addr.AddressID), // use -1 to represent a company that has no addresses
MainAddress = String.Format("{0}, {1}, {2} {3} ({4})", addr.Address1, addr.City, addr.Region, addr.PostalCode, addr.Country)
};
but this displays an empty address in the GridView as ", , ()"
So I updated the MainAddress field to be
MainAddress = (addr.AddressID == null ? "" : String.Format("{0}, {1}, {2} {3} ({4})", addr.Address1, addr.City, addr.Region, addr.PostalCode, addr.Country))
and now I'm getting the Could not translate expression error and a bunch of spewey auto-generated code in the error which means very little to me.
The condition I added to MainAddress is no different to the working condition on AddressID, so can anybody tell me what's going on here?
Any help is greatly appreciated.
The error you are getting is telling you that LinqToSql cannot translate your null check and then string.Format expression into SQL. If you look at the SQL your first query is generating (using either LinqPad or SQL Profiler), you'll see something like:
SELECT [t0].[CompanyID], [t0].[Name],
(CASE
WHEN [t1].[AddressID] IS NULL THEN #p0
ELSE [t1].[AddressID]
END) AS [AddressID],
[t1].[Address1] AS [value],
[t1].[City] AS [value2],
[t1].[Region] AS [value3],
[t1].[PostalCode] AS [value4],
[t1].[Country] AS [value5]
FROM [Company] AS [t0]
LEFT OUTER JOIN [Address] AS [t1] ON [t0].[CompanyID] = [t1].[CompanyID]
WHERE ([t1].[IsMain] IS NULL) OR ([t1].[IsMain] = 1)
For your AddressID field, you can see that it uses a CASE-WHEN to handle the condition when AddressID is null. When you add a CASE-WHEN for MainAddress, it's trying to do the same thing for that field, but there is no SQL equivalent to string.Format it can use for the ELSE clause, so it blows up.
An easy way around this problem is to use a method to format the string. By calling a private method, LinqToSql won't try to translate the string.Format to SQL, and will instead return all of the fields necessary to populate the Address object. The method can then take care of the formatting.
For example:
LINQ:
....
select new {
comp.CompanyID,
comp.Name,
AddressID = (addr.AddressID == null ? -1 : addr.AddressID),
MainAddress = FormatAddress(addr)
};
Method:
private static string FormatAddress(Address addr)
{
return (addr == null ? "" :
string.Format("{0}, {1}, {2} {3} ({4})",
addr.Address1, addr.City,
addr.Region, addr.PostalCode, addr.Country));
}

Pass a NULL in a parameter to a DateTime field in a stored procedure

I have a stored procedure which updates a database using the parameters I supply but I'm having trouble passing a NULL to the stored procedure
The field I need to make NULL is a DateTime field
DB.Parameters.AddWithValue("#date", NULL)
This gives me the error
'NULL' is not declared. 'Null' constant is no longer supported; use 'System.DBNull' instead
So I tried
DB.Parameters.AddWithValue("#date", DBNull.Value.ToString())
But this produces the value 1900-01-01 00:00:00.000 in the column as it's passing a "" to the field
I also tried
DB.Parameters.AddWithValue("#date", DBNull.Value)
But it produces this error
Value of type 'System.DBNull' cannot be converted to 'String'.
Has anybody got any ideas?
Or you can add your parameter like this, which gives it a null value in the database if your variable is null:
DB.Parameters.AddWithValue("#date", myDateTime ?? (object)DBNull.Value);
you need to set it as a nullable type as Amit mentioned.
More details and background available at http://evonet.com.au/overview-of-c-nullable-types/
Try something like this, using Add rather than AddWithValue:
DB.Parameters.Add("#date", SqlDbType.DateTime).Value = DBNull.Value;
Try this
If you are using class and its property and those property values are used as parameter then you can do this
chnage the signature of the property to
public DateTime? myDateTime = null;// Here ? is used to make the property nullable.
Also if your date column is of type varchar then correct it to Date (Sql2008) / DateTime(2005).
//change parameter to this
#SomeDate datetime = null (as suggested by chris)
Allow the field to accept null and then pass the value as
DB.Parameters.Add("#date", DBNull.Value)
This is an example. It's work for me
if(obj.myDate == DateTime.MinValue)
{
aCommand.Parameters.Add("dateParameter", SqlDbType.Date).Value = DBNull.Value;
}
else
{
aCommand.Parameters.Add("dateParameter", SqlDbType.Date).Value = obj.myDate ;
}

Resources