Check number of one column from other column in sql - mysql

I have 2 columns, say dialer_code and country_code. I need to check whether the dialer_code matches with the country_code or not.
Example: dialer_code = 1234567890, so it will check whether 1234567890 in country_code matches any country_code entry. If not then it will remove the last digit (turning the value into 123456789) and check again. This repeats until only the first 2 digit are left.
dialer_code country_code country_name
190095 91 India
190098 44 UK
4456 20 Egypt
445678 1900 US
In the above example, 190095 will be checked in country_code, but we don't have anything matching with it so now it will check for country_code 19009, but we don't have anything matching it either, until we get to 1900. Now it's matching with US. So US has some call rate defined per second so it will return that value.

Assuming the dilaer_code contains country_code, you could use SUBSTRING, like this:
SUBSTRING(dialer_code, CHARINDEX(country_code, dialer_code), 100)

Related

How do I rename a "group by with rollup" column name while maintaining its "NULL" value in super-aggregrate rows

The following SQL statement
SELECT MONTH(start_date) month, name, SUM(pins) pins FROM Table
GROUP BY MONTH(start_date), name WITH ROLLUP
Should display as
1 jim 10
2 jim 15
3 jim 20
NULL jim 45
Instead, it produces the following output:
1 jim 10
2 jim 15
3 jim 20
3 jim 45
As per the MySQL document:
The NULL indicators in each super-aggregate row are produced when the row is sent to the client. The server looks at the columns named in the GROUP BY clause following the leftmost one that has changed value. For any column in the result set with a name that matches any of those names, its value is set to NULL. (If you specify grouping columns by column position, the server identifies which columns to set to NULL by position.)
Apparently, the insertion of "NULL" into a super-aggregate column is completed AFTER the aliasing of fields, and because the "month" column alias in the SELECT field list DOESN'T match the EXACT string used in the "GROUP BY" statement, the value for the "month" column in the super aggregate row is thus considered "undefined" and gets assigned the last value displayed, AND NOT the "NULL" it ideally would have.
I've tried all sorts of "tricks" to work around this and have the column alias created AND keep the super-aggregate value "NULL", have not been able to do so.
Any thoughts or ideas welcome.
Thanks in advance.

Transform part of an identification number into text VBA access

Can someone help me with a little problem. In summary I have a db where you input someones personal details and I have 4 fields I would like VBA to generate automatically. First field is an ID Number field then following that is DateOfBirth and Age fields. They are auto generated from just the ID field. However, to save me time, I want a part of the ID number field to generate whether a person is male or female.
To explain this let me set an example. The ID number is based on South African identification number. It's a 13 digit number e.g. 851205 5205 08 6. The first 6 numbers tells the date of birth (1985/12/05). That part of the number updates my DOB field and then also tells me the persons age with more VBA. Now i want the numbers "5205" to auto generate my gender field whether a person is male or female. Females are assigned numbers in the range 0000-4999 and males from 5000-9999. If you like more detail then please Google 'decoding your South African ID Number'.
So if i should type in that ID number above then it will define the person as a male. Can anyone help me with some VBA?
Thank you
found a solution and it works just the way i want it to:
If (Mid([txtIDnumber].Value, 7, 4) > 4999) Then
Me.cboGender = "MALE"
Else
Me.cboGender = "FEMALE"
End If

format number in specific currency based on another column data

In SSRS
I have a table with column person, column country, and column salary. I want the column salary in a specific currency format based on column country. For example, if column country is United States, corresponding salary should be displayed in $.
Similarly, if the country column shows India, then the salary column should show a rupee, etc.
I am a starter and unable to do this. Please tell me a simple way of doing this
You could return a second dataset with a list of Countries and their Currency symbols such as
Country Currency
USA USD
India RUP
UK GBP
Then you can use LOOKUP to 'look up' the value in the second datasset from the first, so a dataset of
Country Sales
India 404
UK 44
USA 1255
You can then used the lookup as follows
=Lookup(Fields!Country.Value,
Fields!Country.Value,
Fields!Currency.Value,
"DataSetCurrency") <--- Blue <Expr> in the example below
Remember, you can include more than one Placeholder (expression) in a cell, so a design like this
Would render as this (DataSetCurrency included for information only)
Hopefully this will answer your question, though please ask for clarification if required.

