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 thisHH20060831160342DDasb IT 3000FF20060831160709000000001Where '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 salunion allselect 'DD'+'',filename,dept,sal from emp where empno like '%1%'union allselect '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 asHH20060831161226 FF20060831161226000000000 if the second select statement has no recordsPlease help |
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-08-31 : 04:41:11
|
for this queryselect 'DD'+'',filename,dept,sal from emp where empno like '%1%' shouldn't it ibeselect 'DD' + filename + dept + convert(varchar(10), sal) from emp KH |
|
|
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 LarssonHelsingborg, Sweden |
|
|
asbharadwaj
Starting Member
27 Posts |
Posted - 2006-08-31 : 05:48:57
|
Thanks alot |
|
|
|
|
|