parsing in sybase a fields with different digits - sybase

i have a field on sybase for a particular table where i need to select only the first value. See the example below:
Field1
104676;ABC;345776;TEST
2332;ABC;345776;TEST
8765432;ABC;345776;TEST
This particular field have char format. I want to pullout only the first value (starting from the left) that is compost by different number of digits, in particular:
Field1
104676
2332
8765432
in conclusion i need to formart the field in numeric and pullout only the first value.
thanks id advance for your help.

I guess you may easily pull this using SUBSTRING/CHARINDEX function combination -
SELECT SUBSTRING(Field1, 1, CHARINDEX(';', Field1) -1)
FROM YOUR_TABLE

Related

Replace a string with an integer value keeping the string length fixed

I have two columns in SQL Server Table, one is Id and other one is Number, for example if Ids are as follows: 1189, 758, 756, I want a User defined or system defined function, that can generate numbers as: T01189, T00758, T00756.
I want to keep this function either as Default field or add it to trigger - soon after the record inserted, you can suggest me which one is better also.
You could use a padding trick here:
SELECT
Id,
'T' + RIGHT('0000' + Id, 5) AS Number
FROM yourTable;
Demo

Separating Numeric Values from string

I'm having an issue, whereby I need to separate the following BPO007 to show BPO and 007. In some cases another example would be GFE0035, whereby I still need to split the numeric value from the characters.
When I do the following select isnumeric('BPO007') the result is 0, which is correct, however, I'm not sure how split these from each other.
I've had a look at this link, but it does not really answer my question.
I need the above split for a separate validation purpose in my trigger.
How would I develop something like this?
Thank you in advance.
As told in a comment before:
About your question How would I develop something like this?:
Do not store two pieces of information within the same column (read about 1.NF). You should keep separate columns in your database for BPO and for 007 (or rather an integer 7).
Then use some string methods to compute the BPO007 when you need it in your output.
Just not to let you alone in the rain.
DECLARE #tbl TABLE(YourColumn VARCHAR(100));
INSERT INTO #tbl VALUES('BPO007'),('GFE0035');
SELECT YourColumn,pos
,LEFT(YourColumn,pos) AS CharPart
,CAST(SUBSTRING(YourColumn,pos+1,1000) AS INT) AS NumPart
FROM #tbl
CROSS APPLY(SELECT PATINDEX('%[0-9]%',YourColumn)-1) AS A(pos);
Will return
YourColumn pos CharPart NumPart
BPO007 3 BPO 7
GFE0035 3 GFE 35
Hint: I use a CROSS APPLY here to compute the position of the first numeric character and then use pos in the actual query like you'd use a variable. Otherwise the PATINDEX would have to be repeated...
Since the number and text varies, you can use the following codes
DECLARE #NUMERIC TABLE (Col VARCHAR(50))
INSERT INTO #NUMERIC VALUES('BPO007'),('GFE0035'),('GFEGVT003509'),('GFEMTS10035')
SELECT
Col,
LEFT(Col,LEN(Col)-LEN(SUBSTRING(Col,PATINDEX('%[0-9]%',Col),DATALENGTH(Col)))) AS TEXTs,
RIGHT(Col,LEN(Col)-LEN(LEFT(Col,LEN(Col)-LEN(SUBSTRING(Col,PATINDEX('%[0-9]%',Col),DATALENGTH(Col)))))) AS NUMERICs
FROM #NUMERIC

select max value from a string column in SQL

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

How to Split the String and integer in Sql to find the max and put it in SP or functions to call

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?

How would I determine if a varchar field in SQL contains any numeric characters?

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]%'

Resources