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
 General SQL Server Forums
 New to SQL Server Programming
 How to create a file with Header, details & footer

Author  Topic 

asbharadwaj
Starting Member

27 Posts

Posted - 2006-08-31 : 04:16:33
Hi All,

I need to create a query which gives me something like this

HH20060831160342
DDasb IT 3000
FF20060831160709000000001


Where 'HH' is the header(followed by Date and time) and 'FF' is the footer (followed by Date, time and no of records)and 'DD' has some details (few fields) from database.I am using UNION to get this result but the problem is that if the count in the footer is 0 then query should not give any output.but If I am using the following query
select 'HH'+convert(varchar,getDATE(),112)+replace(convert(varchar,getdate(),8),':','') as filename,'' as name,'' as dept,'' as sal
union all
select 'DD'+'',filename,dept,sal from emp where empno like '%1%'
union all
select 'FF'+convert(varchar,getDATE(),112)+replace(convert(varchar,getdate(),8),':','')+ REPLICATE(0, 9-len(COUNT(*)))+''+convert(VARchar(10),COUNT(*)) as filename,'' as name,'' as dept,'' as sal from emp where empno like '%1%'


I am getting the result as


HH20060831161226
FF20060831161226000000000
if the second select statement has no records

Please help

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2006-08-31 : 04:41:11
for this query
select 'DD'+'',filename,dept,sal from emp where empno like '%1%'

shouldn't it ibe
select 'DD' + filename + dept + convert(varchar(10), sal) from emp



KH

Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2006-08-31 : 05:22:50
Or something like this?
IF EXISTS (SELECT * FROM Emp WHERE EmpNo LIKE '%1%')
SELECT 'HH' + CONVERT(varchar, GETDATE(), 112) + REPLACE(CONVERT(varchar, GETDATE(), 8), ':', '') AS Filename,
'' AS Name,
'' AS Dept,
'' AS Sal
UNION ALL
SELECT 'DD',
ISNULL(Filename, ''),
ISNULL(Dept, ''),
ISNULL(Sal, '')
FROM Emp
WHERE EmpNo LIKE '%1%'
UNION ALL
SELECT 'FF' + CONVERT(varchar, GETDATE(), 112) + REPLACE(CONVERT(varchar, GETDATE(), 8), ':', '') + RIGHT('0000000000' + CONVERT(varchar, COUNT(*)), 10)
'',
'',
''
FROM Emp
WHERE EmpNo LIKE '%1%'

Peter Larsson
Helsingborg, Sweden
Go to Top of Page

asbharadwaj
Starting Member

27 Posts

Posted - 2006-08-31 : 05:48:57
Thanks alot
Go to Top of Page
   

- Advertisement -