I have a problem in a select on mssql
in my table there is a column with datatype ntext and i'm searching for an specific character.
when i read the value and convert it to hex ill get something like that
SELECT convert(varbinary(max),convert(nvarchar(max),COLUMN_Value)) FROM db.TABLE WHERE PK = 1234
0x3C005000200063006C006100730073003D004D0073006F004E006F0072006D0061006C0020007300740...
In the hex value there is for example 3C00, when I now rewrite my select and search for this value no row is matching :/
SELECT * FROM db.TABLE WHERE convert(varbinary(max),convert(nvarchar(max),COLUMN_Value)) LIKE '%3C00%'
Does anybody know whats the problem here?
finally i've done it with following query
SELECT * FROM db.TABLE WHERE CONVERT(varchar(max),convert(varbinary(max),convert(nvarchar(max),COLUMN_Value)),2) LIKE '%3C00%';
i used the hex value from my first query (3C00) and found every row containing it
Try this
SELECT * FROM db.TABLE WHERE convert(nvarchar(max),COLUMN_Value) LIKE '%3C00%'
Related
I have table with a column Order of type varchar as follows
Order
-----
Ord-998,
Ord-999,
Ord-1000,
Ord-1001,
I want to get the max value as 1001
But when I run this query, I am getting 999 as max value always
select
SUBSTRING((select isnull(MAX(OrderNo), '0000000')
from OrderSummary
where OrderNo like 'Ord%'), 5, 10) as [OrderMax]
Can anybody provide a solution?
Since you are maxing a string it is sorting alphabetically where C is larger than AAA and 9 is larger than 10. Remove the letters and cast it as an int then get the max. Given that it will always be Ord-### we can remove the Ord- and cast the remainder as an INT.
SELECT
MAX(CAST(SUBSTRING(OrderNo,5,LEN(OrderNo)-4) AS INT))
FROM OrderSummary
WHERE OrderNo LIKE 'Ord-%'
Another possibility would be the REPLACE-function:
SELECT
MAX(CONVERT(int,(REPLACE(OrderNo, 'Ord-' ,'')))) AS OrderMax
FROM OrderSummary;
But like already mentioned in comments, the best solution will be to get rid of the "Ord-" and create a int-column instead.
If I were tackling this issue I would Order Number as an integer instead of a varchar, and not store the repetitive text "Ord-" in the field. If end users require "Ord-998", I'd handle it via my CRUD layer.
This also allows you to declare the Order Number column as an identity column, which will handle the automatic incrementing for you.
Please try this query in SQL Server
select MAX(convert(int, substring(orderno, 5, LEN(orderno))))
from OrderSummary
I attach another possible solution this query is posgresQL
SELECT
CONCAT(RPAD('Ord-',5), MAX(CAST(SUBSTRING(OrderNo,5,length(OrderNo)-4) AS INT)))AS
ORDER
FROM OrderSummary
RESULT
Order
Ord-1001
Use len keyword instead of like keyword
select MAX(CAST(SUBSTRING(OrderNo, 6, len(OrderNo)) AS INT)) FROM OrderSummary
The problem, as stated in other answers is that you have a string. I will provide here an alternative solution: you could select top 1 from your table and have an order by len(OrderNo) desc, OrderNo desc. This way you will not have to cut all the varchars and convert them to integer, but will be able to simply compare their length (very quick) and if similar, their varchar value (pretty slow). Of course, a default for null values is a good idea, but make sure you do not put more 0-s there than the number of digits of your first number.
u can cast as int,it will consider value,not lexic. order
ie.
max(cast(Col as int)) from Table
I have a Retailer code ,It is combination of varchar and Int =>RT003880 like this
I have create a Retailer code as script-wise every day. So i need the last Retailer code i have inserted.
So I have split the Integer and varchar for finding the Max value.
This is the max value by using Query.
select SUBSTRING(Retailer_code,5,9) as RetailerCode
Into #maxfindtable
from dbo.sample
select MAX(RetailerCode) from #maxfindtable
I need to put in in function or Stored Procedure how to do this
Try:
select max(SUBSTRING(RetailerCode,3,len(RetailerCode)-2))
for RT003880 the integer part is starting from position 3,and
len(RetailerCode)-2 isdefine the length of the substring. i.e: all the
character starting from 3rd position
See SUBSTRING for more clearification.
EDIT 2:
try Using Cast
create table #tab (genId varchar(max))
insert into #tab(genId)
values('RT00031'),('RT00013232'),('RT00034'),('RT00084')
select * from #tab
select max(cast(SUBSTRING(genId,3,len(genId)-2) as int)) from #tab
I'd split the RetailerCode into two columns (one CHAR/VARCHAR and one INT/SMALLINT/NUMERIC) to be able to get some performance out of the table. Possibly use a calculated column to concatenate them if requested. I would never query on the calculated column if it was not persisted, however.
Today I have inserted 10000 new records,its choosing wrong max value. Both The codes Written Same Value,Maximum Value is RT0017898,But It shows RT0009999.
After I have Checked a sample data
RetailerId RetailerCode RetailerName
1 RT00031 mBigBazar
2 RT00034 TBazar
3 RT00084 SaravanaStore It shows Correct Value - 00084
When I have insert a new Record it shows wrongly
RetailerId RetailerCode RetailerName
1 RT00031 mBigBazar
2 RT00034 TBazar
3 RT00084 SaravanaStore
4 RT00013232 NewStore
Now it shows 00084 .why it shows wrong?
I have a column in DB where it may contain data with special characters.
for ex:a column,Name may have data as following
santosh's
santosh/s
santosh%s
How to search for these values in DB using like operator.
Select * from table where name like '%santosh%';
How to change the above query to search for apostrophe(santosh's)
for 1. apostrophe s you can use
Select * from table where column like '%santosh''s%'
for 2. /s you can give directly
Select * from table where column like '%santosh/s%'
for 3. %s you can use
Select * from table where column like '%santosh[%]s%'
hope this helps.....
Just use 2 apostrophe to escape the character
Select * from table where name like '%santosh''s%';
I'm working on a project where we have to figure out if a given field is potentially a company name versus an address.
In taking a very broad swipe at it, we are going under the assumption that if this field contains no numbers, odds are it is a name vs. a street address (we're aiming for the 80% case, knowing some will have to be done manually).
So now to the question at hand. Given a table with, for the sake of simplicity, a single varchar(100) column, how could I find those records who have no numeric characters at any position within the field?
For example:
"Main Street, Suite 10A" --Do not return this.
"A++ Billing" --Should be returned
"XYZ Corporation" --Should be returned
"100 First Ave, Apt 20" --Should not be returned
Thanks in advance!
Sql Server allows for a regex-like syntax for range [0-9] or Set [0123456789] to be specified in a LIKE operator, which can be used with the any string wildcard (%). For example:
select * from Address where StreetAddress not like '%[0-9]%';
The wildcard % at the start of the like will obviously hurt performance (Scans are likely), but in your case this seems inevitable.
Another MSDN Reference.
select * from table where column not like '%[0-9]%'
This query returns you all rows from table where column does not contain any of the digits from 0 to 9.
I like the simple regex approach, but for the sake of discussion will mention this alternative which uses PATINDEX.
SELECT InvoiceNumber from Invoices WHERE PATINDEX('%[0-9]%', InvoiceNumber) = 0
This worked for me .
select total_employee_count from company_table where total_employee_count like '%[^0-9]%'
This returned all rows that contains non numeric values including 2-3 ..
This Query to list out Tables created with numeric Characters
select * from SYSOBJECTS where xtype='u' and name like '%[0-9]%'
I am trying to retrieve the largest number from a varchar column that includes both numbers and strings. An example of the data I'm working with:
BoxNumber
123
A5
789
B1
I need to return the largest number (789 in this case) from the column while ignoring the non-numeric values of A5 and B1.
I have found solutions that use custom functions to solve the problem, but I need something that can be executed ad-hoc query without relying on custom functions or procs.
you need a combination because of the fact that isnumeric returns 1 for the following things
select isnumeric('+'),isnumeric('5d2')
your where clause would be like this
WHERE VALUE NOT LIKE '%[a-z]%'
AND ISNUMERIC(VALUE) = 1
create table #bla (value varchar(50))
insert #bla values('123')
insert #bla values('a5')
insert #bla values('789')
insert #bla values('b1')
SELECT MAX(CAST(value AS Int)) FROM #bla
WHERE VALUE NOT LIKE '%[a-z]%'
AND ISNUMERIC(VALUE) = 1
I wrote about this here ISNUMERIC Trouble
You might try
Select MAX(BoxNumber) from {table} where IsNumeric(BoxNumber) = 1
Why not
SELECT MAX(CAST(Value AS Int)) FROM #bla
WHERE ISNUMERIC(Value)=1
AND Value LIKE '%[0-9]%'
then you're only dealing with numeric strings. In this case you may not need ISNUMERIC()
The selected answer worked for me until I added this value to the temp table along with the others in the sample:
insert #bla values('1234')
I expected my max() result to now be 1234, but it remained at 789. Perhaps this is due to some collation setting, but I was able to reproduce on multiple databases. I found this query below worked for me, but I'd certainly be interested to hear if there is a more efficient way of doing this. Also, I did not want to include any decimal values, so I excluded anything with a period as well.
SELECT MAX(CAST(Value AS Int)) FROM #bla
WHERE ISNUMERIC(Value)=1
AND Value NOT LIKE '%[a-z]%'
AND Value NOT LIKE '%.%'
You should check this solution out for values like '+' and '-' as I think the IsNumeric function may return 1 for these values
Well, in the intervening 11 years since this question was asked, SQL Server gained the try_cast function, which returns null rather than throwing an error if the conversion doesn't succeed. So in current versions of SQL Server, a valid answer is
select max(try_cast(BoxNumber as int)) from theTable
Look into casting the column to an int, then selecting the MAX(). I don't know what it will do to columns that contain letters, but it's worth exploring.
http://doc.ddart.net/mssql/sql70/ca-co_1.htm
These answers are only half right. They succeed in isolating numeric values, but don't realize that when the underlying field is a number the Max function evaluates as characters (regardless of casting), reading left to right, so that 789 > 1000 because 7 > 1. A way around this might be to forget about casting and left pad the numbers with zeros to a common length, when Max in character mode should work.
SELECT MAX(CAST(value as signed)) FROM yourTable
WHERE VALUE NOT LIKE '%[a-z]%'
AND ISNUMERIC(VALUE) = 1
In MySql
You should use signed instead of int to cast correctly.
P.s types used to cast may differ in MySql
For more conversions visit this link
select max(cast(column as numeric)) from table
Where column was varchar which was casted to numeric in postgresql