String functions in Sybase ASE - sybase-ase

I want to retrieve "name" from "My_name_is_ABC", how to do it in Sybase-ASE ?
I tried finding on internet but inbuild functions i saw are not supported in ase.
Atleast i need to know how to do find index of a string.

Sybase ASE does have inbuilt string functions as Documented Here. You can use SUBSTRING and CHARINDEX function to get your desired result like below
select substring("My_name_is_ABC",charindex("_", "My_name_is_ABC")+1,4)
Syntax for Substring function : substring(expression, start, length )

select substr('My_name_is_ABC',locate('My_name_is_ABC','name'),4) from dummy

Related

Remove substring from string in D

How to remove all occurrences of a string in another? I can do this using the following code:
std.array.replace: "the string".replace("the", "")
But I wonder if there is a dedicated function for this in phobos?
Yes. It's correct function. But you might want to use it from std.string. Because if in future version something changes you'll still be using correct function.
From documentation of std.string:
The following functions are publicly imported:
std.array: replace replaceInPlace ...

PHP text to number or int

hello guys im new to this forum so i a little confused about giving the title ( and im sorry if this already asked somewhere i just dont know where to find it ) , i heard that returning string / text to mysql database is not that safe , so i just want to turn it into number , but i already search troughout google and cannot find what i was looking for , example i have text :
"this is text within a quotes"
i want to turn it into number or something like :
"2421512152142152134"
but what i got from php its returned 0 instead a number it was supposed to be : ( Screenshot )
i cannot find any method to do this , can anyone tell me what is the method to do it ?
i also want it work to all text like utf-8 , utf-7 , or something like that , not just listed text .
just a note , i hate decimal .
I don't know anything about why returning string / text to mysql database is not safe. Maybe if you have some SQL in that string, but in this case you need to escape your string to prevent it to be execute.
If you really want to transform your string for security reasons, i think some encryption / hash method can help you.
hash() Example
echo hash('ripemd160', 'The quick brown fox jumped over the lazy dog.');
The above example will output:
ec457d0a974c48d5685a7efa03d137dc8bbde7e3
This will never work due to restrictions of interger fields in Mysql-Database. It it is explained here: https://dev.mysql.com/doc/refman/5.7/en/integer-types.html
In short words. If you like to parse a string like this "2421512152142152134" to an integer value, it fails, because the maximum number is 2147483647 or 4294967295 for unsigened integers.
You can try Bigint, but it is not safe, if you get long int or numbers.

MySQL SubString Returns Integer

I've got a table with a varchar(128) field called identifier. It is supposed to get a 3 letter identifier followed by an indexing number:
ABC-1234
At some point in the past, there was a bug and about 5,000 records were input as:
ABC-12345
Our numbers do not go that high, so these values are padded with zeros (0).
I wrote out a script to make sure my Microsoft T-SQL lingo was going to work correctly in this MySQL database, but I was shocked to see, what looks like, MySQL adding my values together:
SELECT
identifier,
SUBSTR(cast(identifier as char),1,4) as 'p',
SUBSTR(cast(identifier as char),6) as 'q',
SUBSTR(cast(identifier as char),1,4)+SUBSTR(cast(identifier as char),6) as 'p+q'
FROM drawing_table where length(identifier)=9
order by identifier desc;
Here is a snippet of the output:
identifier,p,q,p+q
STO-00021,STO-,0021,21
STO-00020,STO-,0020,20
STO-00019,STO-,0019,19
STO-00018,STO-,0018,18
STO-00017,STO-,0017,17
STO-00016,STO-,0016,16
STO-00015,STO-,0015,15
STO-00014,STO-,0014,14
STO-00013,STO-,0013,13
STO-00012,STO-,0012,12
STO-00011,STO-,0011,11
STO-00010,STO-,0010,10
STO-00009,STO-,0009,9
STO-00008,STO-,0008,8
STO-00007,STO-,0007,7
STO-00006,STO-,0006,6
STO-00005,STO-,0005,5
STO-00004,STO-,0004,4
STO-00003,STO-,0003,3
STO-00002,STO-,0002,2
STO-00001,STO-,0001,1
STA-00166,STA-,0166,166
STA-00165,STA-,0165,165
STA-00164,STA-,0164,164
STA-00163,STA-,0163,163
STA-00162,STA-,0162,162
STA-00161,STA-,0161,161
STA-00160,STA-,0160,160
STA-00159,STA-,0159,159
STA-00158,STA-,0158,158
STA-00157,STA-,0157,157
STA-00156,STA-,0156,156
STA-00155,STA-,0155,155
STA-00154,STA-,0154,154
STA-00153,STA-,0153,153
STA-00152,STA-,0152,152
STA-00151,STA-,0151,151
STA-00150,STA-,0150,150
STA-00149,STA-,0149,149
STA-00148,STA-,0148,148
STA-00147,STA-,0147,147
STA-00146,STA-,0146,146
STA-00145,STA-,0145,145
STA-00144,STA-,0144,144
STA-00143,STA-,0143,143
STA-00142,STA-,0142,142
STA-00141,STA-,0141,141
STA-00140,STA-,0140,140
STA-00139,STA-,0139,139
STA-00138,STA-,0138,138
Why is MySQL trying to add my string values?
Try to use CONCAT(SUBSTR(cast(identifier as char),1,4), SUBSTR(cast(identifier as char),6)) please.
Does it work now? Edit: That work!
If you want to concatenate two strings you should use concat function
For example:
SELECT CONCAT('WAS-','0020')
Result
WAS-0020
You have to use the CONCAT function instead of the + operator.
see: Mysql Manual
In your example this would be like:
CONCAT( SUBSTR(cast(identifier as char),1,4), SUBSTR(cast(identifier as char),6) ) as 'p+q'

