If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Sybase > How to fetch only first 6 numeric values from varchar column

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-06-10, 13:17
vsugadev vsugadev is offline
Registered User
 
Join Date: Aug 2010
Posts: 4
Unhappy How to fetch only first 6 numeric values from varchar column

Someone please help to write query for below screnaio.
I want to fetch only first 6 numeric values from varchar column using below conditions.
Conditions are,
1. Substring only first 6 numeric.
2. If any substring output has alphabet ignore the alphabets.

Tablename: Sys_tablename
Columnname: Sys_columnname

Select Sys_columnname from Sys_tablename;
Go

Sys_columnname:
================
1234564567890
ABCDEFGHIJKLMNOPQRSTU
123456AABCDEFGH
123ABCDEFGHIJKL12
12345ASDFASDFEA123
123456789ASDFASDASDF
12345SDFASDFASDF123

Output would be like below:
======================
123456
NULL (ignore)
123456
123
12345
123456
12345
Reply With Quote
  #2 (permalink)  
Old 08-06-10, 15:23
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,517
Can I ask why you're doing this or is it just coursework?
__________________
Mike
Reply With Quote
  #3 (permalink)  
Old 08-06-10, 16:15
dav1mo dav1mo is offline
Registered User
 
Join Date: Dec 2007
Location: Richmond, VA
Posts: 779
c'mon MBK, its easy, they are storing multiple entities in the same column and not sure that they should fix it or not.
Reply With Quote
  #4 (permalink)  
Old 08-06-10, 16:52
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,517
I was going to wait for a reply before lecturing them on their design (or their table naming). Looking at the data though I'd guess it's coursework.

MBK
__________________
Mike
Reply With Quote
  #5 (permalink)  
Old 08-08-10, 21:52
hobbylu hobbylu is offline
Registered User
 
Join Date: Nov 2002
Posts: 21
You can do like this(Only digit and alp).

Select case patindex('%[A-Z]%',upper(Sys_columnname))>6 then substring(Sys_columnname,1,6)
else
substring(Sys_columnname,1,patindex('%[A-Z]%',upper(Sys_columnname))) from Sys_tablename
Reply With Quote
  #6 (permalink)  
Old 08-09-10, 10:06
vsugadev vsugadev is offline
Registered User
 
Join Date: Aug 2010
Posts: 4
Quote:
Originally Posted by mike_bike_kite View Post
I was going to wait for a reply before lecturing them on their design (or their table naming). Looking at the data though I'd guess it's coursework.

MBK
Hi MBK,
I got this doubt,as part of self learning execise on SYBASE database. If u have any idea on this kindly share with me.
Reply With Quote
  #7 (permalink)  
Old 08-09-10, 10:09
vsugadev vsugadev is offline
Registered User
 
Join Date: Aug 2010
Posts: 4
thanks hobblylu.

But patindex is not working.
Reply With Quote
  #8 (permalink)  
Old 08-09-10, 10:29
hobbylu hobbylu is offline
Registered User
 
Join Date: Nov 2002
Posts: 21
Sorry for upper sql. It's wrong.
Right SQL is:
Select upper(Sys_columnname),case when patindex('%[A-Z]%',upper(Sys_columnname))>6 then substring(Sys_columnname,1,6)
when patindex('%[A-Z]%',upper(Sys_columnname))=0 then substring(Sys_columnname,1,6)
else
substring(Sys_columnname,1,patindex('%[A-Z]%',upper(Sys_columnname))-1) end from Sys_tablename
1234564567890
123456
ABCDEFGHIJKLMNOPQRSTU
NULL
123456AABCDEFGH
123456
123ABCDEFGHIJKL12
123
12345ASDFASDFEA123
12345
123456789ASDFASDASDF
123456
12345SDFASDFASDF123
12345
Reply With Quote
  #9 (permalink)  
Old 08-09-10, 10:51
vsugadev vsugadev is offline
Registered User
 
Join Date: Aug 2010
Posts: 4
thanks a lot hobblylu.Its working ....
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On