I have the following table and data:
1) i want to show only the last part of sentence.
2) remove any single character from end of sentence.
3) remove and special charachter like -,#,?,_ from end of words
create table t1 (id number(9) , words varchar2(20));
insert into t1 values(1,'hello UK');
insert into t1 values(2,'hello Eypt');
insert into t1 values(3,'hello ALL');
insert into t1 values(4,'hello I');
insert into t1 values(5,'hello USA');
insert into t1 values(6,'hello #');
insert into t1 values(7,'hello #');
insert into t1 values(8,'hello A');
insert into t1 values(9,'hello 20');
insert into t1 values(10,'hello 2-2-2010');
i have used this
select REGEXP_SUBSTR(words,'\S+$)from t1;
expected results
id word
1 UK
2 EGYPT
3 ALL
5 USA
9 20
10 2-2-2010
MySQL version
SELECT id, SUBSTRING_INDEX('hello UK', ' ', -1) as word WHERE LENGHT(word) > 1
OracleDB version (The one you must use)
SELECT id, SUBSTR('hello UK', INSTR('hello UK', ' ')) as word WHERE LENGHT(word) > 1
will return in both cases {id} : UK
Don't forget to replace 'hello UK' with the good column name :)
HERE is the explanation for SUBSTR used with INSTR
Good luck :)
Read your database manual, they all have functions that do string manipulation and they all depend on the application for their syntax. SUBSTRING(words, 7, DATALENGTH(word) -7) would work in SQL Server.
Related
i have first name as 'jose manuel' in my database- its total character length is 11 (combining space)
when i run the above query it is showing me output as 11.
but i need output without space i mean after trimming space ie., 10.
how do i do that in sql.
I'm looking through the comments, and The Impaler is correct.
In this circumstance, using Replace would help you a lot.
I've included testing data into a temporary table.
You can just use the select part without the stuff above that, just change the from part.
Declare #TestData TABLE
(
FIRST_NAME varchar(24)
);
INSERT INTO #TestData (FIRST_NAME) select 'jose manuel'
INSERT INTO #TestData (FIRST_NAME) select 'bob chris'
INSERT INTO #TestData (FIRST_NAME) select 'steven'
select t1.FIRST_NAME,
len(t1.FIRST_NAME) as 'Original_Length',
len( replace(t1.FIRST_NAME, ' ', '') ) as 'New_Length'
from #TestData as t1
The result of this will give you:
FIRST_NAME
Original_length
New_length
jose manuel
11
10
bob chris
9
8
steven
6
6
How to select table's columns using its number? For example:
Select col:1, col:2
From Banks
instead of
Select Id, Name
From Banks
I have problem like this - I have list of selecting queries in database, but first and second columns has different names/aliases:
Id, Name
Name, Description
1 CODE, 'Male' VALUE
...
I want to filter them with a parameter:
Select Id, Name
From Banks
Where lower(Name) like lower(''%' + p_bank_name + '%'')'
So, how can I write this code:
Select col:1, col:2
From Banks
Where lower(col:2) like lower(''%' + p_bank_name + '%'')'
??
Also, I can write sub queries:
Select col:1, col:2
From (
Select Id, Name
From Banks
) r
Where lower(col:2) like lower(''%' + p_bank_name + '%'')'
But, how??
This is not exactly same as what you are trying to do. However, It is almost there. It won't select column by number, however you dont have to specify the explicit column from your real table while writing this query.
As all us suggested, you have to use the dynamic SQL. This is a little idea I created:
create table test1(name1 varchar(10), address1 varchar(10), zipcode1 varchar(10))
insert into test1 values('Test1.1','USA','12344')
insert into test1 values('Test1.2','USA','12344')
insert into test1 values('Test1.3','USA','12344')
insert into test1 values('Test1.4','USA','12344')
create table test2(name2 varchar(10), address2 varchar(10), zipcode2 varchar(10))
insert into test2 values('Test2.1','USA','12344')
insert into test2 values('Test2.2','USA','12344')
insert into test2 values('Test2.3','USA','12344')
insert into test2 values('Test2.4','USA','12344')
You see, the Table name, and the Column name are completely different in both.
Now this sql statement doesn't care about column names :
select * from
(
select '' as T1, '' as T2, '' as T3
union all
select * from test1 --No matter whether it is Id, Name or description
union all
select * from test2 --No matter whether it is Id, Name or description
) as D
where D.T1<>'' -- your other conditions!
Only issue is, since we are using Union, you have to match the number of columns when you specify your empty columns:
select '' as T1, '' as T2, '' as T3, '' as T4, 0 as T5 -- and so on
Here's the output:
How should i write a single sql statement or a stored procedure,
To insert 1000 values in 1000 rows and same column with each column having different values (among those 1000)
Here is the query i wrote,
INSERT INTO a_b values
(
(SELECT max(a_b_id) + 1 from a_b),
1111,
(SELECT s_id FROM a_b WHERE s_id in ('0','1','2','3','4')),
0,
1,
sysdate,
sysdate,
0,
1,
null
);
like say, i have 1000 s_id's i want select them one by one and insert them in one particular column, each time creating a new row.
EX, in first row s_id should be 0 then in second row it should be 1 like that goes on till thousand, attached an image of sample database i am working with.
You can use connect by for this:
INSERT INTO a_b (s_id, col2, col3, ....)
select level, --<< this value will change for every row
1111,
sysdate,
... more columns ...
from dual
connect by level <= 1000;
you can use cross apply to get 1000 rows along with 1000 other columns to insert 1000 rows as below:
insert into a_b (column names...)
select (max(a_b_id) over()) +1 as MaxId, s_id from a_b a cross apply (select 0, 1,SYSDATETIME, SYSDATETIME, 0, 1, null) b where a.s_id('111','222')--condition
The below is a syntax error. You will never get something like that to work.
create table fff
( id int not null
);
insert fff values (select 1,7777,select 3, select 3);
So you need to break it up into chunks
DROP PROCEDURE IF EXISTS uspRunMe;
DELIMITER $$
CREATE PROCEDURE uspRunMe()
BEGIN
insert into a_b select max(a_b_id) + 1 from a_b;
insert into a_b values (1111);
insert into a_b SELECT s_id FROM a_b WHERE s_id in ('0','1','2','3','4');
insert into a_b values (0,1);
insert into a_b select sysdate,sysdate;
insert into a_b values (0,1,null);
END;$$
DELIMITER ;
Test it:
call uspRunMe();
The above is for MySQL. You have a few db engines tagged here.
How do I extract values from a string? I'm trying to separate into 3 new columns. A separate column for city, state and zipcode.
I've tried
select address2,
left(address2, charindex('',address2)-1)
from table
and ---when I try the below code I get "Invalid length parameter passed to the left or substring function"
,LTRIM(substring(a.Address2, CHARINDEX(' ', a.Address2)+1, CHARINDEX(' ', substring(a.address2, charindex(' ',
a.address2)+1, len(a.address2)))-1))
I can break out the city (except for West Warwick) using the following code, but not sure how to make it work for state and zip. This also removes the error.
SUBSTRING(Address2,1,CHARINDEX(' ', a.address2+ ' ')-1) as city
Any ideas what to try?
It looks like your zip codes and your states are all the same length. If that is true, you should be able to use something like this:
SELECT
LEFT(a.Address2,LEN(a.Address2) - 13) AS City,
RIGHT(LEFT(a.Address2,LEN(a.Address2) - 11),2) AS State,
RIGHT(a.Address2,10) AS Zip_Code
FROM
table;
DEMO CODE
Create the table and data:
CREATE TABLE MyTable (Address2 VARCHAR(100));
INSERT INTO MyTable
VALUES
('SAN DIEGO CA 92128-1234'),
('WEST WARWICK RI 02893-1349'),
('RICHMOND IN 47374-9409');
The query:
SELECT
LEFT(Address2,LEN(Address2) - 13) AS City,
RIGHT(LEFT(Address2,LEN(Address2) - 11),2) AS State,
RIGHT(Address2,10) AS Zip_Code
FROM
MyTable;
The output:
Since you only have 3 parts (City/State/Zip) you can take advantage of a function called parsename in SQL Server 2008 and later. (The original intent of the function is to parse out object names.)
Using a combination of the replace and parsename functions will allow you to be able to separate the data into 3 parts, even if the length of the State (not likely) or the Zip (more likely) change.
Example Data:
create table #my_table
(
address2 varchar(75) not null
)
insert into #my_table values ('CONNERSVILLE IN 47331-3351')
insert into #my_table values ('WEST WARWICK RI 02893-1349')
insert into #my_table values ('RICHMOND IN 47374-9409')
insert into #my_table values ('WILLIAMSBURG IN 47393-9617')
insert into #my_table values ('FARMERSVILLE OH 45325-9226')
--this record is an example of a likely scenario for when the zip length would change.
insert into #my_table values ('WILLIAMSBURG IN 47393')
Solution:
with len_vals as
(
select t.address2
, len(parsename(replace(t.address2,' ','.'), 1)) as zip_len
, len(parsename(replace(t.address2,' ','.'), 2)) as st_len
from #my_table as t
group by t.address2
)
select left(a.address2, len(a.address2) - b.zip_len - b.st_len - 2) as city
, substring(a.address2, len(a.address2) - b.zip_len - 2, b.st_len) as st
, right(a.address2, b.zip_len) as zip_code
from #my_table as a
inner join len_vals as b on a.address2 = b.address2
Results:
How can i fetch this query using mysql?
Table1:
id : nos
1 12,13,14
2 14
3 14,12
Table2:
id : values
12 PHP
13 JAVA
14 C++
Now , I want output like this:
1 PHP, JAVA, C++
2 C++
3 C++, PHP
Not tested but it should be something like this:
SELECT table1.id, GROUP_CONCAT(table2.values)
FROM table1 INNER JOIN table2 ON FIND_IN_SET(table2.id, table1.nos)
GROUP BY table1.id
There's no way that I know of to achieve that in SQL. You should instead have a 1 to N relationship to represent those lists. Something like:
Table 1: (just ids)
1
2
3
Table 1.1: (map ids to values in their list)
1, 12
1, 13
1, 14
2, 14
3, 14
3, 12
Not sure if this will work in mySQL but in SqlServer you could create a function:
create function dbo.replaceIdsWithValues
(
#inIds varchar(50)
)
returns varchar(50)
as
begin
declare #ret as varchar(50)
set #ret = #inIds
select #ret = replace(#ret,cast(id as varchar),theValues) from t2
return #ret
end
and then simply call:
select id, nos, dbo.replaceIdsWithValues(nos) from t1
that assuming your tables structure:
create table t1 (id int, nos varchar(50))
create table t2 (id int, theValues varchar(50))
You can test the full example
create table t1 (id int, nos varchar(50))
create table t2 (id int, theValues varchar(50))
insert into t1(id, nos)
select 1, '12,13,14'
union all select 2, '14'
union all select 3, '14,12'
insert into t2(id, theValues)
select 12, 'PHP'
union all select 13, 'JAVA'
union all select 14, 'C++'
select id, nos, dbo.replaceIdsWithValues(nos) from t1
Intended this as comment but it is getting long.
SoulMerge answer(+1) is specific to MySql, which the question was intially intended. Please see the edits for the initial question.
Seems the question again got edited for the MY-SQL, but anyway.
While you can achieve this in MS SQL by using PATINDEX, I am not sure you can do it this in oracle.
I think it would be better to restructure the tables as suggested by jo227o-da-silva(+1).
Although not completely relevant to the subject (MySQL), but will help others finding the question by title, in MSSQL server this can be achived using the FOR XML hint and some nasty string replacements.
I'll post up some code when I find it...