How to check the Year EDT lenght? - variables

I created a Table's Field, and in addition to this, I created my custom EDT, named : MyEDT (for example).
MyEDT is INTEGER type and I have extended the System standard EDT YearBase.
So, if I insert the alphabetic characters (look like "abecjskjfh") I get an error.
But I need to have a rule, I want to insert only value with 4 Number character, I only want values look like : 2000 , 2006, 1982 etc... .
I can check/control this by code, in methods validateWrite or validateField I insered this code :
switch (p1)
{
case fieldNum(MyTable, MyField) :
if (strLen( (strFmt("%1",this.MyField)) ) != 4)
throw error ("Inser only value AAAA");
break;
}
But,It's possible or exist to creato or axtends the YEAR EDT with only 4 number char length ? Or, there is another way to check the length the field valu ?
Thanks all,
enjoy!

If you want to use an integer EDT, I think there is no way to restrict the range of allowed numbers, except the AllowNegative property. So you have to do the validation in code like in your question. But I would suggest to change your validation logic to validate a number range instead of casting the number to a string and then validating the number of characters. This way you could also make sure that users cannot enter a year like 0000.
if (this.MyField< 1900 || this.MyField > 9999)
{
throw error("Please enter a year between 1900 and 9999");
}
Another possibility could be to use a date EDT where you set the DateYear, DateMonth and DateDay properties such that only the year is shown. This would also help with data entry (e.g. 2 gets replaced with 2002) and gives you a nice error dialog if the users enters for example "abc".

You can use X++ match function.
match('<:d:d:d:d>','2004') would match all 4 digit string (0000-9999).
match('<[12]:d:d:d>','2004') would match string 1000-2999.
Alternate way is to use System.Text.RegularExpressions.Regex.IsMatch('1234', '^\d{4}$')

Related

How to extract numbers(Integer) from String field in database and find the maximum

