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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Can't get Query Syntax right..... 1

Status
Not open for further replies.

brettgab

Programmer
Aug 29, 2001
13
AU
I'm trying to run a query which returns three fields, one of the fields needs any potential leading space stripped off. Due to my being a bit SQL-retarded, I can't get it working. Any help appreciated...

SELECT DISTINCT kpp.plan_phone_code + kpp.plan_phone_suffix AS Plan_Code, kph.plan_header_code AS Header_Code,
IF (SELECT SUBSTRING(kpp.plan_phone_description,1,1)) = ' '
SELECT SUBSTRING(kpp.plan_phone_description,2,len(kpp.plan_phone_description)) AS Phone_Desc
ELSE
SELECT kpp.plan_phone_description AS Phone_Desc
FROM dbo.kit_plan_phones kpp INNER JOIN
dbo.kit_plan_header kph ON kpp.plan_phone_code = kph.plan_header_code

 

Just use the LTRIM function to eliminate leading spaces and RTRIM to eliminate trailing spaces.

SELECT
DISTINCT kpp.plan_phone_code +
kpp.plan_phone_suffix AS Plan_Code,
kph.plan_header_code AS Header_Code,
ltrim(kpp.plan_phone_description,1,1) AS Phone_Desc
FROM dbo.kit_plan_phones kpp
INNER JOIN dbo.kit_plan_header kph
ON kpp.plan_phone_code = kph.plan_header_code

You can nest the functions calls.

Select Ltrim(Rtrim(colname)) AS NewCol
From Table Terry L. Broadbent
FAQ183-874 contains tips for posting questions in these forums.
NOTE: Reference to the FAQ is not directed at any individual.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top