Solved

Left substring in Sybase

Posted on 2006-06-27
7
1,162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

Question has a verified solution.

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

In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
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…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

635 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