Create leading zero in Oracle - sql

I am using Adempiere which has database Oracle
I have window called Stock Code from table called M_StockCode
The fields are Code and Description.
Currently, Code data type is Number and Description is Varchar2
I want to input Sparepart with Code 01, and Body Repair with Code 02.
As I input the data in Adempiere and save it, what will show is Sparepart with Code 1 (without leading zero)
I've tried putting LPAD function but it's still failed.
How can I put 01 both in Adempiere interface and in database?
Any suggestion will be appreciated :)

A NUMBER cannot have leading zero, a STRING can.
If you want to store the codes with leading zero in the database table, then you must use VARCHAR2 and not NUMBER.
If you want to just display the number with leading zero, then use TO_CHAR to convert the number into string.
For example,
SQL> SELECT TO_CHAR(1, '00') FROM DUAL;
TO_
---
01
You could also use LPAD, but remember, the data type of the result would be a string and not a number.
For example,
SQL> SELECT LPAD(1, 2, '0') FROM DUAL;
LP
--
01

In Adempiere you manage the model via the Application Dictionary, it's the equivalent of the Data Dictionary used by relational databases.
Login to Adempiere as the System Administrator.
Select menu option Application Dictionary->Table & Column.
Select the M_StockCode table and from its window, click on the Columns Tab.
Here, select the Code column and if need be toggle to the Form view
Now, from the drop-down select the Reference value of String and set a Length equal 2.
Finally, hit the Synchronize Column button to have Admepiere modify the database.
If this code is some kind of classification it might be useful to create new Reference type in the Application Dictionary and then you could point your column to it; this would ensure consistency, offering the user a selection to choose from rather than entering values. A Reference can be a List type which is handy for short lists or Table driven which is useful when the classification is more volatile and needs to maintained by the users.

Related

Increment a number by 4, starting with a number I choose

Is it possible, if one does not want to take the AutoValue, to take as default value for a Large Integer, or even normal number, the largest value already used for this from the column and add to it?
I need an increment of 4, starting with a number I choose.
I'm using version 2016 of Office.
I found several possible solutions, which I've entered into the expression generator of the default value at Field Properties:
CODE: SELECT ALL
=Max([MyID])+1
Or
CODE: SELECT ALL
=DomMax("MyID"; "Table name")
I always get the error message:
Unknown function 'DMax' in the validity expression or default value in 'MyID.TableName'
I came across the tip that the references under VBA Editor --> Extras --> References must be correct. Here, however, I haven't the slightest idea which of these I would have to activate, if this is the problem at all.
The PrimaryKey of a table can sometimes be used as a part number in a Parts table or a userID in a Users table but otherwise is a number the user of a database should never see and certainly not manipulate. This is the first reason I suggest solution 2.
Solution 1. In Access just like with any other database I have seen that uses SQL, You can create an AutoNumber that increments by 4 using the Data Definition Language part of SQL. Maybe the Access designers considered changing the increment too rare or too advanced to put in the menu system. However, Altering the increment is not hard just open the query designer and go to the sql tab and type in appropriate SQL.
In Access 2007, how do I change the Auto Increment value?
First, Create your table as normal but don't insert any data. Then open the query designer, and open the sql pane (In this case it doesn't matter what you have in the design view as you are over writing any SQL), type
ALTER TABLE MyTableName
ALTER COLUMN MyAutoNumberColumnName AUTOINCREMENT(1,4);
The sql pane's ribbon should auto select Data Definition Language.
Run the query and solution 1 is done. When you enter Data in the table MyAutoNumberColumnName will start at 1 and increment by 4.
Solution 2: Don't use the primary key. Instead create another variable and display it. This is one example why data should be entered using forms. In the form's BeforeInsert Event calculate the variable and set it's textbox.
Private Sub Form_BeforeInsert(Cancel As Integer)
Me.txtBoxAlternateIDColumn.Value = Nz(DMax("MyAlternateIDColumn", "MyTableName") + 4, 1)
End Sub
'NZ handles the annoying case where the table is completely empty
I've used Solution 2 a couple times and I don't let the person doing data entry even see the alternateID textbox on the data entry form.
You can also use a Create Table statement in the DDL
Here is a youtube example of solution 2: https://www.youtube.com/watch?v=ZOg4P6v5ewA

convert an option value stored in a table field (without a reference table) into a corresponding text

We have a legacy vb6 solution working with an access database with one of the forms containing a number of mutually exclusive option buttons
There is no reference table and the options are saved/loaded using hard coding ie. values in the field optState would be either 0, 1 or 2.
We are building a query in Access to export data into XML and looking for a way to convert the options into text fields without updating the DB or VB application!. So if 0 show some text, 1 some other text ... etc.
Is there a way we can do this in the access query or access sql?
No plans to upgrade the VB or DB so looking for a workaround.
thanks
jay
You could use IIf (Immediate If) to do this in a query. As you have multiple values per field, you would need to nest them. Something like:
SELECT tblStatus.*,
IIf([Status]=0,"Available",IIf([Status]=1,"Sold",IIf([Status]=2,"Withdrawn",""))) AS StatusOut
FROM tblStatus;
This returns an empty string if a value is not 0/1/2. Or you could use Switch in the query:
SELECT tblStatus.*,
Switch([Status]=0,"Available",[Status]=1,"Sold",[Status]=2,"Withdrawn") AS StatusOut
FROM tblStatus;
Regards,

