Extract Substring with variable start and length - sql

I have a field from a horrible database in which every value is a string such as
Field Stage changed from "Kortec Sales Cycle/3 Proposal" to "Kortec Sales Cycle/Postponed"
Or
Field Stage changed from "Kortec Sales Cycle/5 Evaluate" to "Kortec Sales Cycle/6 Implement"
I need to make 2 calculated columns from this, named 'From' and 'To', with the two values within the quote marks or the number they start with (all 7 stages except 'Postponed' begin with a number).
It's the 'To' field I need the most. I've had a try using Instr(), InStrRev(), Left, and Mid functions, but no joy so far. I've also searched online for similar problems but haven't come across any text manipulation requirements like this.
Anyone know how I should go about breaking this down?
Closest I (think I) got was
SELECT RIGHT([OppMovs (Base)].[DETAILS], InStrRev([OppMovs (Base)].[DETAILS], 'to')-3)
FROM [OppMovs (Base)]

Split can be used to easily extract these:
s = "Field Stage changed from ""Kortec Sales Cycle/3 Proposal"" to ""Kortec Sales Cycle/Postponed""
? s
Field Stage changed from "Kortec Sales Cycle/3 Proposal" to "Kortec Sales Cycle/Postponed"
? Split(Split(s, "from """)(1), """")(0)
Kortec Sales Cycle/3 Proposal
? Split(Split(s, "to """)(1), """")(0)
Kortec Sales Cycle/Postponed
However, for use in a query, you must create small helper functions like:
Public Function GetFrom(ByVal Value As String) As String
GetFrom = Split(Split(Value, "from """)(1), """")(0)
End Function
Public Function GetTo(ByVal Value As String) As String
GetTo = Split(Split(Value, "to """)(1), """")(0)
End Function

Related

How to calculate Employee Service in MS Access and save in Table Fields. (not via query)

I am facing regarding the EmployeeServiceWithUs (Field in table, datatype is Number) in "years".
In TxtServiceWithUs I use the following and Result is OK:
Control Source=DateDiff("yyyy",[DateOfJoing],Date())
But in following:
Private Sub EmployeeServiceWithUs_LostFocus()
[EmployeeServiceWithUs] = [DateOfJoing] & (DateDiff("yyyy", [DateOfJoing], Date))
End Sub
I get:
error "2113 The value you enter is not valid for this field.
Is the able syntax is wrong?
DateDiff calculates the difference in calendar years, not _years of service, or age or similar. You will need a custom function like AgeSimple found here.
Then your ControlSource becomes:
=AgeSimple([DateOfJoing])
To update a (temporary) table:
Update
YourTable
Set
[EmployeeServiceWithUs] = AgeSimple([DateOfJoing])

convert text field to a date/time field in access query not working

I'm working with a access database and vb6. My table has a field named "InvoiceDate" which is a text field. I'm not allowed to make database modifications. So I guess my only option is to change the text field into a date/time field in my query. I found several methods to do so. They are as follows.
Format(InvoiceDate, "yyyy/mm/dd")
(DateSerial(Left(InvoiceDate,4),Mid(InvoiceDate,5,2),Right(InvoiceDate,2))
Between #2015/01/01# And #2016/01/01#))
DateValue(InvoiceDate, "yyyy/mm/dd")
CDate(InvoiceDate, "yyyy/mm/dd")
But those 4 methods didn't work. I can't figure out this.
The query I'm using as follows
SELECT Invoice.InvoiceDate, InvoicedProduct.InvoiceType, Invoice.InvoiceStatus,
Invoice.RetailerID, Invoice.DailySalesID, Invoice.RepID,
InvoicedProduct.Quantity, InvoicedProduct.UnitRate,
InvoicedProduct.TotalItemValue
FROM Invoice
INNER JOIN InvoicedProduct
ON (Invoice.DailySalesID = InvoicedProduct.DailySalesID)
AND (Invoice.RepID = InvoicedProduct.RepID)
AND (Invoice.InvoiceID = InvoicedProduct.InvoiceID)
WHERE (((InvoicedProduct.ProductID)='9010001174130.4')
AND (DateValue(Invoice.InvoiceDate) Between #2015/01/01# And #2016/01/01#))
GROUP BY Invoice.InvoiceDate, InvoicedProduct.InvoiceType, Invoice.InvoiceStatus,
Invoice.RetailerID, Invoice.DailySalesID, Invoice.RepID,
InvoicedProduct.Quantity, InvoicedProduct.UnitRate,
InvoicedProduct.TotalItemValue
HAVING (((InvoicedProduct.InvoiceType)='Invoice' OR (InvoicedProduct.InvoiceType)='Sound')
AND ((Invoice.InvoiceStatus)='VALID'))
ORDER BY Invoice.InvoiceDate;
This gives me the error "Data Type mismatch in criteria expression"
Following two types are include in my InvoiceDate Field
2016/01/04 10:00: AM and 2016/01/20 08:25 PM
The only difference is the colon after the time
Please help.
Thank you.
Your criteria:
DateValue(Invoice.InvoiceDate) Between #2015/01/01# And #2016/01/01#
is correct, so the error message indicates, that one or more of your text dates in InvoiceDate don't represent a valid date, like 2015-06-31 or Null.
Run a query to check this:
Select *, IsDate(InvoiceDate) As ValidDate From Invoice
and see if any of the values of ValidDate are False.
To ignore the extra colon:
DateValue(Replace(Invoice.InvoiceDate, ": ", " ")) Between #2015/01/01# And #2016/01/01#

Displaying SSAS measure in thousands or millions

I have an Olap cube created using Microsoft SSAS. Inside I have a many-to-many relationship between source transaction currency and required "Reporting" currency. This is all functional, however to display a dynamic currency symbol I am using the "Currency" format string default and passing in a custom LCID based on the currency selected.
The problem with using the "Currency" format is the decimal places and large numbers. I am reporting millions of pounds/dollars and my CFO wants to see these numbers reported in thousands or millions. To control this I have read about using a special format string like #,, but this won't allow the currency symbol to be shown.
I had an idea to have a special dimension which would equate to 1, 1000, 1000000 and then create a calculated measure which divides by this (obviously defaulting to 1 and not aggregatable), but I have lots of measures.
Can anybody else advise on an alternative approach?
I would just set the FORMAT_STRING via a script assignment:
FORMAT_STRING(([Dim-Currency].[Currency Code].&[USD])) = "$#,,";
FORMAT_STRING(([Dim-Currency].[Currency Code].&[Euro])) = "€#,,";
You may use the SCOPE statement here:
Scope(AddCalculatedMembers([Measures].Members));
This = case
when [Measures].CurrentMember >= 1000000
then Cstr(Cint([Measures].CurrentMember / 1000000)) + " millions"
when [Measures].CurrentMember >= 1000
then Cstr(Cint([Measures].CurrentMember / 1000)) + " thousands"
else [Measures].CurrentMember
end;
End Scope;
Where Cint return int value and Cstr helps to join int value to text. I'm not sure if it's not too much. I've never used the "Currency" type, honestly.
I'm with #GregGalloway. In our cube-script it is implemented like this:
Scope
([Dim-Currency].[Currency Code].&[EUR]);
//EUR
FORMAT_STRING(This) = '€ #,##0.00';
End Scope;
Scope
([Dim-Currency].[Currency Code].&[GBP]);
FORMAT_STRING(This) = '£ #,##0.00';
End Scope;
We render via Pyramid front-end.

Apex parse error when creating SQL query with sql function

I have the following function:
CREATE OR REPLACE FUNCTION calc_a(BIDoctor number) RETURN number
IS
num_a number;
BEGIN
select count(NAppoint)
into num_a
from Appointment a
where BIDoctor = a.BIDoctor;
RETURN num_a;
END calc_a;
What we want is adding a column to a report that shows us the number of appointments that doc have.
select a.BIdoctor "NUM_ALUNO",
a.NameP "Nome",
a.Address "Local",
a.Salary "salary",
a.Phone "phone",
a.NumberService "Curso",
c.BIdoctor "bi",
calc_media(a.BIdoctor) "consultas"
FROM "#OWNER#"."v_Doctor" a, "#OWNER#"."Appointment" c
WHERE a.BIdoctor = c.BIdoctor;
and we got this when we are writing the region source on apex.
But it shows a parse error, I was looking for this about 2 hours and nothing.
Apex shows me this:
PARSE ERROR ON THE FOLLOWING QUERY
This is probably because of all your double quotes, you seem to have randomly cased everything. Double quotes indicate that you're using quoted identifiers, i.e. the object/column must be created with that exact name - "Hi" is not the same as "hi". Judging by your function get rid of all the double quotes - you don't seem to need them.
More generally don't use quoted identifiers. Ever. They cause far more trouble then they're worth. You'll know when you want to use them in the future, if it ever becomes necessary.
There are a few more problems with your SELECT statement.
You're using implicit joins. Explicit joins were added in SQL-92; it's time to start using them - for your future career where you might interact with other RDBMS if nothing else.
There's absolutely no need for your function; you can use the analytic function, COUNT() instead.
Your aliases are a bit wonky - why does a refer to doctors and c to appointments?
Putting all of this together you get:
select d.bidoctor as num_aluno
, d.namep as nome
, d.address as local
, d.salary as salary
, d.phone as phone
, d.numberservice as curso
, a.bidoctor as bi
, count(nappoint) over (partition by a.bidoctor) as consultas
from #owner#.v_doctor a
join #owner#.appointment c
on d.bidoctor = a.bidoctor;
I'm guessing at what the primary keys of APPOINTMENT and V_DOCTOR are but I'm hoping they're NAPPOINT and BIDOCTOR respectively.
Incidentally, your function will never have returned the correct result because you haven't limited the scope of the parameter in your query; you would have just counted the number of records in APPOINTMENT. When you're naming parameters the same as columns in a table you have to explicitly limit the scope to the parameter in any queries you write, for instance:
select count(nappoint) into num_a
from appointment a
where calc_a.bidoctor = a.bidoctor; -- HERE

How to create if and or parameter statement in Crystal Reports

Apologies for posting a new question but I just can't think how to search for this question.
I'm creating a Crystal Report with multiple parameters and at the moment each one is connected by an ‘AND’ in the Report > Selection Formulas part of the report (not the SQL command part).
I haven’t fully authored the report and it contains lots of arrays to deal with multiple text values and wildcard searches but I think my question should be more around logic than the technical functions.
So…
Parameters are for things like product code, date range, country, batch number etc.
Currently the parameters I’m concerned with are Faults and keyword searches for complaints against products.
(Query 1) If all other parameters are set to default I can enter Fault Combination = ‘Assembly – Code’ and that gives me 17 records.
(Query 2) Entering keyword = ‘%unit%’ gives me 55 records.
The 2 parameters are connected by an AND so if I use Fault Combination = ‘Assembly – Code’ and Keyword = ‘%unit%’ then I get 12 records. If the connect the 2 queries with OR then I get 12 records.
If I compare the unique records, in excel, between query 1 & 2 then there are 60 records with Fault Combination = ‘Assembly – Code’ OR keyword = ‘%unit%.
How can write the parameter formula to get the 60 unique records with one query?
Many thanks!
Gareth
Edit - Code Added
This is the segment i'm concerned with. The arrays are defined earlier in the statement and the '*' & '%' parts of the query below are just to deal with the different wildcard operators between SQL and Crystal. There are a lot of other parameters but these 3 are the only ones that need the OR kind or connection.
Hope that helps!
(IF "%" LIKE array_fn2
THEN ((ISNULL({Command.FaultNoun})=TRUE) OR ({Command.FaultNoun} LIKE '*'))
ELSE IF {Command.RecordType} = 'Complaint'
THEN ({Command.FaultNoun} like array_fn2)
ELSE ((ISNULL({Command.FaultNoun})=TRUE) OR ({Command.FaultNoun} LIKE '*'))) AND
(IF "%" LIKE array_fa2
THEN ((ISNULL({Command.FaultAdjective})=TRUE) OR ({Command.FaultAdjective} LIKE '*'))
ELSE IF {Command.RecordType} = 'Complaint'
THEN ({Command.FaultAdjective} like array_fa2)
ELSE ((ISNULL({Command.FaultAdjective})=TRUE) OR ({Command.FaultAdjective} LIKE '*'))) AND
(IF ("%" LIKE array_k2) OR ({Command.RecordType} = 'Sale')
THEN ((ISNULL({Command.ActualStatements})=TRUE) OR ({Command.ActualStatements} LIKE '*')
OR (ISNULL({Command.ResultsAnalysis})=TRUE) OR ({Command.ResultsAnalysis} LIKE '*')
OR (ISNULL({Command.Observation})= TRUE) OR ({Command.Observation} LIKE '*'))
ELSE
({Command.ActualStatements} like array_k2) OR
({Command.ResultsAnalysis} like array_k2) OR
({Command.Observation} like array_k2))