Solved

Left substring in Sybase

Posted on 2006-06-27
7
1,127 Views
Last Modified: 2012-08-13
I have data in a column that is declared as varchar(20) and the data looks as follows:   123456.pdf
The number of characters before the .pdf can range from 6-10, so I need to grab all data to the left of the period.
I want to put this data into a new column in a different table truncating the ".pdf" part of the string.

My question is basically this:  How do I truncate the data to the left of the .pdf?
0
Comment
Question by:jmp02008
7 Comments
 
LVL 14

Expert Comment

by:sathyagiri
ID: 16994679
In ORACLE it would be

 select substr('123456.pdf',1,instr('123456.pdf','.pdf') -1 ) from <tname>

Not sure if SYBASE has a INSTR function
0
 
LVL 38

Expert Comment

by:Jim P.
ID: 16994879
SELECT  LEFT(MyField, LENGTH(MyField)-4)
FROM MyTableName
0
 

Author Comment

by:jmp02008
ID: 16995091
Ok this is what I got to work, but I'm getting an error when trying to insert it into a new table.

insert into table2 select substring(column_name, 1, charindex('.', column_name)-1) from table1

The table's are identical with respect to column name, type, and size.  But this is my error:
Msg 213, Level 16, State 4
Insert error: column name or number of supplied values does not match table definition.

any suggestions?



I can't use Left because I don't know the length of the string, it could be anywhere between 6-10 characters.
0
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
LVL 38

Expert Comment

by:Jim P.
ID: 16995295
insert into table2 (column_name)
select substring(column_name, 1, charindex('.', column_name)-1) from table1


 LENGTH(MyField)-4 <-- If you know they will end in .pdf this will determine the length of the string and take off the last 4 characters.
0
 
LVL 25

Expert Comment

by:jrb1
ID: 16996310
How many columns in your tables? If more than one, you should have

insert into table2 (col1, col2, col3)
select substring(column_name, 1, charindex('.', column_name)-1), col2, col3
from table1
0
 
LVL 38

Accepted Solution

by:
Jim P. earned 500 total points
ID: 16996600
jrb1,

http://www.experts-exchange.com/Databases/Q_21900951.html#16995091

At the above post you can see that the Asker is only working on one column and I addressed it in my follow up post to that.

0
 
LVL 38

Expert Comment

by:Jim P.
ID: 17002812
Glad to be of assistance. May all your days get brighter and brighter.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I guess that all of us know that caching the data usually increase the performance, but I worried if all of us are aware about the risk that caching the data provides and how to minimize this.  That’s the reason why I decided to write this short art…
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now