Find the exact phrase in a string (SQL Server 2008) - sql-server-2008

I receive the file from third party, which I upload to a table. It has a Description column in which I have to find an "Account NO" (9 characters in length).
A few sample descriptions:
FROM AMH-061060-POK UNSECURED OVER 40 DAYS
MAINTENANCE FEE A/C A4G123456
ADJUST BALANCE VS 6PK-123123
REIMBURSEMENT OF CHECK RE-ORDER FEE A4G111111 p11
Asset Mgmt acct gold annual fee MPL NFL234234.
Description column is a free text and doesn't know where the account no appears.
As you see above, it may appear at the end of the description or in the middle somewhere or at the start.
For this AMH-061060-POK, we truncate POK and remove '-' to get 9 length account number.
Any help is appreciated in finding the account number using SQL functions?

Anupama - in the first example, how do you know that the account number is "AMH-061060" and not "UNSECURED"? Both are nine characters.
What is logic in determining which string is the account number? First define it, then you can code it.

Related

Bug in conversion of net/gross prices

In my project the user input for prices can be net or gross, depending on a configuration value. In database we save the net price as decimal(9,2), the VAT and a flag for the price to know if is deductible or not.
For example conversion from gross to net (that most users use):
VAT = 19%
Gross Price (user input)
Net Price (saved in Database)
1500
1260.50
But revert conversion to user input is not the same:
Net Price (saved in Database)
Gross Price (user input)
1260.50
1499.99
Saving the cents in database, will not solve the problem.
Saving the gross price in database is a bad practice and will imply a big data migration when the VAT changes.
Rounding the gross price for user will also not help, because it will generate calculation errors for the total price.
Please help...
Edit:
Another example (database price column is double):
Gross Price (user input)
Net Price (saved in Database)
19500
16386.554621848
Reverted conversion:
Net Price (saved in Database)
Gross Price (user input)
16386.554621848
19499.999999999
Revised answer:
It is not a bug but a roundoff error. You seem to be performing a division operation 1500 / (119 / 100) and storing the result in a fixed point datatype. Unfortunately, like in the above example, the result will contain infinite many digits after decimal (enter the above example on wolframalpha.com to see what I mean). When you use the truncated value to reproduce the original value, it will not match.
Unfortunately, the only thing you can do is to increase the scale of the net column. This will not eliminate the error, but it will decrease its magnitude. Then use the ROUND function for accounting purpose and for recreating the original value.
DB<>Fiddle

How to Split the value and find the longest text string in google sheet

I have a column in google sheet in which each cell contains this type of text
manager, finance manager
accountant
accountant, chief accountant
manager, auditor, other, finance manager
accountant
I want to find the longest text like below and show that text into new cell
finance manager
accountant
chief accountant
finance manager
accountant
I used split function to split the text and find function but finding character or number of character is working but i am unable to understand how i will get whole longest word
Kindly help me.
An alternative would be to use
=FILTER(TRIM(SPLIT(A1, ",")), LEN(SPLIT(A1, ",")) = MAX(LEN(SPLIT(A1, ","))))
and fill down as far as needed.
Suppose your comma-separated lists reside in A2:A. Place the following in, say, B2 of an otherwise empty range B2:B ...
=ArrayFormula(IF(A2:A="",,TRIM(REGEXEXTRACT(A2:A,"[^,]{"&REGEXEXTRACT(TRIM(TRANSPOSE(QUERY(TRANSPOSE(IF(REGEXMATCH(A2:A,"[^,]{"&SEQUENCE(1,30,30,-1)&"}")=FALSE,,SEQUENCE(1,30,30,-1))),,30))),"\S+")*1&"}"))))
This is a complex formula, one that would be difficult to explain. So I will leave it to you (and others who may be interested) to dissect, analyze and understand the inner workings. However, if there is a specific question I can answer should you (or others) get stuck, feel free to ask.
In short, the formula checks to see if there are any REGEX matches for non-comma groupings of 30 characters in length, then 29, 28 and so on to 1. If so, that number is returned in a space-separated list. The first number (which will be the highest) is returned and used to extract a non-comma REGEX expression of that exact length (which then has any leading or trailing spaces removed from it).
if your text is stored in column A, you can use this formula:
=hlookup(max(arrayformula(len(trim(split(A1,","))))), {arrayformula(len(trim(split(A1,","))));arrayformula(trim(split(A1,",")))},2,False)
Second option is to write a custom function.

SSAS Dimension - genuinely repeated data in 1 column, how to deal?

I have a dimension (DimContact) which looks like this:
ContactAlternateKey ContactDOB ContactFullName ContactPostalAddress
12345 17.4.2000 Timo Ohio 17 Blah Street
12346 17.4.2000 Miki Massa 25 Other Street
12347 19.1.1999 Sven Tokio 17 Blah Street
I have another Dim which is used for Addresses based on ParcelID (that's the ID for that actual piece of land) which works as planned. This Dim is populated from free text fields inside the source system, and not as structured as our Parcel data, also, the contactpostaladdress isn't necessarily in our country, so is just there for supporting information, rather than to serve a legitimate purpose.
When I process the Dim WITHOUT the ContactPostalAddress field as an attribute, everything works to plan, however as soon as I use that field too SSAS complains about a duplicate attribute key (which is one of the addresses that has been repeated - but actually the ContactFullName is different (2 people living at the same address), so is genuinely a different record.
Can anyone suggest how to get around this please? I am confused, as surely there are duplicate ContactDOB's in there too which SSAS is happy to work around.
I am using VS2015 on SQL Server 2016 Standard Edition.
Thank you
Moments later I had a lightbulb moment - and have implemented a change in the KeyColumns area of the Properties for the ContactPostalAddress. I have ordered the "Key Columns" with ContactAlternateKey as first on the list and then ContactPostalAddress as second on the list. This forces SSAS to do a distinct(both columns) when building the table, rather than just a distinct(ContactPostalAddress). This works a treat and hopefully will help another user in the future.

Mysql datatype for money

i was trying to create a money related app in which users can choose their currency. Mysql datatype i tried is decimal(19,4). Now the problem is few currencies need three precisions and some need two
Eg:
oman rial needs three precisions. ie 1000 baisa = 1 omani rial. Hence my customers may enter 6.783 omani rial.
Where as my US customers will need only 2 precisions as 100 cents = 1 dollar and they may enter 5.50.
When i insert these two entries to my database using decimal(19,4), it is saved as 6.7830 and 5.5000 respectively.
Now the real pain is when i need to display their entrys as i dont want to display that extra 0 in omani rial entry and that 00 in US dollar. I also tried float but last digit gets rounded off at times.
Is there any mysql data type in which i can save exact entry as it is without any rounding off or extra zeros? If there is no such entry, how can i make it ppssible?
You can use VARCHAR to store exact representations, but I don't recommend that because it takes more bytes to store a number as a string. And any arithmetic you do on the value will convert it to a number anyway.
I recommend you use DECIMAL(19,4), and then format the value in application code, to display it with the appropriate digits. Every programming language has some function like printf() that allows you to control the output formatting, regardless of the value stored.

MS Access Mask Credit card numbers

i have table in MS Access that has a column with credit card information. the credit card information can be in any format anywhere in that string. but i need to render it not usable.
example: my card number 2343 25545 454555 2424 exp:12-12
ie. it can be in any format. any location in the string. beginning or end
i need to detect 4 digit sequence and replace or delete.
How to do that? sql, macro,...?
my final goal is to have scheduled daily job that is going to delete / replace some of the CC digits.
Thanks