473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Substring result short by 1

Hi,

I have a question about substring(), when I run the following query:

prepaid=# select substring('15148300', 0, 5);
substring
-----------
1514
(1 row)
I get a result with only the first 4 characters, not five. Why is
that? The documentation doesn't spend much time discussing the
substring() function and google turned up nothing. I tried
searching the archives but the site is down.

David

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to ma*******@postgresql.org

Nov 23 '05 #1
2 2001
From: "David Filion" <da***@filiontech.com>


prepaid=# select substring('15148300', 0, 5);


try substring('15148300', 1, 5);

gnari

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

Nov 23 '05 #2
David Filion <da***@filiontech.com> writes:
prepaid=# select substring('15148300', 0, 5);
substring
-----------
1514
(1 row) I get a result with only the first 4 characters, not five. Why is
that?


AFAICS this is per spec. It's a bit odd that SQL doesn't consider
start position less than 1 as an error, but that's how the spec
is written:

a) Let C be the value of the <character value expression>, let
LC be the length of C, and let S be the value of the <start
position>.

[ so for your example, LC = 8, S = 0 ]

b) If <string length> is specified, then let L be the value of
<string length> and let E be S+L. Otherwise, let E be the
larger of LC + 1 and S.

[ L = 5, E = S+L = 5 ]

c) If either C, S, or L is the null value, then the result of
the <character substring function> is the null value.

[ nope ]

d) If E is less than S, then an exception condition is raised:
data exception-substring error.

[ nope ]

e) Case:

i) If S is greater than LC or if E is less than 1, then the
result of the <character substring function> is a zero-
length string.

[ nope ]

ii) Otherwise,

1) Let S1 be the larger of S and 1. Let E1 be the smaller
of E and LC+1. Let L1 be E1-S1.

[ S1 = 1, E1 = 5, L1 = 4 ]

2) The result of the <character substring function> is
a character string containing the L1 characters of C
starting at character number S1 in the same order that
the characters appear in C.

[ result = what you got ]

The only case in which you can get an error is by specifying a negative
L. Otherwise, you get whatever part of the string overlaps your
subscript range specification --- at either end.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Radhika Sambamurti | last post by:
Hi, I've written a substring function. The prototype is: int substr(char s1, char s2) Returns 1 if s2 is a substring of s1, else it returns 0. I have written this program, but Im sure there is an...
62
by: Jupiter | last post by:
Hey, I would like to know 2 things. 1)Is there any function (in C standard library) that extracts a substring from a string? 2)Is there any function (in C standard library) that returns the...
6
by: becte | last post by:
I am little bit confused Is this a legal way of removing a substring from a string? What about the second alternative using strcpy, is it ok even though the source and dest. strings overlap? ...
3
by: Bill | last post by:
I just ran into a situation where string data from a mainframe contained a couple of non-alphanumeric characters (hex CC and C8). I was parsing a field that occurred after these unexpected...
3
by: Tim Soliday | last post by:
I have been looking at regular expressions lately, and I'm hoping that maybe someone could help me? I am trying to find the position of a substring, and then returning the substring including...
11
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not...
15
by: Duncan Allen | last post by:
Hi, Using C# I'm trying to use the substring method of a string variable but it just generates an "error: 'variable.Substring' does not exist " exception - how do I fix this ? code example: ...
8
by: gmclee | last post by:
Hi there, I need a program to extract a substring from the following pattern xxxx<XXXXX>xxxx therer, x and X are any character and the string between < and is what I need. I use the following...
4
by: Jean-François Michaud | last post by:
Hello, I've been looking at this for a bit now and I don't see what's wrong with the code. Can anybody see a problem with this? Here is an XSLT snippet I use. <xsl:template match="graphic">...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.