Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 [Resolved] Need to substring a field

Author  Topic 

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-05-11 : 15:24:40
I have a proc where I need to substring the field dbo.Batch.Name (char 50).
Rule is: Need to substring from position 1 up till the 1st "-" like

RGWGSWD-391 THU 5/7/09 -> RGWGSWD
RGWGJO-189 THU 5/7/09 -> RGWGJO
RGS-250 MON 5/11/09 -> RGS

Here is my code:

SELECT           dbo.Job.CompanyJobId as job,
dbo.Item.CompanyItemId as costcode,
dbo.Batch.ReportDate as jobdate,
dbo.Equipment.CompanyEquipmentID as equipmentID,
substring(dbo.Equipment.Name, 1, 20) as equipmentname,
dbo.EquipmentLaborEvent.Hours as hours,
dbo.Batch.Name as foreman,
substring (e2.UserName, 1, 10) as approver

FROM dbo.EquipmentLaborEvent
inner join dbo.Equipment on dbo.Equipment.EquipmentGuid = dbo.EquipmentLaborEvent.EquipmentGuid
inner join dbo.Event on dbo.Event.EventGuid = dbo.EquipmentLaborEvent.EventGuid
inner join dbo.Job on dbo.Job.JobGuid = dbo.Event.JobGuid
inner join dbo.Item on dbo.Item.ItemGuid = dbo.Event.ItemGuid
inner join dbo.Batch on dbo.Batch.BatchGuid = dbo.Event.BatchGuid
inner join dbo.Region on dbo.Region.RegionGuid = Batch.RegionGuid
left join dbo.Account e1 on e1.AccountGuid = dbo.Event.AccountGuid
left join dbo.Account e2 on e2.AccountGuid = dbo.Event.ApprovedByAccountGuid

WHERE (@Division IS Null OR dbo.Region.CompanyRegionID = @Division) and
(@JobNumber IS Null or dbo.Job.CompanyJobID = @JobNumber) and
(@EquipmentID IS Null or dbo.Equipment.CompanyEquipmentID = @EquipmentID) and
(@DateFrom IS Null OR dbo.Batch.Reportdate >= @DateFrom) and
(@DateTo IS Null OR dbo.Batch.ReportDate <= @DateTo)

GROUP BY dbo.Job.CompanyJobID, dbo.Item.CompanyItemID, dbo.Batch.ReportDate,
dbo.Equipment.CompanyEquipmentID, dbo.Equipment.Name, dbo.EquipmentLaborEvent.Hours, e1.UserName, e2.UserName

ORDER BY dbo.Batch.ReportDate, dbo.Job.CompanyJobID, dbo.Item.CompanyItemID



robvolk
Most Valuable Yak

15732 Posts

Posted - 2009-05-11 : 15:34:52
Use: substring (e2.UserName, 1, charindex('-',e2.UserName)-1)
Go to Top of Page

snufse
Constraint Violating Yak Guru

469 Posts

Posted - 2009-05-11 : 17:00:49
This worked fine. Thank you.
Go to Top of Page
   

- Advertisement -