From a Sybase Database, how I can get database state ? - sybase-ase

Anyone can explain , query for sybase database state ?
for this
am tried below query , but its not working..
select name from sysdatabases where status2='..'

status2 is an smallint datatype, so using quotes to find a value will give you a syntax error. You need to specify either a number, or range of numbers (without quotes) to get the query to return.
select name from sysdatabases where status2 = 1
or
select name from sysdatabases where status2 in (0,1)
More information on sysdatabases can be found here:
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc36274.1550/html/tables/X42615.htm

Related

Ignore specific rows from a table

I have a table with data like this:
ND
10212121
10232323
10212323
212526
295652
232565
I would like make a select to all ND from this table excluding these starting with 10...using openquery to a oracle database.
Regards
In the following query I check the first two characters of the ND column and compare against 10 to see if they be equal. You did not mention whether or not ND is a numeric type, so I added a cast to varchar2 so that the substring will work.
SELECT ND
FROM yourTable
WHERE SUBSTR(CAST(ND AS varchar2(30)), 1, 2) <> '10'

MySQL showing strange result

I have a table called tabela1512823699024883 that looks like this:
On which I run query like this:
SELECT * FROM tabela1512823699024883 WHERE `age` = 'male'
This query does not make sense, because age column is int type and I'm looking for string value in my query, but MySQL still returns no empty rows whatsoever. Here is what query returned:
So age row does not contains male value in neither rows returned. How can this be possible?
Same issue will occur with casting a string to an integer type.
SELECT CAST('male' as SIGNED); #0
#or
SELECT CAST('male' as UNSIGNED); #0
However if a number is supplied within the string.
SELECT CAST('1234male' as UNSIGNED); #1234
#and
SELECT CAST('male1234' as UNSIGNED); #0
To resolve the issue use BINARY on the column, which will also cause the textual value to become case-sensitive.
SELECT * FROM tabela1512823699024883 WHERE BINARY `age` = 'male';
Try this simple query:
SELECT 'male' + 0
This seems not to make sense. However the query executes and returns a value of 0. This is because of implicit type conversion. String value 'male' is converted to 0 and then added to 0 and thus 0 is returned .
The same thing happens with your query. 'male' in converted to 0 when compared with a field of type int.
"male" is 0! So you keep getting records where age is 0. To really understand what's happening, just do:
select user_name, age, age='male' from tabela1512823699024883;
Your 3rd column will be 1 for the records containing '0' age, and '0' for the records containing non-zero age.
The Problem here is that whenever you try to compare a string to integer column, the value will be cast to the data type of column. Same case will happen with float , big int, tiny int etc.
What ever value is passed in comparison first being cast for that particular data type by mysql engine
Here is the test result of the query casting few values.
select cast('' AS UNSIGNED), cast('anything' AS UNSIGNED), cast(NULL AS UNSIGNED), cast(35.56 AS UNSIGNED), cast(NOW() AS UNSIGNED), cast(1 AS UNSIGNED), cast('Test' AS BINARY), cast('' AS BINARY);
SELECT * FROM `test_type` WHERE flt = 'test'
the flt is of float type column.

Mysql pattern recognition and update [closed]

I've got a mysql table - that contains a column of links
like www.example.com/?book=2
how do I select through the entire table - but extract the id, to put it into its own column? A kind of select, update query?
Would it be easier to just copy the column - then force it to an int type? Would that then leave me the id as a number?
What is the best solution for this.
SELECT id, sourcelink FROM books
UPDATE books SET id=1 WHERE sourcelink ='www.example.com?book=1';
You could do:
update books
set id = substring_index(sourcelink, '=', -1) + 0
where sourcelink like '%\?%=%';
This sets the id to whatever is after the = converted to a number. The where clause checks that the sourcelink has a structure similar to what you expect. This is a rather loose pattern. It could be made more specific by using regular expressions.
This is a mischievous but correct answer, if your id number always comes at the end of the text string.
UPDATE books
SET id = CAST(REVERSE(CAST(REVERSE(sourcelink) AS INT)) AS INT) AS id
WHERE CAST(REVERSE(CAST(REVERSE(sourcelink) AS INT)) AS INT) <> 0
This relies on the quirk of MySQL's casting of a string to an integer that causes it to understand the string value 99 red balloons as the integer value 99.

round() returns different values

I have a field in mysql DB rating with data type float(2,1). One row contains a value 0.5, but when I use
SELECT round(rating) FROM table WHERE ... gives value 0 instead of 1.
But SELECT round(0.5) gives value 1 (for Postgresql too).
Also, PHP always gives value 1 for echo round(0.5);
I am really confused.
Change your data type from float(2,1) to decimal(2,1) it may works for you.
Try SELECT ROUND(rating) instead of SELECT ROUND('rating').
ROUNDing a string might give you weird results. You're probably after the column value, which is not supposed to be escaped with '.
With
SELECT round('rating') FROM table WHERE ...
you are rounding the string 'rating' , that's why it gives you zero
try this one
SELECT round(rating) FROM table WHERE ...

Finding Maximum value from a Varchar Field

I have a database field know as SCORES which has Scores
the value may be like the following
123
14
56*
342
423*
I am storing that in a Varchar Field in the database.
Suppose If I convert that to a integer a datatype, then I can write
max(SCORES) and get the Maximum score or Highest Scores.
But Integer doesnot allow special character like *.
(Here * represent some clause for that scores)
To accomadate that I have made that to the varchar.
What will be best way to get the Highest score very easily with minimum programming method.
So that If I execute a query I should be get the answer as
423*
Please suggest me
The best way to handle this situation is to change you table structure to make
SCORES of int data type.
Add a new field in the table called
clause
If most of your SCORES are without a
clause, you must normalize the table
to move the clause field to a
different table.
You should change table schema ...
SELECT scores
FROM tablename
ORDER BY replace(scores, '*', '') DESC
LIMIT 1;
I think your query should be generic for getting maximum score, today you have only '*' is attached with score but in future may be you use some others character or may be you use some combination of character so you should take care of that scenario.
so i thing it will be better if you create a user define function which takes varchar as a input and return number from input string like if you pass '1234*' then function will return 1234 and if you pass 1234** it will return 1234.
CREATE FUNCTION dbo.ParseNumeric
(
#string VARCHAR(8000)
)
RETURNS VARCHAR(8000)
AS
BEGIN
DECLARE #IncorrectCharLoc SMALLINT
SET #IncorrectCharLoc = PATINDEX('%[^0-9A-Za-z]%', #string)
WHILE #IncorrectCharLoc > 0
BEGIN
SET #string = STUFF(#string, #IncorrectCharLoc, 1, '')
SET #IncorrectCharLoc = PATINDEX('%[^0-9]%', #string)
END
SET #string = #string
RETURN #string
END
GO
then use:
select max(cast(dbo.ParseNumeric(score) as int)) from tableName
SELECT MAX(CAST(scores AS SIGNED))
FROM tablename
But this query will be quite slow, since it cannot be optimized using indexes.
You could move the asterisk to a new field (depends if you need it or not). After that you can change the datatype.
UPDATE
tableName
SET
score = REPLACE(score, '*', ''),
specialCharField = IF(CONTAINS(score, '*'), '*', '');
Try this SQL:
SELECT MAX(CONVERT(REPLACE(FIELD,'*',''),signed)) FROM TABLE
It:
Replace the * from your varchar
Covnert the result into integer (signed)
Select the MAX value
EDIT 1:
I have been tring the SQL with some data, i have a table with one field (varchar, no key), I've inserted 62962 values (one each 1000 have a '*', the results as follow :)
SELECT MAX(CONVERT(REPLACE(FIELD,'*',''),signed)) FROM TABLE
Took 0.0666sec.
SELECT scores FROM tablename ORDER BY replace(scores, '*', '') DESC LIMIT 1;
Took 0.089sec to execute and got the wrong value:
HTH :)

Resources