get number between two string by using substring - mysql

I m trying to get number in between "sims_7009_alaira", i want 7009.
SELECT sno,dbase, SUBSTRING_INDEX(dbase, 'sims_', -1)temp
FROM school
How should i do that in SQL

Give this a try:
select substring_index(SUBSTRING_INDEX(dbase, '_', 2),'_',-1) from school;
Check this here:
SQL Fiddle

Just use substring_index() two times:
SELECT sno, dbase, substring_index(substring_index(dbase, 'sims_', -1), '_alaira', 1) as number FROM school

Do this instead:
SELECT sno,dbase, SUBSTRING_INDEX(SUBSTRING_INDEX(dbase, "_", 2),'_',-1) temp
FROM school;
For more insight see this.

Related

Truncate a column that is the result of CONCAT function?

I'm trying to truncate a column that I have created in my query to display only 15 characters, however I don't know how to. In a table (yes), in a query (no).
How would I write my code to do this? Here's the code:
(SELECT CONCAT(StudLName, ',',UPPER(StudFName)) AS StudentName FROM STUDENT);
Current result:
I think this is what you want:
(SELECT LEFT(CONCAT(StudLName, ',',UPPER(StudFName)),15) AS StudentName FROM STUDENT);
Just wrap the CONCAT function in a LEFT function
An alternative to LEFT, using a cast:
SELECT CAST(CONCAT(StudLName, ',', UPPER(StudFName)) AS CHAR(15)) AS StudentName
FROM STUDENT;

Get last two number in 2018

I want to trim JAN2018 and get last value like this =>JAN18. how I can ?
you can use Trim,concat and SUBSTR function
SELECT SUBSTR("Tutorial", 5, 3) AS ExtractString; it will return "ria"
For your case
SELECT concat( SUBSTR(Trim( ' JAN2018'),1,3), SUBSTR(Trim( ' JAN2018'),-2));
For more knowledge on Concat, Trim and SUBSTR
SELECT SUBSTRING("JAN2018", -2) AS endtwochar; //if you have one char then will return empty
SELECT right("JAN2018", 2) AS endtwochar; // if you have on char then you will one char
Both should give same result - decide based on output
if you need output JAN18 then try below
SELECT insert("JAN2018", 4,2, '') AS removetwochar;
Hope this query may help you:
SELECT RIGHT(columnname,2) FROM tablename;
Here is a demo
You can use SUBSTR()
SELECT SUBSTR('JAN2018',-2);
For your new requirement(an ugly way):
SELECT REPLACE('JAN2018','20','')
Give this a try, this is the query to find last two digit from date
select substr(to_char(sysdate),length(to_char(sysdate))-1,2) last_two from dual

SQL query to display the length and first 3 characters of ename column in emp table

just as the question can we do something to get the length and first 3 characters of the employee name of one column
Please do not mark as answered or duplicate
i have the test tomorrow Advance SQL so I am trying to solve some imp question..
Please answer the problem
thanks again
Hi Shanu, You can use LEN() or LENGTH()(in case of oracle sql) function to get the length of a column.
SELECT LEN(column_name) FROM table_name;
And you can use SUBSTRING or SUBSTR() function go get first three characters of a column.
SUBSTRING( string, start_position, length );
SELECT SUBSTRING( column_name, 1, 3 ) FROM table_name;
To get both together use concatenation operator,
SELECT LEN(column_name)||SUBSTRING( column_name, 1, 3 ) FROM table_name;
Hope you got what you need. Any issues, feel free to ask
We can use SUBSTRING or SUBSTR() function, go get first three characters of a column.
And then try this particular query:
SELECT SUBSTRING(ename,1,3)
FROM emp;
Select len(ename) as Column_Length, left(ename,3) first_three_char from employee; ---------need to code your query. Should not use test format, will be confusing
You can also use substring function instead of left. Query will look like
Select len(ename) as Column_Length,substring(ename,1,3) first_three_char from employee;
SELECT LEN(EMPLOYEE_NAME),LEFT(EMPLOYEE_NAME,3) FROM EMPLOYEE_TABLE;

Finding language in string (doing substring?) with MySQL

I have a table with languages which s_name value looks like this:
'en_UK'
'en_US'
'de_CH'
'de_AT'
I want to get all the distinct languages, without the country part. So for example, in case I just had those of the example, I would need to get:
en
de
What would be the best way of doing so?
I have this right now:
SELECT DISTINCT SUBSTRING(name,1,2)
FROM my_languages
Try this:
SELECT DISTINCT SUBSTRING_INDEX(name, '_', 1) langName
FROM my_languages
OR
SELECT SUBSTRING_INDEX(name, '_', 1) langName
FROM my_languages
GROUP BY langName
Check this link MySQL STRING Functions
Here is a simple way:
select distinct left(s_name, 2)
from t
This assumes the language name is the left two characters.
This will work in Oracle as well as in MySql I think:
SELECT SUBSTR('en_UK',INSTR('en_UK','_')+1) country FROM dual
/

How to search with firstname and last name in - Mysql

Consider I am having tow fields in my table
field_profile_first_name_value field_profile_last_name_value
Victor Brecher
Abhi Jass
select field_profile_first_name_value, field_profile_last_name_value
from content_type_profile where field_profile_first_name_value LIKE '%Victor Bre%' OR
field_profile_last_name_value LIKE '%Victor Bre%'
I am having a auto complete text box.
When i enter the keyword as victor it will fetch the result. But if i give the first name and last name in the same time it will not produce the result.
That is if i give the keyword as Victor Brecher it will not produce the result.
How can i make to get the result if i enter first name and last name ?
How can i make it ?
Try :
select * from contracts
where lower(concat_ws(' ', field_profile_first_name_value, field_profile_last_name_value))
like lower('%Victor Bre%')
Well, even you don't need to use lower too. just use it simply.
select * from contracts where concat_ws(' ', field_profile_first_name_value,field_profile_last_name_value)
like '%Victor Bre%'
I would make use of soundex.
select * from contracts where SOUNDEX(concat_ws(' ', field_profile_first_name_value,field_profile_last_name_value)) = SOUNDEX('Victor Bre');
I suggest to use this code, so the search works also if the search input is the lastname before the name
SELECT
*
FROM
contracts
WHERE
CONCAT(firstname, ' ', lastname)) LIKE 'Victor Bre%'
OR CONCAT(lastname, ' ', firstname)) LIKE 'Victor Bre%';
Moreover, I tested this sql code with and without LOWER(), in mysql 5.7+ the search already do that.