T-SQL - substring - LEN

Generic1

Benutzer
Beiträge
11
Hallo,

ich habe ein furchtbares SQL- Statement zusammengestellt, welches funktioniert aber eben sehr unübersichtlich ist.
Meine Frage wäre jetzt, muss ich, um die Länge zu bestimmen, nochmals das gleiche SQL- Statement verwenden oder gibt es eine Möglichkeit, aus dem ersten Sub- SELECT die länge rauszubekommen und diese dann in SUBSTRING(x,y, LEN) -> LEN einsetzen?

Vielen Dank!


SELECT
employee.ID,
employee.FIRSTNAME,
employee.MIDDLENAME,
employee.LASTNAME,
substring(
(
SELECT ',' + phone.NUMBER AS [text()]
FROM [Intranet_Inside_cons].[dbo].[EMPLOYEE] AS employee1 LEFT JOIN [Intranet_Inside_cons].[dbo].[PHONE] AS phone ON employee1.ID = phone.EMPLOYEE_ID
WHERE employee1.ID = employee.ID
ORDER BY employee1.ID
FOR XML PATH ('')
), 2, LEN(
(
SELECT ',' + phone.NUMBER AS [text()]
FROM [Intranet_Inside_cons].[dbo].[EMPLOYEE] AS employee1 LEFT JOIN [Intranet_Inside_cons].[dbo].[PHONE] AS phone ON employee1.ID = phone.EMPLOYEE_ID
WHERE employee1.ID = employee.ID
ORDER BY employee1.ID
FOR XML PATH ('')
)
)) AS [PHONENUMBERS]
FROM [Intranet_Inside_cons].[dbo].[EMPLOYEE] AS employee
 
Werbung:
Sub-Select hat einmal funktioniert... Warum sollte es das nicht auch ein zweites mal? (Kann sein das du Aliase vergeben musst... Bin aus der Oracle-Ecke :) )
Code:
Select id
      ,firstname
      ,middlename
      ,lastname
      ,substring(yvalue, 2, len(yvalue))
From   (Select employee.id
              ,employee.firstname
              ,employee.middlename
              ,employee.lastname
              ,(Select ',' + phone.number As [ text() ]
                From   [ intranet_inside_cons ] . [ dbo ] . [ employee ] As employee1
                Left   Join [ intranet_inside_cons ] . [ dbo ] . [ phone ] As phone
                On     employee1.id = phone.employee_id
                Where  employee1.id = employee.id
                Order  By employee1.id
                For    xml path('')) As yvalue
        From   [ intranet_inside_cons ] . [ dbo ] . [ employee ] As employee)
 
Aus reiner Neugierde... Wäre das hier nicht "effizienter" ? (Vllt. als kleine Anmerkung... Ich hab keine Ahnung was und warum du das da machst...)

Code:
Select id
      ,firstname
      ,middlename
      ,lastname
      ,substring(yvalue, 2, len(yvalue))
From   (Select employee.id
              ,employee.firstname
              ,employee.middlename
              ,employee.lastname
              ,(Select ',' + phone.number As [text()]
                From   [intranet_inside_cons].[dbo].[phone] As phone
                On     employee.id = phone.employee_id
                Order  By phone.id
                For    xml path('')) As yvalue
        From   [intranet_inside_cons].[dbo].[employee] As employee)
 
Aus reiner Neugierde... Wäre das hier nicht "effizienter" ? (Vllt. als kleine Anmerkung... Ich hab keine Ahnung was und warum du das da machst...)

Code:
Select id
      ,firstname
      ,middlename
      ,lastname
      ,substring(yvalue, 2, len(yvalue))
From   (Select employee.id
              ,employee.firstname
              ,employee.middlename
              ,employee.lastname
              ,(Select ',' + phone.number As [text()]
                From   [intranet_inside_cons].[dbo].[phone] As phone
                On     employee.id = phone.employee_id
                Order  By phone.id
                For    xml path('')) As yvalue
        From   [intranet_inside_cons].[dbo].[employee] As employee)


Hallo,

besten Dank für die Antworten,
dein SQL- Statement würde schon hinkommen, mein Problem ist nur, dass es noch nicht funktioniert für MSSQL:


Msg 156, Level 15, State 1, Line 12
Falsche Syntax in der Nähe des On-Schlüsselworts.

Wie kann ich diese Fehlermeldung noch verhindern? Das mit den Aliase ist mir noch nicht ganz klar.
Das SUBSelect alleine funktioniert einwandfrei uns liefert auch shcon das, was es liefern soll - bis auf den Beistrich am Anfang.

Vielen Dank!
 
Werbung:
Hallo zusammen,

so wie ich die Abfrage interpretiere geht es nur darum aus dem Subselect das erste Zeichen zu eliminieren, da dies ein Trennzeichen (",") ist.
In diesem Fall funktioniert die Abfrage auch so:

Code:
SELECT
employee.ID,
employee.FIRSTNAME,
employee.MIDDLENAME,
employee.LASTNAME,
STUFF(
(
SELECT ',' + phone.NUMBER AS [text()]
FROM [Intranet_Inside_cons].[dbo].[EMPLOYEE] AS employee1 LEFT JOIN [Intranet_Inside_cons].[dbo].[PHONE] AS phone ON employee1.ID = phone.EMPLOYEE_ID
WHERE employee1.ID = employee.ID
ORDER BY employee1.ID
FOR XML PATH ('')), 1, 1, '') AS [PHONENUMBERS]
FROM [Intranet_Inside_cons].[dbo].[EMPLOYEE] AS employee

Viele Grüße,
Tommi
 
Zurück
Oben