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.
Author |
Topic |
KilpAr
Yak Posting Veteran
80 Posts |
Posted - 2013-04-10 : 09:29:09
|
I have a problem where I have defined project numbers like a/b. Now in the database I have where invoices are listed by either a or b.I need to receive the first invoice date, but I can't figure how to deal with the project numbers.I'm trying with: DECLARE @ProjectID varchar(64); SET @ProjectID = 'a/b'; SELECT MIN(Date_) AS startdate FROM mydatabase WHERE projectnumber IN( SELECT '''' + REPLACE ( @ProjectID, '/' , ''',''' ) + '''' ) It returns NULL.If I replace the in clause with'a','b'it works immediately.I believe I need some split type function, but what's the smartest way to go from here? |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-04-10 : 09:48:45
|
[code]-- CustomSplit UDFCREATE FUNCTION [dbo].[CustomSplit] (@sep char(1), @s varchar(4000))RETURNS tableASRETURN (WITH Pieces(n, start, stop) AS (SELECT 1, 1, CHARINDEX(@sep, @s)UNION ALLSELECT n + 1, stop + 1, CHARINDEX(@sep, @s, stop + 1)FROM PiecesWHERE stop > 0)SELECT n,SUBSTRING(@s, start, CASE WHEN stop > 0 THEN stop-start ELSE 4000 END) AS ValFROM Pieces)GODECLARE @ProjectID varchar(64);SET @ProjectID = 'AD_VP/SA_REP';SELECT MIN(HIRE_DATE) AS startdate FROM EMPLOYEES WHERE JOB_ID IN( SELECT Val FROM CustomSplit('/', @ProjectID))[/code] |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-04-10 : 09:52:45
|
--Alternate is Note: This is feasible for small set of values as well as delimiter should be / without any spacesDECLARE @ProjectID varchar(64);SET @ProjectID = 'AD_VP/SA_REP';SELECT MIN((Date_) AS startdate FROM mydatabase WHERE '/'+@ProjectID+'/' LIKE '%/'+projectnumber+'/%' |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
KilpAr
Yak Posting Veteran
80 Posts |
Posted - 2013-04-17 : 08:15:22
|
Thanks, I went with that UDF, works nicely! |
|
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2013-04-17 : 08:17:58
|
quote: Originally posted by KilpAr Thanks, I went with that UDF, works nicely!
Welcome--Chandu |
|
|
|
|
|
|
|