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
Related
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.
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;
I have column contain a string like that :
"https://www.youtube.com/watch?v=R3-kfnKmDVc"
I want to select this column but get string like that "R3-kfnKmDVc".
How do?
If you want the part of the string after the last =, you can do:
select substring_index(col, '=', -1)
In the event that the string may not contain an '=':
select (case when col like '%=%' then substring_index(col, '=', -1) end)
By getting the id only you are not improving the performance, so you can simply fetch it from DB then use regex functions to get the id of the youtube link.
You can use:
SELECT SUBSTRING('https://www.youtube.com/watch?v=R3-kfnKmDVc', (SELECT LOCATE('?v=', 'https://www.youtube.com/watch?v=R3-kfnKmDVc') + 3));
-> usage in select:
select SUBSTRING(url_name, LOCATE('?v=', url_name) + 3) from table;
Thank You
I want to return the 5th character or in a id number.
Ex. I have id number PID-15, I want to return the query after the '-' so in this example 15 then make this a a integer or long integer. How can I achieve this? what particular function that can make this?
I think you can use SUBSTRING_INDEX
SELECT SUBSTRING_INDEX(id,'-',-1) from your_table;
SQLFiddle Demo
Try this code
If you want to get this out of your table using SQL, take a look at the following functions that will help you: SUBSTRING and CHARINDEX. You can use those to trim your entries.
A possible query will look like this (where col is the name of the column that contains your image directories:
SELECT SUBSTRING(col, LEN(SUBSTRING(col, 0, LEN(col) - CHARINDEX ('/', col))) + 1, LEN(col) - LEN(SUBSTRING(col, 0, LEN(col) - CHARINDEX ('/', col))) - LEN(SUBSTRING(col, CHARINDEX ('.', col), LEN(col))));
i have field with content like this: 1, 2, 100 first two numbers can be any size third one can be up to 100.
Now i need to sort my fields by this third number but since i have 2 other numbers i don't know how to do it.
Maybe i could use something like REGEXP or something else?
So far i've tried SUBSTRING but since my two numbers can be any lenght something like
order by SUBSTRING(my_field, 4)
would work but if i have numbers like 32, 451, 30 it takes wrong numbers
Also i use php for to build my query, but i don't think it matters.
You can use SUBSTRING_INDEX. So something like:
SUBSTRING_INDEX(my_field, ',', -1)
EDIT: if you have spaces you might want to do some trimming as well.
ORDER BY SUBSTRING_INDEX(my_field,',',-1)+0 ASC -- or DESC
Use SUBSTRING_INDEX, which grabs the substring up to the nth occurence of the delimiter (in your case, comma), or, in the case of a negative n, it will return the substring after the nth occurence from the end.
To see what I mean try:
SELECT SUBSTRING_INDEX(my_field,',',-1)
FROM my_table
The reason there is a +0 in the ORDER BY is so that mysql sorts these as numbers, not strings.
This should work:
order by CONVERT(SUBSTRING(my_field, LOCATE(",", my_field, RIGHT)), INTEGER)
SELECT *
FROM (
SELECT
SUBSTRING(my_field, 4) AS my_field,
...
FROM
...
WHERE
...
) temp_table
ORDER BY my_field