Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trim leading blank space within CASE statement

Status
Not open for further replies.

madanthrax

IS-IT--Management
Sep 15, 2001
123
0
0
AT
Hi all,

I have a select statement with case in it which you guys helped me with a few months ago. Its been running fine, but recently the inputs to the field that is the subject of the case function (SeriesInfo) have been getting a blank leading space which messes up the character count (see code below).

Unfortunately the inputs to the MSSQL database are done by another department which has recently started employing monkeys to input the data, who only seem to use copy/paste. Repeated attempts to get them to cooperate have failed. I would like to use the trim function but have no idea what I am doing, or even if its possible within a case statement.

Here is the code with an attempted trim added to it (not working):
Code:
SELECT   TOP (100) PERCENT PubId, Title, Language, LanguageName, ElectronicFile, FileSize, SeriesInfo, Released, Issued, StatusId, Type, SeriesText, CASE WHEN SUBSTRING(trim(SeriesInfo, 4, 1)) = 'R' THEN '1' WHEN SUBSTRING(SeriesInfo, 3, 1) = 'R' THEN '1' WHEN SUBSTRING(SeriesInfo, 1, 4) = 'SF-1' THEN '3' ELSE '2' END AS Category

The select is running in a view, and the case part is to add an extra column to the view that detects what category of document each row is (1,2,or 3).

So is it possible to trim the SeriesInfo field please, and if so what would be the syntax?

Thanks in advance for any help you can give me.

Anthony.

[sub]"Nothing is impossible until proven otherwise"[/sub]​
 

If you change the trim be be Ltrim(Rtrim(SeriesInfo)) that will strip an dleading or trailing spaces.

I love deadlines. I like the whooshing sound they make as they fly by
Douglas Adams
(1952-2001)
 
Thanks dhulbert.

In case anyone is interested in the solution, this worked for me.
Using LTRIM in the case statement was not possible so I used it on the SeriesInfo field in the select statement. This code was accepted by SQL Server Man. Studio but somehow the SUBSTRING was still reading the untrimmed value. I then made view to go between the source data and the view under discussion in which I placed "LTRIM(SeriesInfo) AS SeriesInfo," in the select statement. This worked fine.



[sub]"Nothing is impossible until proven otherwise"[/sub]​
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top