473,408 Members | 2,444 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,408 software developers and data experts.

LEFT and string function

select left('Hello World /Ok',charindex('/','Hello World /Ok')-1)
Hello World
That works fine.

However I got an error message:
select left('Hello World Ok',charindex('/','Hello World Ok')-1)
Instead of:
'Hello World Ok'

I get:
Server: Msg 536, Level 16, State 3, Line 1
Invalid length parameter passed to the substring function.

Microsoft Doc incorrectly says:
"LEFT ( character_expression , integer_expression )
integer_expression
Is a positive whole number. If integer_expression is negative, a null
string is returned."

Is there an easier solutoin using left or any other string function
instead of using a case statement?
Also, charindex('/','Hello World Ok') should return NULL instead 0 so
that we can use isnull function.
Thanks.

Nov 3 '06 #1
2 7985
On 3 Nov 2006 02:45:44 -0800, ot*******@yahoo.com wrote:

(snip)
>Microsoft Doc incorrectly says:
"LEFT ( character_expression , integer_expression )
integer_expression
Is a positive whole number. If integer_expression is negative, a null
string is returned."
Hi othellomy,

Your copy of Books Online must be outdated, then. Here's what my version
says:

"integer_expression

"Is a positive integer that specifies how many characters of the
character_expression will be returned. If integer_expression is
negative, an error is returned. integer_expression can be of
type bigint."
>Is there an easier solutoin using left or any other string function
instead of using a case statement?
Also, charindex('/','Hello World Ok') should return NULL instead 0 so
that we can use isnull function.
You can use NULLIF to change the 0 to NULL yourself:

SELECT LEFT ('Hello World Ok',
NULLIF (CHARINDEX('/','Hello World Ok'), 0) - 1);

--
Hugo Kornelis, SQL Server MVP
Nov 3 '06 #2
Thanks :)

Hugo Kornelis wrote:
On 3 Nov 2006 02:45:44 -0800, ot*******@yahoo.com wrote:

(snip)
Microsoft Doc incorrectly says:
"LEFT ( character_expression , integer_expression )
integer_expression
Is a positive whole number. If integer_expression is negative, a null
string is returned."

Hi othellomy,

Your copy of Books Online must be outdated, then. Here's what my version
says:

"integer_expression

"Is a positive integer that specifies how many characters of the
character_expression will be returned. If integer_expression is
negative, an error is returned. integer_expression can be of
type bigint."
Is there an easier solutoin using left or any other string function
instead of using a case statement?
Also, charindex('/','Hello World Ok') should return NULL instead 0 so
that we can use isnull function.

You can use NULLIF to change the 0 to NULL yourself:

SELECT LEFT ('Hello World Ok',
NULLIF (CHARINDEX('/','Hello World Ok'), 0) - 1);

--
Hugo Kornelis, SQL Server MVP
Nov 6 '06 #3

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

Similar topics

15
by: lawrence | last post by:
Sorry for the dumb question but I'm new to Javascript. I wrote this script hoping to animate some div blocks on a page. You can see the page here: http://www.keymedia.biz/demo.htm Can anyone...
17
by: Nathan Given | last post by:
Hello All, I am trying to debug a broken query. The query uses Left$(,4) instead of Left(,4). What is the difference between the Left() and Left$() functions in Microsoft Access? Thanks!...
4
by: Agnes | last post by:
in .net , any left function ?? or I should use Microsoft.VisualBasic.Left(myString, 5) Thanks
18
by: Jm | last post by:
Hi all I feel stupid for asking this, but i just went to use the left() function from vb6 only to find it doesnt do what it used to under .NET. Im assuming theres something else now im meant to...
3
by: OutdoorGuy | last post by:
Greetings, I have a "newbie" question. I was wondering if there is anything in C# that corresponds to VB's "Left" function? I simply want to retrieve the leftmost characters of a string and I...
9
by: sql guy123 | last post by:
I normally use MS ACCESS vs MS SQL,, which has a left() and right() function. I need to use MS SQL for this project but I am not familiar with it. I have read a few books, but can not figure out...
6
by: kellygreer1 | last post by:
What is a good one line method for doing a "length safe" String.Substring? The VB classes offer up the old Left function so that string s = Microsoft.VisualBasic.Left("kelly",200) // s will =...
6
by: =?Utf-8?B?R3JlZw==?= | last post by:
I have two questions with regards to the LEFT function. I ran into a problem with the LEFT function today. I knew it was a valid Function, but when I tried to use it, it was getting interpreted...
2
by: yalbizu | last post by:
#include <iostream> #include <string> #include <fstream> #include <iomanip> using namespace std; const int NO_OF_STUDENTS=20; struct studentType { string studentFName; string studentLName;
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.