Kumar AU

Kumar AU

  • 1.4k
  • 320
  • 63k

Help Required on SQL search the the fields till space/-

Jun 28 2016 11:03 AM
Dear All,
Could you please help me with the below query :-
Create Table #Users1
(
UniQueNumber Varchar(50),
FName varchar (50),
LName Varchar (50),
Number varchar(10),
CreateDate Datetime
)
INSERT INTO #Users1 Values ('44354','Kumar','Shruthi-hh','11111','05-25-2016 00:10:000')
INSERT INTO #Users1 Values ('44254','Shankar','Raj','22222','05-25-2016 01:18:000')
INSERT INTO #Users1 Values ('34154','Babu','Swamy k','33333','05-25-2016 06:10:000')
INSERT INTO #Users1 Values ('35054','Mani','Kanta H','44444','06-25-2016 06:10:000')
I need My Result should be :- Here I am not understaning how to get the 'UniQueNumber' column values AS shows below
UniQueNumber Fname LName
44354-KShruthi Kumar Shruthi-hh
44254-SRaj Shankar Raj
34154-BSwamy Babu Swamy k
35054-MKanta Mani Kanta H
My Query :-
SELECT (UniQueNumber + '-' + SUBSTRING(FName,1,1) + CASE WHEN PATINDEX('%-%',LName) > 0 THEN SUBSTRING(LName,0,PATINDEX('%-%',LName)) ELSE LName END) as UniQueNumber
,FName
,LName
,Number
,CreateDate
FROM #Users2
Here I am not understanding how to manage the space as well in the above query

Answers (4)