I have an entity. One field named "number" consists of String. It is a number + some text information. For example:
131-MOD
11853-ARO
983-AKK
etc.
My task is: get the maximum of the first number. So, I have to extract Integer value from String "number" and find the maximum from it. For the examples higher, it would be the numbers 131, 11853 and 983. So, the maximum is 11853. I have to get this Integer value as a result.
Here i have my try using Hibernate. But it working with only Integer values. How to extract number, i have no idea.
public Integer getMaxNumber()
{
return (Integer) getSessionFactory().getCurrentSession().createQuery("select max(id) from EmployeeTripCard s").uniqueResult();
}
How can i do that?
You may use the following JPQL query:
SELECT
MAX(CAST(SUBSTRING(id, 1, LOCATE(id, '-') - 1) AS INTEGER))
FROM EmployeeTripCard s;
We can use LOCATE to find the index of the first -, then call SUBSTRING to find the initial number. Note carefully that we also need to cast this resulting string to an integer, in order for MAX to behave the way we want (numbers as text don't always sort the same way as actual pure numbers).

Exclude rows when column contains a 1 in position 2 without using function

I have a column that will always be 5 digits long, and each digit will always be a 1 or a 0. I need to put in my where clause to exclude when the second position is equal to 1. For example 01000 is to be excluded but 10010 is to be kept. I currently have:
WHERE (SUBSTRING(field, 2, 1) <> '1') or field IS NULL
How do do this without using the Substring function?
Edit:Also, the column is a varchar(10) in the database. Does this matter?
You could use the like operator to check that character directly:
WHERE field LIKE '_1%' OR field IS NULL
Use LEFT and RIGHT and then check that is 1 or not as below-
WHERE RIGHT(LEFT(field,2),1) <> '1' OR field IS NULL
No.
If 'field' is of a string type, you need to use string functions to manipulate it. SUBSTRING or some other flavor of it.
You can also convert it to binary and use bitwise AND operator but that won't solve the root issue here.
You are facing the consequences of someone ignoring 1NF.
There is a reason why Codd insisted that every "cell" must be atomic. Your's is not.
Can you separate this bitmap into atomic attribute columns?

How to check remove numbers from a string?

I'm using an SSIS package to bring data through data from one table to another. However, I have a predicament where a field in the table(GroupName) brings through data with numbers at the end. This comes in two forms, either the string will be a name and then a set of numbers less than 4 characters in length. (E.g - Group Name 22)
Or it will come as a name and four numeric characters. (E.g Group Name 2012). Now I'd like to do a check on the data in SQL to see if the length of numeric characters at the end of the string is less than 4. If so, remove the numbers.
Can anyone help
You can use patindex
SELECT
SUBSTRING('Group Name 2012'
,PATINDEX('%[0-9]%'
,'Group Name 2012')
,LEN('Group Name 2012')) as NumberOnly
,LEN( SUBSTRING('Group Name 2012'
,PATINDEX('%[0-9]%'
,'Group Name 2012')
,LEN('Group Name 2012'))) as Numberlength
Alternatively add a Derived column transformation with
NumericCheck= RIGHT(stringvariable,4)
and then in a separate Derived column transformation
(DT_I4)Numeric_check == (DT_I4)Numeric_check ? 1 : 0
Note: You will need to Configure the Error output to "Ignore Failure" for this check. Then have a conditional split which sends the zero values to be updated via an OLE DB Command

DB2 SQL - How can I display nothing instead of a hyphen when the result of my case statement is NULL?

All,
I'm writing a query that includes a CASE statement which compares two datetime fields. If Date B is > Date A, then I'd like the query to display Date B. However, if Date B is not > Date A, then the user who will be getting the report created by the query wants the column to be blank (in other words, not contain the word 'NULL', not contain a hyphen, not contain a low values date). I've been researching this today but have not come up with a viable solution so thought I'd ask here. This is what I have currently:
CASE
WHEN B.DTE_LNP_LAST > A.DTE_PROC_ACT
THEN B.DTE_LNP_LAST
ELSE ?
END AS "DATE OF DISCONNECT"
If I put NULL where the ? is, then I get a hyphen (-) in my query result. If I omit the Else statement, I also get a hyphen in the query result. ' ' doesn't work at all. Does anyone have any thoughts?
Typically the way nulls are displayed is controlled by the client software used to display query results. If you insist on doing that in SQL, you will need to convert the date to a character string:
CASE
WHEN B.DTE_LNP_LAST > A.DTE_PROC_ACT
THEN VARCHAR_FORMAT(B.DTE_LNP_LAST)
ELSE ''
END AS "DATE OF DISCONNECT"
Replace VARCHAR_FORMAT() with the formatting function available in your DB2 version on your platform, if necessary.
You can use the coalesce function
Coalesce (column, 'text')
If the first value is null, it will be replaced by the second one.

SQL Search with parameter that could be date or could be string

I have a search box where you can enter plain text. The keywords are separated by spaces.
I check each keyword if it fits the format for a date and store 'all keywords' in one array and 'date keywords' in another. All the 'date keywords' are also in the 'all keywords' array, as I can not tell if its 'only' a date or maybe also a description or name. I search over many fields like name, description, etc...
I use something like
SELECT * FROM Tbl
WHERE NOT EXISTS(
SELECT * FROM #SearchItems WHERE NOT(
name LIKE SItem OR
description LIKE SItem OR
field3 LIKE SItem)
)
This searches for entries, where no searchitems exist, that are not found -> Means, it shows only items, where all searchitems are found in any field. I use these two 'NOT's because otherwise I would have to check if the count of found items = count of all items.
But now I have a problem with the dates. Because 10/05/05 is a date but could also be a number in the description. So I want to get all items where there is a 10/05/05 in one of the normal fields, or the date matches the specific date.
But if i add somehting like
AND NOT EXISTS(
SELECT * FROM #DateItems WHERE NOT(
date = DItem)
)
It means, that the item needs to have 10/05/05 as string in a field AND as date, but if I use OR, it means that all item with the right day will be shown, regardless of the other keywords. If I delete the 'date-keywords' from the 'all-keywords' it would not find the string in the description.
How to fix this? I want to get all items from the date, or with the date in description when I enter '10/05/05' but if I enter 'blue 10/05/05' blue has to be in one of the fields and 10/05/05 could be in a normal field OR the date.
Edit: I forgot to mention: In the 'all-keywords' array, the dates are in original format like '10/05/05'. The keywords realized as dates are stored in the 'date-keywords' array in ISO format '2005-05-10'. So i cant do something like WHERE DItem = date OR description like DItem.
As this is a bit hard to describe, please ask if anything is unclear.
Thanks for any help.
I found a solution. And because it took me a good while to get to this soltion i will write it down here, so someone will maybe get help from it.
Since a possible date also should be a regular search string, the search fails when this string is not found. The only exception is when the interpreted date of the string matches the date of the item (in which case the string representation does not need to be found somewhere). But to do this you need to know which 'date-keyword' belongs to which 'all-keywords' search string.
If you have this relation, it is possible to accept an item if the string representation is found OR if the date matches.
So now i load the dates with their string representations into a table and have a simple
OR relation between them: WHERE Date = DateItem OR Field = StrItem.