How can I get a list of the column names in my table where the values for those columns are not NULL












1















Here is how I can get a list of the column names for the table



SELECT t.name AS table_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%tkrt%' and t.name = 'timerate'


So I get a list as such:



table_name  colunm_name
timerate tkrt01
timerate tkrt02
...
timerate tkrt50


Now in the actual table based on the tkinit and the tkeffdate I get this



select * from timerate where tkinit = '0005' and tkeffdate = 
(select MAX(tkeffdate) from timerate where tkinit = '0005')

tkinit tkeffdate tkrt01 tkrt02 ... tkrt22 ... tkrt50
0005 2014-01-01 00:00:00.000 440.00 NULL ... 400.00 ... NULL


so the return has some values for tkrt01, tkrt22... and some of the tkrt's are NULL



I was thinking if I use the first query to retun just a list of the colunm names (tkrt01...tkrt50) and if there is some way then to look through using another query using a variable from the loop that checks if each column and if it's null don't return it.
Basically I want a list of the column names that have values in them so my final result would be



column_name
tkrt01
tkrt22


As those are only the two columns that have values in them.



This isn't the case for everyone as I have two items to filter on being the tkinit and the tkeffdate



Each id (tkint) has a different max(tkeffdate). I have looked at doing For XML Path and For Each look but can't figure out the best way to just list the colunm names from the sys.tables join sys.columns where in the specific table (t.name) those respective columns (based on the two filters) have data in them










share|improve this question

























  • I think this logic is best handled elsewhere.

    – Marcus Adams
    Jan 8 '14 at 21:56
















1















Here is how I can get a list of the column names for the table



SELECT t.name AS table_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%tkrt%' and t.name = 'timerate'


So I get a list as such:



table_name  colunm_name
timerate tkrt01
timerate tkrt02
...
timerate tkrt50


Now in the actual table based on the tkinit and the tkeffdate I get this



select * from timerate where tkinit = '0005' and tkeffdate = 
(select MAX(tkeffdate) from timerate where tkinit = '0005')

tkinit tkeffdate tkrt01 tkrt02 ... tkrt22 ... tkrt50
0005 2014-01-01 00:00:00.000 440.00 NULL ... 400.00 ... NULL


so the return has some values for tkrt01, tkrt22... and some of the tkrt's are NULL



I was thinking if I use the first query to retun just a list of the colunm names (tkrt01...tkrt50) and if there is some way then to look through using another query using a variable from the loop that checks if each column and if it's null don't return it.
Basically I want a list of the column names that have values in them so my final result would be



column_name
tkrt01
tkrt22


As those are only the two columns that have values in them.



This isn't the case for everyone as I have two items to filter on being the tkinit and the tkeffdate



Each id (tkint) has a different max(tkeffdate). I have looked at doing For XML Path and For Each look but can't figure out the best way to just list the colunm names from the sys.tables join sys.columns where in the specific table (t.name) those respective columns (based on the two filters) have data in them










share|improve this question

























  • I think this logic is best handled elsewhere.

    – Marcus Adams
    Jan 8 '14 at 21:56














1












1








1








Here is how I can get a list of the column names for the table



SELECT t.name AS table_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%tkrt%' and t.name = 'timerate'


So I get a list as such:



table_name  colunm_name
timerate tkrt01
timerate tkrt02
...
timerate tkrt50


Now in the actual table based on the tkinit and the tkeffdate I get this



select * from timerate where tkinit = '0005' and tkeffdate = 
(select MAX(tkeffdate) from timerate where tkinit = '0005')

tkinit tkeffdate tkrt01 tkrt02 ... tkrt22 ... tkrt50
0005 2014-01-01 00:00:00.000 440.00 NULL ... 400.00 ... NULL


so the return has some values for tkrt01, tkrt22... and some of the tkrt's are NULL



I was thinking if I use the first query to retun just a list of the colunm names (tkrt01...tkrt50) and if there is some way then to look through using another query using a variable from the loop that checks if each column and if it's null don't return it.
Basically I want a list of the column names that have values in them so my final result would be



column_name
tkrt01
tkrt22


As those are only the two columns that have values in them.



This isn't the case for everyone as I have two items to filter on being the tkinit and the tkeffdate



Each id (tkint) has a different max(tkeffdate). I have looked at doing For XML Path and For Each look but can't figure out the best way to just list the colunm names from the sys.tables join sys.columns where in the specific table (t.name) those respective columns (based on the two filters) have data in them










share|improve this question
















Here is how I can get a list of the column names for the table



SELECT t.name AS table_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name LIKE '%tkrt%' and t.name = 'timerate'


So I get a list as such:



table_name  colunm_name
timerate tkrt01
timerate tkrt02
...
timerate tkrt50


Now in the actual table based on the tkinit and the tkeffdate I get this



select * from timerate where tkinit = '0005' and tkeffdate = 
(select MAX(tkeffdate) from timerate where tkinit = '0005')

tkinit tkeffdate tkrt01 tkrt02 ... tkrt22 ... tkrt50
0005 2014-01-01 00:00:00.000 440.00 NULL ... 400.00 ... NULL


so the return has some values for tkrt01, tkrt22... and some of the tkrt's are NULL



I was thinking if I use the first query to retun just a list of the colunm names (tkrt01...tkrt50) and if there is some way then to look through using another query using a variable from the loop that checks if each column and if it's null don't return it.
Basically I want a list of the column names that have values in them so my final result would be



column_name
tkrt01
tkrt22


As those are only the two columns that have values in them.



This isn't the case for everyone as I have two items to filter on being the tkinit and the tkeffdate



Each id (tkint) has a different max(tkeffdate). I have looked at doing For XML Path and For Each look but can't figure out the best way to just list the colunm names from the sys.tables join sys.columns where in the specific table (t.name) those respective columns (based on the two filters) have data in them







mysql sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 6:00









Joakim Danielson

7,9343724




7,9343724










asked Jan 8 '14 at 20:25









user3174897user3174897

61




61













  • I think this logic is best handled elsewhere.

    – Marcus Adams
    Jan 8 '14 at 21:56



















  • I think this logic is best handled elsewhere.

    – Marcus Adams
    Jan 8 '14 at 21:56

















I think this logic is best handled elsewhere.

– Marcus Adams
Jan 8 '14 at 21:56





I think this logic is best handled elsewhere.

– Marcus Adams
Jan 8 '14 at 21:56












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21005769%2fhow-can-i-get-a-list-of-the-column-names-in-my-table-where-the-values-for-those%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f21005769%2fhow-can-i-get-a-list-of-the-column-names-in-my-table-where-the-values-for-those%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation