Previously I was using the MySQL. With that I was able to use the query below to get the maximum number from the database.
Here 'No' is the varchar(10):
SELECT max(cast(No as unsigned)) as No FROM `tableName` LIMIT 1
The above query working fine in MySQL. I want to do the same thing in the MS SQL. When I run the same query, I get the following error:
Warning: sqlsrv_fetch_array() expects parameter 1 to be resource, boolean given
Any advice on this?
There is no LIMIT in SQL Server, no unsigned datatype, and no need to quote the table name.
Does this work:
SELECT max(cast(No as bigint)) as No FROM tableName
Related
I have a series of tables with the same prefix, and I need to select data from the latest version --whose postfix with the highest numeric number. Here is what I have:
SELECT
#latest_version_number :=
MAX(
CAST(SUBSTRING_INDEX(table_name,'_',-1) AS UNSIGNED)
)
FROM information_schema.tables
WHERE lower(table_name) like '{table_prefix}%';
SELECT
*
FROM CONCAT('`{table_prefix}', CAST(#latest_version_number AS CHAR), '`')
It behaved like what I expected when ran as 2 separate queries in the console. But I got "syntax error" trying to run it as a single query. What's the cleanest way to refactor this into a single query? Thanks
The only way I'm aware that you can use a variable in a table name in MySQL is using the prepare + execute statements.
Here is an example I found online that gives you exact instructions.
https://www.tutorialspoint.com/set-user-defined-variable-with-table-name-in-mysql-prepare-statement
I have a problem with Aliased Columns in MySQL!
My Query:
SELECT Price AS Pr, (Pr*10/100) FROM MyTable;
MySQL WorkBench Error: UnKnown Column 'Pr' in Field List !!!
I tested my query in W3Schools with no error !
I tested my query in W3Schools with no error!
This doesn't prove that your query is valid.
You can only use aliases in GROUP BY, ORDER BY or HAVING clauses. Your usage variant is not allowed, because the value of alias is not known when MySQL is selecting the 2-nd column.
I've got a suspicion that W3Schools uses MS Access to run user queries, and MS Access does allow such atrocity as referencing column aliases in a SELECT clause that are defined in the same SELECT clause.
The standard doesn't allow this and MySQL does follow standard in this particular case.
As for solution to your problem, I can see two options.
The more generic solution, which would run in probably any SQL product, would be to use a derived table:
SELECT
Pr,
(Pr * 10 / 100) AS SomethingElse
FROM
(
SELECT
SomeComplexExpression AS Pr
FROM MyTable
) AS sub
;
The other option would be to use a variable, which is MySQL-specific:
SELECT
#Pr := SomeComplexExpression AS Pr,
(#Pr * 10 / 100) AS SomethingElse
FROM MyTable
;
Finally, if you need to test/demonstrate if something can/cannot work in MySQL, I'd recommend using SQL Fiddle.
There is an Issue while i try to auto Increment alphanumeric Id (or number Series) for example
in Mysql "Sample" Table 'RefNo' column (of type Varchar)
AB7
AB10
AB9
AB8
above i have four entries now i want to retrieve max (or highest value).
for this i have tried query as = SELECT MAX(RefNo) FROM sample;
but this gives result as 'AB9' which is wrong and it is supposed to return 'AB10' as result.
To get this in Mysql i have modified the Query as
= SELECT MAX(CONVERT(SUBSTRING_INDEX(RefNo,'B',-1),UNSIGNED INTEGER)) from sample where RefNo like 'AB%'
this work's fine in mysql but in hibernate (hql) query is not supported.
I hope you understand the scenerio and Please help me to solve the issue.
You should be able to use substring and cast to get the numeric part of the string and cast it to a number. Something like that (not tested) :
select max(cast(substring(sample.refNo, 3) as INTEGER)) from Sample sample ...
I have Sphinx running to index my MySQL query's.
When I do the following SQL query I get a perfect result:
SELECT * FROM huisjesIndex WHERE MATCH('#formatted "^20120901$"') ORDER BY huis_contract DESC, global_points DESC LIMIT 200,20;
But I would like to replace MATCH('#formatted "^20120901$"') with a larger/smaller than query. In SQL I would do the following: formatted BETWEEN 20120901 AND 20120931. Unfortunately this draws an error on Sphinx.
Can anyone give me the syntax of the SQL query to do a query on a range? I need this to get results for the entire 201209 month.
EDIT
I was able to answer my own question.
When using MATCH() the value described has to be a field (because you are string searching).
When you do a compare like 'int >= 0' then the int must be an attribute.
Thanks for suggestions!
I am getting this error while I am trying to execute a simple SELECT statement in Toad
MySql.Data.Types.MySqlConversionException
Unable to convert MySQL date/time value to System.DateTime
What could be wrong?
That could mean one of these two common issues:
1) Zero dates, which are 0000-00-00 in MySQL. MySQL allows you to store them to mark 0 dates, you can even use 0001-01-01, but not all drivers or downstream programs can handle them. Add to the connection string
Allow Zero Datetime=true;
The other choice is explicitly removing them, something like
SELECT IF(DateCol='0000-00-00' OR DateCol<'1970-01-01', NULL, DateCol) as DateCol,
Othercol1, ID ....
FROM TBL
2) Date formatting. For some driver/program combination, the dates are handled as strings. Explicit conversion is necessary:
SELECT DATE_FORMAT(DateCol, '%m/%d/%Y') as DateCol,
Othercol1, ID ....
FROM TBL