How do I create a Identity starting with a Letter first then numbers after? - sql

I want to create data that contains info about my supplier making it auto generate his ID.
For example, SupplierID I want it to appear as - SID001, SID002 all to auto generate after each other.
How do I do this with SQL?

Ask yourself this: what are the costs of doing this? In particular, what is required to compare two strings versus comparing two numbers? To generate strings from numbers?
Then ask yourself, what value is added by having an id of 'SID0001' rather than just 001?
Then ask yourself, is there an easy way to display a prefix without redundantly storing it for each row? (Answer: yes, with a database view).

You could just use a sequence or identity column and store the prefix in a separate column, or alternately, if the table in question will only ever have suppliers with a prefix of SID, don't store the SID part at all and simply add it at the application level.

You could check for the next ID available and concatenate your prefix with that value. The result should be inserted in the SupplierID column.

Some databases(Oracle, postgres etc) support a sequence for number part. Some(mysql) have an auto increment feature, so you get new number when inserting.
You could then concatenate with string to generate string based IDs.

Related

What's the fastest way to concatenate items from several lines into one field?

In ABAP, what would be the fastest way to concatenate items of the same field from multiple lines into a field of one line?
My program is supposed to report a list of payments, the vendor's ID, and the vendor's email addresses.
Email addresses are stored in table ADR6, one line per address, along with the vendor's ID they belong to.
For the report I would need an internal table populated with vendor IDs (unique key) and concatenated email_addresses, separated by semicolons.
How to populate this internal table?
There is no real magic way to concatenate some fields from a table. Just use something like:
data: email_addresses type string.
loop at [table with addresses] assigning field symbol(<address>).
at first.
email_addresses = <address>-[email field].
continue.
endat.
concatenate email_addresses ';' <address>-[email field] into email_addresses.
endloop.
Only faster method I can think of would involve native SQL.
HANA Solution
Use string_aggr:
SELECT vendor, STRING_AGG(email_address,';')
FROM ADR6
GROUP BY vendor;
HANA Academy has a video demonstrating the use, along with sample code. Note that this solution requires HANA SPS09.

Turn string variables into numeric representatives and store the strings elsewhere?

I don't know the best way to describe my problem and I'm just looking for a push in the right direction, or where to start. I'd be perfectly happy with an answer that's a very useful link or pseudo code.
My problem, I have a database that's about to hit the MS Access hard coded 2 GB database limit and I don't want to split the database.
What I think is a possible solution - make the database more efficient in it's data storage. I think, but don't know if this is true, that I could do this by turning some string fields into numeric fields. Stay with me...
For instance:
My database has several million records of a field we'll call TooLongString
Each value is about 50 characters
Every record has a value for this field
There's only 9 possible values for TooLongString
Would it decrease my database size to instead store a number that
represents one of the 9 possible values and store the text value in a small table? (So go from 50 characters to 1 character several million times)
Did I explain my issue correctly? Is my potential solution actually a solution? How would I go about doing this?
Thanks!
The short answer is yes, that would reduce the size of your database. You could have a second table that holds the nine possible values for "TooLongString" and just store the ID of the appropriate answer in the main table, as you suggested. You would then need to join these tables when pulling the data out in order to retrieve the actual text instead of the ID.
I would set up your new table first, then add a new column for the ID into your existing one. As there are only nine possible values, I'd be tempted to just manually run an UPDATE query nine times, e.g. if the first string in your new table is "MyFirstString" with ID 1, you could run "UPDATE existingTableName SET newColumn = 1 WHERE oldColumn = 'MyFirstString'". Do this for each of the nine values then you can remove the old string column from your table at the end.

Want to find fields in a column that start with certain letters

I'm looking to find a way fields that only start with certain letters
Example: Within the "Postcode" column on the report, I want to show only postcodes that start with "CF"
My best guess would be to create an object that marks these down as a number, but I don't know how to identify them.
Thanks for any advice.
In the case of Webi.
You can create variable, wich helds only symbols you are interested in. In your example it would be:
f=left([Postcode];2)
Then restrict table in the report by filter. And add values you want to filter list.

The best way to store sortable list in PostreSQL and Rails

I have a model A which has many B. So a.bs is a list of B. I want this list to be ordered by user. The common way is special order field in the database and then :order_by => order_field for B. But i don't want to manually manipulate with this field. Is there any special type in PostreSQL? Or maybe there is some extension or i can do this with trigger.
Thanks.
You'll need to store this stuff with some kind of order field, and you'll need to manipulate it manually...
It doesn't necessarily need to contain an integer, btw. For small numbers of values, a float works fine too -- that's what Postgres does in the pg_catalog when you alter an enum field and add a value before an existing one.

Generate unique name everytime like "Windows folder structure" behaviour

I want to create folder structure logic like windows.I am using SQlite database for that. I want to generate a unique name everytime. For e.g. if user enters text with name "New". And if again he enter same name "New" then it should be New(1). if again he enter same name "New" then it should be "New(2)". If user delete "New(1)" entry and enter "New" then "New(1)" should be placed in between "New" and "New(2)".
Could anyone suggest logic for that? Any help is appreciated?
Thanks in advance
Tejas
I consider you will have a database with id and foldername as fields. You just need to retrieve all records from database. In where of query use like operator. Once you get list go in a loop and add add new entry by comparing number. Try this.
Here's the logic I use to do something similar in an app:
Declare an NSString called prefix and an NSInteger called number.
Check if your name has a number on the end of it:
If it does, set number to the value of that number and prefix to the name without the number.
If it does not, set number to 1 and prefix to the name.
Loop from 1 up to some maximum number, doing the following:
Construct a proposedName from base and number:
If number is 1, use the bare prefix.
Otherwise, concatenate the prefix and number in the format you're looking to generate.
Check if proposedName is already in use.*
If it is in use, increment number and loop again.
If it is not in use, proposedName is your new name.
Given that you're using a SQLite database, you can probably speed this up a bit by pre-fetching names that begin with your prefix and loading them into an NSMutableSet. That'll be faster than performing a query for each individual name.
* When you do this test, you should probably exclude the object you're already looking at. This will allow you to try to "uniquify" the name of an existing object without actually changing it.