How to change decimal separator cleanly in outputs in mysql - php combo

I was trying to export database query to CSV and the need arised to use different decimal comma. It seems impossible to change the decimal comma in MySQL, so I tried in PHP:
setlocale(LC_NUMERIC, "cs_CZ");
But it seems that all the database functions like mysql_fetch_row and mysql_fetch_assoc are retrieving data of type string instead of double:
$res = mysql_query("select 50/3");
$row = mysql_fetch_row($res);
var_dump($row); // $row[0] is of type "string"
So in general PHP already doesn't have data of type double, but only strings!
So is there some general, clean way to specify output of the decimal point?
I ended up converting the strings using str_replace('.', ',', $row[0]) but this is ugly for two reasons:
you have to know which field is of type double
it's a dirty string job.
I don't know which database client/driver you're using but there is something like mysql_field_type­Docs which gives you the type based on it's offset, like 0.
This should do the job to find out if a certain column needs re-formatting or not.
To reformat, there is number_format­Docs.
With these you can do the conversion automatically.
Edit: Regarding your comments:
If you want to get PHP datatypes mapped, consider using the MySQL Native Driver­Docs. Use it together with PDO:
Advantages of using mysqlnd for PDO
mysqlnd returns native data types when using Server-side Prepared Statements, for example an INT column is returned as an integer variable not as a string. That means fewer data conversions internally. Source
So depending of what you try to achieve, use the right tool.
See as well the multiple options you have when fetching data from a PDO Statement­Docs.
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
Try
number_format($row[0], 2, ",", ".");
That should change the format of the number. (german format)

unixODBC + PHP , Invalid string or buffer length error message

I access to an AS400/DB2 with PHP via unixODBC
But some field name of my AS400 are named with "££.." (it can't be changed)
And when I tried to launch an sql query from my PHP page
select * from LIBNAME.TABLE where ££FIELD like 'test%'
I always get the same error message
SQL error: [unixODBC][IBM][System i Access ODBC Driver]Invalid string or buffer length., SQL state S1090 in SQLExecDirect
If someone have clue.
Try adding this statement before the query execution:
setlocale(LC_CTYPE, 'en_US');
For a more detailed description of the issue:
http://bugs.centos.org/view.php?id=3187
DB2 should have some way of enclosing the identifiers. Some databases use double quotes
select * from "tableID" where "fieldID" like 'blah%'
mySQL uses backticks. Not sure with DB2 but shouldn't be too hard to find out and try. If that doesn't work, see if there is a way to represent characters by hex value in a string. Like 'hello\x0A\x0Dthere' would be the words 'hello' and 'there' separated by a line-break. You may just need to find the value for £.

Resources