Extracting numeric part from string - SYBASE ASE - sybase-ase

I am trying to extract the 7658 from string XYZ.ABC7658#YAHOO.CO.IN.
Below is the query which I am trying and the output is also shown. However the query doesn't server the purpose.
Can you please help on this?
(Running Query on Sybase ASE)
SELECT SUBSTRING('XYZ.ABC7658#YAHOO.CO.IN', PATINDEX('%[0-9]%', 'XYZ.ABC7658#YAHOO.CO.IN'),
LEN('XYZ.ABC7658#YAHOO.CO.IN')) as number
Go
Number
--------------
85#YAHOO.CO.IN

You did not mention the end poistion
DECLARE #str VARCHAR(100)
SELECT #str='XYZ.ABC7658#YAHOO.CO.IN'
SELECT SUBSTRING(#str, PATINDEX('%[^0-9][0-9]%', #str) + 1, PATINDEX('%[0-9][^0-9]%', #str) - PATINDEX('%[^0-9][0-9]%', #str))

Related

I want to store more than 4000 character in oracle 11g

I am working with Informatica mapping which updates a column in oracle table.
The column has a datatype of VARCHAR2 (4000).
Now I want to update the column with a value more than 4000 characters,i tried the datatypes LONG/CLOB but then also it is being able to accommodate only up to 4000 characters.
I also tried to to update the column using manual sql query but it is giving error.
Please advise the best way to store more than 4000 characters in the column.
Thanks in advance.
Rahul
You can change the column to LONG or LOB columns (CLOB, NCLOB, BLOB), then you can do:
declare v LONG;
begin
v := 'stringwithmorethan4000characters...';
update YOUR_TABLE set COLUMN_X = v where id=10;--use your own command
end;

Using SQL Server, is there a way to query (555) 344-2524 using 5553442524?

Using the PHP SQLSRV driver to connect to SQL Server 2000, is there a way I could match all of these rows using this piece of data: 5553442524?
555-344-2524
(555) 344-2524
555.344.2524
1-555-344-2524
I imagine this would be done through a specific query probably using a stored procedure?
Thank you.
For SQL 2000 the only way I can think of would be using the REPLACE function.
declare #SearchTerm bigint
Set #SearchTerm = 5553442524
Select * From dbo.Table
Where Replace(Replace(Replace(Replace(Col1,'-',''), '(',''),')',''),'.','')
= #SearchTerm
The problem with this would be it wouldn't cater for the leading 1.
A better way would be wrap all this logic in to a function.
e.g.
Create Function dbo.fn_FormatTelephoneNumber(#input varchar(100))
returns bigint
as begin
declare #temp bigint
Set #temp = Replace(Replace(Replace(Replace(#input ,'-',''), '(',''),')',''),'.','')
If Len(#temp) = 11
begin
Set #temp = Right(#temp, 10)
end
return #temp
End
To call the function you would use it like so:
Select *,
dbo.fn_FormatTelephoneNumber(YourColumnName) as [FormattedTelephoneNumber]
From dbo.YourTable
Or to use it in a WHERE clause:
Select *
From dbo.YourTable
Where dbo.fn_FormatTelephoneNumber(YourColumnName) = 5553442524
Obviously the best thing here would be to clean up the data that is stored in the columns and restrict any further "bad" data from being inserted. Although in my experience that is easier said than done.

How to tweak LISTAGG to support more than 4000 character in select query?

Oracle Database 11g Enterprise Edition Release 11.2.0.2.0 - 64bit Production.
I have a table in the below format.
Name Department
Johny Dep1
Jacky Dep2
Ramu Dep1
I need an output in the below format.
Dep1 - Johny,Ramu
Dep2 - Jacky
I have tried the 'LISTAGG' function, but there is a hard limit of 4000 characters. Since my db table is huge, this cannot be used in the app. The other option is to use the
SELECT CAST(COLLECT(Name)
But my framework allows me to execute only select queries and no PL/SQL scripts.Hence i dont find any way to create a type using "CREATE TYPE" command which is required for the COLLECT command.
Is there any alternate way to achieve the above result using select query ?
if you cant create types (you can't just use sql*plus to create on as a one off?), but you're OK with COLLECT, then use a built-in array. There's several knocking around in the RDBMS. run this query:
select owner, type_name, coll_type, elem_type_name, upper_bound, length
from all_coll_types
where elem_type_name = 'VARCHAR2';
e.g. on my db, I can use sys.DBMSOUTPUT_LINESARRAY which is a varray of considerable size.
select department,
cast(collect(name) as sys.DBMSOUTPUT_LINESARRAY)
from emp
group by department;
You should add GetClobVal and also need to rtrim as it will return delimiter in the end of the results.
SELECT RTRIM(XMLAGG(XMLELEMENT(E,colname,',').EXTRACT('//text()')
ORDER BY colname).GetClobVal(),',') from tablename;
A derivative of #anuu_online but handle unescaping the XML in the result.
dbms_xmlgen.convert(xmlagg(xmlelement(E, name||',')).extract('//text()').getclobval(),1)
I end up in another approach using the XMLAGG function which doesn't have the hard limit of 4000.
select department,
XMLAGG(XMLELEMENT(E,name||',')).EXTRACT('//text()')
from emp
group by department;
For IBM DB2, Casting the result to a varchar(10000) will give more than 4000.
select column1, listagg(CAST(column2 AS VARCHAR(10000)), x'0A') AS "Concat column"...

Dynamic where condition in sybase stored procedure

I am storing where clause statements in the database table, after that I am getting and using in select statement.
Here is my sybase query,
"INSERT table(RUN_ID,RUN_DATE,GRGR_ID,SGSG_ID,COLUMN_NAME,ERROR_CODE)SELECT #pnRunId,getdate(),GRGR_ID,SGSG_ID,#COLUMN_NAME,#ERROR_CODE from table1_exct where + #WHERE_CLAUSE"
#WHERE_CLAUSE = '#IKA_COLUMN_NAME IS NOT NULL'
Please help me how to achieve this.
I get the feeling you can insert your where clauses but don't know how to run them.
That would be hard but not impossible. You'd have to use a cursor or a loop to build up a piece of Sql in a VARCHAR.
declare #Sql VARCHAR(2000)
select #Sql = 'INSERT table(RUN_ID,RUN_DATE,GRGR_ID,SGSG_ID,COLUMN_NAME,ERROR_CODE)SELECT #pnRunId,getdate(),GRGR_ID,SGSG_ID,#COLUMN_NAME,#ERROR_CODE from table1_exct where '
while 1 = 1
begin
declare #claus VARCHAR(200)
SET ROWCOUNT 1
SELECT #claus = where_clause from some_table
select #sql = #sql + #claus
delete some_table where where_clause = #claus
set rowcount 0
end
This has 2 things to think about - it doesn't provide for WHERE and AND for the first and subsquent clauses (I'm sure you can do that,) and it deletes your where clauses from their source (you can get them all into a temp tab first to get round that.)
If your clauses are all on the same column, you can have a list of LIKE expressions in a column and join to that using the LIKE operator - that can be handy.
Anyway at the end of the above, run the sql:
EXEC(#Sql)
Abe's answer is correct, if you build a dynamic SQL query as a varchar, you can exec it via EXEC(#varname) if this is only a 1-off issue.
You could consider use XML functionality in ASE 15. We actually went through this exact same exercise for a in-depth business rules engine at one point, and were fairly set on using the xml features inside ASE 15 to merge computed columns, case statements and where clauses inside XML text in the DB.
Here's some info http://www.sypron.nl/xml.html
So, I believe a solution like this could solve your issue a little more elegantly than dynamic SQL.
We ended up not using it because we couldn't enable XML support in production (legacy 2k page issue)...but I feel like this could solve your problem if your problem is large enough to support the workload of implementing something that could use it (We ended up just using java.)

convert column data to xml format; query fails because all columns types are currently not supported

I am trying to convert column data to xml format, but I get this error message:
The query fails because all columns types are currently not supported.
CREATE TABLE EMP(NAME VARCHAR2(10 BYTE))
INSERT INTO EMP VALUES ('C');
INSERT INTO EMP VALUES ('A');
INSERT INTO EMP VALUES ('T');
SELECT xmlelement("NAME",NAME) FROM EMP;
I am using:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
SQLTools 1.5.0 Beta build 9 as EDITOR
Why is this error arising??? What is the solution for this?
I've found the answer:
select dbms_xmlquery.getxml('select * from EMP') from dual;
This is more of a workaround and not a solution.
I was having the same problems as sam - also running a SELECT xmlelement statement, also using SQLTools. One difference is that I was running Oracle DB version 11.2.0.2.0.
I found that if I ran the statement in SQLPlus, it was able to display the result.
SQL> SELECT XMLELEMENT("name",ename) FROM scott.emp WHERE ROWNUM < 3;
XMLELEMENT("NAME",ENAME)
--------------------------------------------------------------------------------
<name>SMITH</name>
<name>ALLEN</name>
If I ran the statement in SQL Developer, it tried to display the results, but only showed (XMLTYPE).

Resources