Access 2013 form field value gets cut off on changing the number before the point

Recently I created a form which loads some records from an SQL (linked) database.
I want to display some field values (which are decimal numbers - 30,2 in the sql server).
The values are loaded in the form and displayed with a comma for the decimals and a point as a 1000 separator like this: 5.222,55. (Language settings on the computer).
Though the thing is, when I change the 5 before the point into any number then the value gets truncated and it sees the point as the decimal separator. So for example, if I only select the number 5 on the 5.222,55 number (so I leave the point) and then change it to a 2, the value is changed to 2,22.
Though when I select the whole number or the first number AND the point then it changes correctly. So how can I get this right? The easy way is to just select the whole number on changing but I just want it to work in every way. Perhaps I can achieve it with VBA? I tried setting the format option (back in access 2000 I believe I could set the text field to long integer or currency or something but I cant find this in the access 2013 field properties).
Additional information:
I am linking with a SQL server 2012,
The linked table in Access sees the fields record source (the SQL fields) as short text (while they are decimals in the SQL server)
Access cannot handle a 30,2 decimal, thus it is converted to text by teh ODBC driver.
So, either convert back and forth between text and numerics with Str and Val (the C* functions won't do), or change the data type of the field in SQL Server to, say, Money (= Currency in Access).

Find or Strip Invalid characters from Database

We are using a database where the front end software has allowed the input of invalid characters. (I have no control or re-writing of the software.)
The types of characters are carriage returns, line breaks, �, ¶, basically anything that is not 0-9, a-z or standard punctuation causes us issues with the database and how we use the data.
I'm looking for a way to scan the entire database to identify these invalid codes and either display them as results or strip them out?
I had been looking at This site wondering if there was a way of searching for a certain range? But I might be barking up the wrong tree.
I'm fairly new to SQL so be gentle with me, thanks.
The only way I could think to do this would be to write a stored procedure which uses system tables to get a list of all fields in the database/schema in question. Have it exclude system tables (or only include those that are user defined) then dynamically write out SQL update statements based on the columns/tables found in the system table inquiries. Using regular expressions or character removal like in this article
The system tables in question are:
SELECT
table_name,column_name
FROM
information_schema.columns
Psudo code would be:
Get list of tables we want to do this for
For each table in list
get list of columns for table that have string data.
For each column in table
generate update statement to strip unwanted characters
--Consider writing out table, column key, before after values to history table. incase this
has to be undone.
--Consider counter so I have an idea of what was updated
execute updatestatement
next column
next table
write out counter
Since you say
the data then moves to a second program that cannot handle these
characters and this causes the process to fail.
I'm wondering if you can leave the unreadable data where it is and create a new column for changed data that's only populated if/when the 2nd process fails. You'll still have to test every character of the data in the failed cell, but you wouldn't have to test every character of every row. After you determine the updated text to process, you can call the 2nd process again with the updated value.

Date in a short text data type field... Select query trouble

In my Access database, I have a table called customers. In this table I have a column called DateEntered. The data type for the field is short text.
The values in this column are not coherent - they come in several variations:
MM-DD-YYYY,
MMDDYYYY and
MM/DD/YYYY.
There doesn't seem to be any standard set.
My goal is to select all customers from 2012. I tried
select *
from customers
where DateEntered <('%2013') AND >('%2012');
but it comes up blank when I run it.
Can anyone point out what I'm failing to do correctly & more importantly explain why exactly this query doesn't work in Access? From my understanding of SQL (not very advanced) this should work.
Another variant)
select * from customers where RIGHT(DateEntered, 4) = '2012'
If you have control over the database and application code, the best way to handle this is to use an actual Date field instead of text in the table.
One way to handle this would be to add a new field to the table, write a query or two to correctly convert the text values to actual date values, and populate the new field.
At this point, you would then need to hunt down the application code the refers to this field in any way and adjust to treat the field as a date, not text. This includes your insert and update statements, report code, etc.
Finally, as a last step, I would rename the original text field (or remove it altogether) and rename the new date field to the original field name.
Once you fix the problem, querying against the field will be a piece of cake.
Alternatively, if you can't alter the table and source code, you can use the date conversion function CDATE() to convert the text value to an actual date. Note that you may need to guard against non-date entries (NULL or empty string values, as well as other text values that aren't really dates in the first place). The IsDate() function can be your friend here.
If you have the time and patience, fixing the data and code is the better approach to take, but sometimes this isn't always feasible.
Why don't you use LIKE operators (they're appropriate when you have a pattern using % and _):
select * from customers where DateEntered like '%2013' or DateEntered like '%2012'