my sql query with possible combinations

i have the search requirement that with doing search option for rental house.
city,price range,numberOfRooms are the criteria.
if the user does not mention any of the details and click submit we have to show all the results.
if the user mentioned for example price range is 5000 to 10000 then it has to give the results for considering all cities,all possible 1bhk,2bhk,3bhk...
how to write single query that covers all the possibilities
i have this idea in my mind..
let us consider only city and price are the option and no other options are there for the better explanation.
i create a table called city
city
option cityname
1 delhi
2 hyderabad
3 bangalore
4 delhi
4 hyderabad
4 bangalore
SELECT *
FROM rental
WHERE city IN (select cityname from city where option=#{option_value}) and price>=#{minPrice} and price<=#{maxPrice};
Do we need to create the table city?
Is there any to send the values in list and based on option those list values should come.
is it correct?
Here is another way to do this, by using optional parameters something like:
SELECT *
FROM rental
WHERE 1 = 1
AND (#city IS NULL OR city = #city)
AND (#PricerangeFlag IS NULL OR price BETWEEN #minPrice AND #maxPrice)
..
If any of the parameters #city, #pricerangeFlag or any other parameter passed this way to this query, passed with a NULL value then the whole predicate, for example, AND (#city IS NULL OR city = #city) will be evaluated to true without getting into the second predicate OR city = #city since #city is NULL evaluates to true. The same with other parameters and if all the parameters passed with the value NULL to the query the WHERE clause wil be evaluated to WHERE 1 = 1 which is always true predicate, there for act like it wasn't presented and returns all rows.
Note that in case of the price ranges parameters I used a flag parameter to disable the two parameters #minPrice and #maxPrice

How to prioritize a LIKE query select based on string position in field?

I am attempting to query a table for a limited resultset in order to populate an autocomplete field in javascript. I am, therefore, using a LIKE operator with the partial string entered.
If I have, for example, a table such as:
tblPlaces
id country
1 Balanca
2 Cameroon
3 Canada
4 Cape Verde
5 Denmark
For the sake of this example, let's say I want two rows returning - and yeah, for this example, I made up a country there ;) I want to prioritize any instance where a partial string is matched at the beginning of country. The query I began using, therefore is:
SELECT id, country FROM tblPlaces WHERE country LIKE 'ca%' LIMIT 2
This returned 'Cameroon' and 'Canada' as expected. However, in instances where there are no two names in which the string is matched at the beginning of a word (such as 'de'), I want it to look elsewhere in the word. So I revised the query to become
SELECT id, country FROM tblPlaces WHERE country LIKE '%ca%' LIMIT 2
This then returned 'Cape Verde' and 'Denmark', but in doing so broke my original search for 'ca', which now returns 'Balanca' and 'Cameroon'.
So, my question is, how to go about this using a single query that will prioritize a match at the start of a word (perhaps I need to use REGEXP?) I am assuming also that if the 'country' column is indexed, these matches will at least be returned with subsequent alphabetical priority (i.e. Cameroon before Canada etc).
If you mean to prioritize matches that are Exactly at the start...
SELECT id, country
FROM tblPlaces
WHERE country LIKE '%ca%'
ORDER BY CASE WHEN country LIKE 'ca%' THEN 0 ELSE 1 END, country
LIMIT 2
EDIT
More generic and possibly faster (Assuming "closer to the start the 'better' the match")...
SELECT id, country
FROM tblPlaces
WHERE country LIKE '%ca%'
ORDER BY INSTR(country, 'ca'), country
LIMIT 2