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 reomve the in the query

Author  Topic 

mohan123
Constraint Violating Yak Guru

252 Posts

Posted - 2014-02-17 : 05:14:49
Hi i have wriiten a query to get the latest modified objects in Database and my problem is it is giving beacuse of case condition

how to get the distinct of all Columns


Name
Raj
NULL
Raju
NULL
NULL
Ram


required output :

Name
Raj
Raju
Ram


here is my query

SELECT CASE WHEN type = 'TT' THEN name ELSE '' END AS TableType,
CASE WHEN type = 'P' THEN name ELSE '' END AS Procedures,
CASE WHEN type = 'FN' THEN name ELSE '' END AS Functions,
CASE WHEN type = 'U' THEN name ELSE '' END AS Tables,
CASE WHEN type = 'V' THEN name ELSE '' END AS Views,
CASE WHEN type = 'TR' THEN name ELSE '' END AS Triggers
FROM sys.objects
WHERE type IN ('TT','FN','U','V','P','TR')
AND DATEDIFF(D,modify_date,GETDATE())< 5


P.V.P.MOhan

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-17 : 05:20:09
[code]
SELECT DISTINCT CASE WHEN type = 'TT' THEN name ELSE '' END AS TableType,
CASE WHEN type = 'P' THEN name ELSE '' END AS Procedures,
CASE WHEN type = 'FN' THEN name ELSE '' END AS Functions,
CASE WHEN type = 'U' THEN name ELSE '' END AS Tables,
CASE WHEN type = 'V' THEN name ELSE '' END AS Views,
CASE WHEN type = 'TR' THEN name ELSE '' END AS Triggers
FROM sys.objects
WHERE type IN ('TT','FN','U','V','P','TR')
AND DATEDIFF(D,modify_date,GETDATE())< 5


[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

mohan123
Constraint Violating Yak Guru

252 Posts

Posted - 2014-02-17 : 05:31:49
Please see in my question whatever the query i posted you gave me the same answer in this query i just want to remove the null spaces .
;WITH CTE AS
(
SELECT CASE WHEN type = 'TT' THEN name ELSE NULL END AS TableType,
CASE WHEN type = 'P' THEN name ELSE NULL END AS Procedures,
CASE WHEN type = 'FN' THEN name ELSE NULL END AS Functions,
CASE WHEN type = 'U' THEN name ELSE NULL END AS Tables,
CASE WHEN type = 'V' THEN name ELSE NULL END AS Views,
CASE WHEN type = 'TR' THEN name ELSE NULL END AS Triggers
FROM sys.objects
WHERE type IN ('TT','FN','U','V','P','TR')
AND DATEDIFF(D,modify_date,GETDATE())< 5

)
Select ISNULL(CONVERT(VARCHAR(MAX),TableType),0),
ISNULL(CONVERT(VARCHAR(MAX),Procedures),0),
ISNULL(CONVERT(VARCHAR(MAX),Functions),0),
ISNULL(CONVERT(VARCHAR(MAX),Tables),0),
ISNULL(CONVERT(VARCHAR(MAX),Views),0),
ISNULL(CONVERT(VARCHAR(MAX),Triggers),0) FROm CTE WHERE Tables <> '0'


P.V.P.MOhan
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-17 : 06:09:12
quote:
Originally posted by mohan123

Please see in my question whatever the query i posted you gave me the same answer in this query i just want to remove the null spaces .
;WITH CTE AS
(
SELECT CASE WHEN type = 'TT' THEN name ELSE NULL END AS TableType,
CASE WHEN type = 'P' THEN name ELSE NULL END AS Procedures,
CASE WHEN type = 'FN' THEN name ELSE NULL END AS Functions,
CASE WHEN type = 'U' THEN name ELSE NULL END AS Tables,
CASE WHEN type = 'V' THEN name ELSE NULL END AS Views,
CASE WHEN type = 'TR' THEN name ELSE NULL END AS Triggers
FROM sys.objects
WHERE type IN ('TT','FN','U','V','P','TR')
AND DATEDIFF(D,modify_date,GETDATE())< 5

)
Select ISNULL(CONVERT(VARCHAR(MAX),TableType),0),
ISNULL(CONVERT(VARCHAR(MAX),Procedures),0),
ISNULL(CONVERT(VARCHAR(MAX),Functions),0),
ISNULL(CONVERT(VARCHAR(MAX),Tables),0),
ISNULL(CONVERT(VARCHAR(MAX),Views),0),
ISNULL(CONVERT(VARCHAR(MAX),Triggers),0) FROm CTE WHERE Tables <> '0'


P.V.P.MOhan


nope you asked for DISTINCT and I did exactly that
Unless you give us proper sample data to work with and explain what you want this is all we can do
How do I know what according to you represent duplicate and what you expect as output
Give us the detail and then somebody might be able to give you exact solution you're after

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

mohan123
Constraint Violating Yak Guru

252 Posts

Posted - 2014-02-17 : 08:02:00
i have applied union all to get all the result in single column and thanks visakh for your valuable suggestions
;WITH CTE AS
(
SELECT CASE WHEN type = 'TT' THEN name ELSE NULL END AS TableType,
CASE WHEN type = 'P' THEN 'SP_HELPTEXT' +''''+ name +'''' ELSE NULL END AS Procedures,
CASE WHEN type = 'FN' THEN 'SP_HELPTEXT' +''''+ name +'''' ELSE NULL END AS Functions,
CASE WHEN type = 'U' THEN name ELSE NULL END AS Tables,
CASE WHEN type = 'V' THEN 'SP_HELPTEXT' +''''+ name +'''' ELSE NULL END AS Views,
CASE WHEN type = 'TR' THEN 'SP_HELPTEXT' +''''+ name +'''' ELSE NULL END AS Triggers
FROM sys.objects
WHERE type IN ('TT','FN','U','V','P','TR')
AND DATEDIFF(D,modify_date,GETDATE())< 5
)

Select ISNULL(CONVERT(VARCHAR(MAX),TableType),0) , 'TABLEYPES' From CTE
WHERE TableType <> '0'
UNION ALL
Select ISNULL(CONVERT(VARCHAR(MAX),Procedures),0) , 'Procedures' From CTE
WHERE Procedures <> '0'
UNION ALL
Select ISNULL(CONVERT(VARCHAR(MAX),Functions),0) , 'Functions' From CTE
WHERE Functions <> '0'
UNION ALL
Select ISNULL(CONVERT(VARCHAR(MAX),Tables),0) , 'Tables' From CTE
WHERE Tables <> '0'
UNION ALL
Select ISNULL(CONVERT(VARCHAR(MAX),Views),0) , 'Views' From CTE
WHERE Views <> '0'
UNION ALL
Select ISNULL(CONVERT(VARCHAR(MAX),Triggers),0) , 'Triggers' From CTE
WHERE Triggers <> '0'

P.V.P.MOhan
Go to Top of Page
   

- Advertisement -