Solved

Left substring in Sybase

Posted on 2006-06-27
7
1,143 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
Creating and Managing Databases with phpMyAdmin in cPanel.
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…

680 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