SQL Query to Fetch only column with value equals to current date












0















Need SQL logic



I have table with Column name EID, Emp_name, 1,2,3,4,5,6,7....till 31 (Column 1 to 31 are calendar dates)
now i m trying to fetch only 3 column EID, EMP_name and date which is equals to sys date.



Example



Todays SYS_date is 2nd-Jan-2019



and i need column with value = 2 like this...



EID    Emp_name        2  |
123 James SCOTT P |
133 Mark M A |
133 Mark Man P |









share|improve this question




















  • 2





    2 solutions: UNPIVOT statement, or Dynamic SQL statement.

    – Thomas G
    Jan 2 at 11:28













  • anything will do, but prefer both solutions

    – J.James
    Jan 2 at 11:30











  • @ThomasG it did not solved my problem :(

    – J.James
    Jan 2 at 11:42
















0















Need SQL logic



I have table with Column name EID, Emp_name, 1,2,3,4,5,6,7....till 31 (Column 1 to 31 are calendar dates)
now i m trying to fetch only 3 column EID, EMP_name and date which is equals to sys date.



Example



Todays SYS_date is 2nd-Jan-2019



and i need column with value = 2 like this...



EID    Emp_name        2  |
123 James SCOTT P |
133 Mark M A |
133 Mark Man P |









share|improve this question




















  • 2





    2 solutions: UNPIVOT statement, or Dynamic SQL statement.

    – Thomas G
    Jan 2 at 11:28













  • anything will do, but prefer both solutions

    – J.James
    Jan 2 at 11:30











  • @ThomasG it did not solved my problem :(

    – J.James
    Jan 2 at 11:42














0












0








0








Need SQL logic



I have table with Column name EID, Emp_name, 1,2,3,4,5,6,7....till 31 (Column 1 to 31 are calendar dates)
now i m trying to fetch only 3 column EID, EMP_name and date which is equals to sys date.



Example



Todays SYS_date is 2nd-Jan-2019



and i need column with value = 2 like this...



EID    Emp_name        2  |
123 James SCOTT P |
133 Mark M A |
133 Mark Man P |









share|improve this question
















Need SQL logic



I have table with Column name EID, Emp_name, 1,2,3,4,5,6,7....till 31 (Column 1 to 31 are calendar dates)
now i m trying to fetch only 3 column EID, EMP_name and date which is equals to sys date.



Example



Todays SYS_date is 2nd-Jan-2019



and i need column with value = 2 like this...



EID    Emp_name        2  |
123 James SCOTT P |
133 Mark M A |
133 Mark Man P |






sql tsql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 11:24









jarlh

29.8k52138




29.8k52138










asked Jan 2 at 11:23









J.JamesJ.James

65




65








  • 2





    2 solutions: UNPIVOT statement, or Dynamic SQL statement.

    – Thomas G
    Jan 2 at 11:28













  • anything will do, but prefer both solutions

    – J.James
    Jan 2 at 11:30











  • @ThomasG it did not solved my problem :(

    – J.James
    Jan 2 at 11:42














  • 2





    2 solutions: UNPIVOT statement, or Dynamic SQL statement.

    – Thomas G
    Jan 2 at 11:28













  • anything will do, but prefer both solutions

    – J.James
    Jan 2 at 11:30











  • @ThomasG it did not solved my problem :(

    – J.James
    Jan 2 at 11:42








2




2





2 solutions: UNPIVOT statement, or Dynamic SQL statement.

– Thomas G
Jan 2 at 11:28







2 solutions: UNPIVOT statement, or Dynamic SQL statement.

– Thomas G
Jan 2 at 11:28















anything will do, but prefer both solutions

– J.James
Jan 2 at 11:30





anything will do, but prefer both solutions

– J.James
Jan 2 at 11:30













@ThomasG it did not solved my problem :(

– J.James
Jan 2 at 11:42





@ThomasG it did not solved my problem :(

– J.James
Jan 2 at 11:42












2 Answers
2






active

oldest

votes


















0














Try like this:



declare @sql nvarchar(max)
set @sql = 'select EID, Emp_name, [' + convert(nvarchar(2),day(getdate())) + '] as d from tableName '

exec(@sql)





share|improve this answer


























  • i m getting error Conversion failed when converting the varchar value 'select EID, ' to data type int.

    – J.James
    Jan 2 at 11:38













  • @J.James edited, try now

    – claud.io
    Jan 2 at 11:43











  • i tried this but i m getting values 2,2,2,2,2,2 in the rows for column d but i want associated values from column [2] and in rows P, A,P, WO,

    – J.James
    Jan 2 at 12:16











  • You should surround the "day" column with square brackets, because it is a number, which is invalid column name and will select the number itself, not the contents of the column with this name. I.e. set @sql = 'select EID, Emp_name, [' + convert(nvarchar(2),day(getdate())) + '] as d from tableName'

    – Andrey Nikolov
    Jan 2 at 12:17











  • @AndreyNikolov....Thank you brother it helped

    – J.James
    Jan 2 at 12:29



















0














One method to approach this is to unpivot the data. Here is one method:



select t.eid, t.emp_name, v.n
from t cross apply
(values ([1]), ([2]), ([3]), . . ., ([4])
) v(n)
where v.n = day(getdate());


I will caution that the column name is a fixed name, not 2. SQL queries have fixed column names. You would need to use dynamic sql to get a variable column name.






share|improve this answer


























  • this is what i want but i m getting error invalid column name 'n'

    – J.James
    Jan 2 at 12:03













  • can you please get back to me for this..

    – J.James
    Jan 2 at 12:18











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%2f54005453%2fsql-query-to-fetch-only-column-with-value-equals-to-current-date%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Try like this:



declare @sql nvarchar(max)
set @sql = 'select EID, Emp_name, [' + convert(nvarchar(2),day(getdate())) + '] as d from tableName '

exec(@sql)





share|improve this answer


























  • i m getting error Conversion failed when converting the varchar value 'select EID, ' to data type int.

    – J.James
    Jan 2 at 11:38













  • @J.James edited, try now

    – claud.io
    Jan 2 at 11:43











  • i tried this but i m getting values 2,2,2,2,2,2 in the rows for column d but i want associated values from column [2] and in rows P, A,P, WO,

    – J.James
    Jan 2 at 12:16











  • You should surround the "day" column with square brackets, because it is a number, which is invalid column name and will select the number itself, not the contents of the column with this name. I.e. set @sql = 'select EID, Emp_name, [' + convert(nvarchar(2),day(getdate())) + '] as d from tableName'

    – Andrey Nikolov
    Jan 2 at 12:17











  • @AndreyNikolov....Thank you brother it helped

    – J.James
    Jan 2 at 12:29
















0














Try like this:



declare @sql nvarchar(max)
set @sql = 'select EID, Emp_name, [' + convert(nvarchar(2),day(getdate())) + '] as d from tableName '

exec(@sql)





share|improve this answer


























  • i m getting error Conversion failed when converting the varchar value 'select EID, ' to data type int.

    – J.James
    Jan 2 at 11:38













  • @J.James edited, try now

    – claud.io
    Jan 2 at 11:43











  • i tried this but i m getting values 2,2,2,2,2,2 in the rows for column d but i want associated values from column [2] and in rows P, A,P, WO,

    – J.James
    Jan 2 at 12:16











  • You should surround the "day" column with square brackets, because it is a number, which is invalid column name and will select the number itself, not the contents of the column with this name. I.e. set @sql = 'select EID, Emp_name, [' + convert(nvarchar(2),day(getdate())) + '] as d from tableName'

    – Andrey Nikolov
    Jan 2 at 12:17











  • @AndreyNikolov....Thank you brother it helped

    – J.James
    Jan 2 at 12:29














0












0








0







Try like this:



declare @sql nvarchar(max)
set @sql = 'select EID, Emp_name, [' + convert(nvarchar(2),day(getdate())) + '] as d from tableName '

exec(@sql)





share|improve this answer















Try like this:



declare @sql nvarchar(max)
set @sql = 'select EID, Emp_name, [' + convert(nvarchar(2),day(getdate())) + '] as d from tableName '

exec(@sql)






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 13:21

























answered Jan 2 at 11:31









claud.ioclaud.io

406212




406212













  • i m getting error Conversion failed when converting the varchar value 'select EID, ' to data type int.

    – J.James
    Jan 2 at 11:38













  • @J.James edited, try now

    – claud.io
    Jan 2 at 11:43











  • i tried this but i m getting values 2,2,2,2,2,2 in the rows for column d but i want associated values from column [2] and in rows P, A,P, WO,

    – J.James
    Jan 2 at 12:16











  • You should surround the "day" column with square brackets, because it is a number, which is invalid column name and will select the number itself, not the contents of the column with this name. I.e. set @sql = 'select EID, Emp_name, [' + convert(nvarchar(2),day(getdate())) + '] as d from tableName'

    – Andrey Nikolov
    Jan 2 at 12:17











  • @AndreyNikolov....Thank you brother it helped

    – J.James
    Jan 2 at 12:29



















  • i m getting error Conversion failed when converting the varchar value 'select EID, ' to data type int.

    – J.James
    Jan 2 at 11:38













  • @J.James edited, try now

    – claud.io
    Jan 2 at 11:43











  • i tried this but i m getting values 2,2,2,2,2,2 in the rows for column d but i want associated values from column [2] and in rows P, A,P, WO,

    – J.James
    Jan 2 at 12:16











  • You should surround the "day" column with square brackets, because it is a number, which is invalid column name and will select the number itself, not the contents of the column with this name. I.e. set @sql = 'select EID, Emp_name, [' + convert(nvarchar(2),day(getdate())) + '] as d from tableName'

    – Andrey Nikolov
    Jan 2 at 12:17











  • @AndreyNikolov....Thank you brother it helped

    – J.James
    Jan 2 at 12:29

















i m getting error Conversion failed when converting the varchar value 'select EID, ' to data type int.

– J.James
Jan 2 at 11:38







i m getting error Conversion failed when converting the varchar value 'select EID, ' to data type int.

– J.James
Jan 2 at 11:38















@J.James edited, try now

– claud.io
Jan 2 at 11:43





@J.James edited, try now

– claud.io
Jan 2 at 11:43













i tried this but i m getting values 2,2,2,2,2,2 in the rows for column d but i want associated values from column [2] and in rows P, A,P, WO,

– J.James
Jan 2 at 12:16





i tried this but i m getting values 2,2,2,2,2,2 in the rows for column d but i want associated values from column [2] and in rows P, A,P, WO,

– J.James
Jan 2 at 12:16













You should surround the "day" column with square brackets, because it is a number, which is invalid column name and will select the number itself, not the contents of the column with this name. I.e. set @sql = 'select EID, Emp_name, [' + convert(nvarchar(2),day(getdate())) + '] as d from tableName'

– Andrey Nikolov
Jan 2 at 12:17





You should surround the "day" column with square brackets, because it is a number, which is invalid column name and will select the number itself, not the contents of the column with this name. I.e. set @sql = 'select EID, Emp_name, [' + convert(nvarchar(2),day(getdate())) + '] as d from tableName'

– Andrey Nikolov
Jan 2 at 12:17













@AndreyNikolov....Thank you brother it helped

– J.James
Jan 2 at 12:29





@AndreyNikolov....Thank you brother it helped

– J.James
Jan 2 at 12:29













0














One method to approach this is to unpivot the data. Here is one method:



select t.eid, t.emp_name, v.n
from t cross apply
(values ([1]), ([2]), ([3]), . . ., ([4])
) v(n)
where v.n = day(getdate());


I will caution that the column name is a fixed name, not 2. SQL queries have fixed column names. You would need to use dynamic sql to get a variable column name.






share|improve this answer


























  • this is what i want but i m getting error invalid column name 'n'

    – J.James
    Jan 2 at 12:03













  • can you please get back to me for this..

    – J.James
    Jan 2 at 12:18
















0














One method to approach this is to unpivot the data. Here is one method:



select t.eid, t.emp_name, v.n
from t cross apply
(values ([1]), ([2]), ([3]), . . ., ([4])
) v(n)
where v.n = day(getdate());


I will caution that the column name is a fixed name, not 2. SQL queries have fixed column names. You would need to use dynamic sql to get a variable column name.






share|improve this answer


























  • this is what i want but i m getting error invalid column name 'n'

    – J.James
    Jan 2 at 12:03













  • can you please get back to me for this..

    – J.James
    Jan 2 at 12:18














0












0








0







One method to approach this is to unpivot the data. Here is one method:



select t.eid, t.emp_name, v.n
from t cross apply
(values ([1]), ([2]), ([3]), . . ., ([4])
) v(n)
where v.n = day(getdate());


I will caution that the column name is a fixed name, not 2. SQL queries have fixed column names. You would need to use dynamic sql to get a variable column name.






share|improve this answer















One method to approach this is to unpivot the data. Here is one method:



select t.eid, t.emp_name, v.n
from t cross apply
(values ([1]), ([2]), ([3]), . . ., ([4])
) v(n)
where v.n = day(getdate());


I will caution that the column name is a fixed name, not 2. SQL queries have fixed column names. You would need to use dynamic sql to get a variable column name.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 2 at 12:53

























answered Jan 2 at 11:46









Gordon LinoffGordon Linoff

790k35314418




790k35314418













  • this is what i want but i m getting error invalid column name 'n'

    – J.James
    Jan 2 at 12:03













  • can you please get back to me for this..

    – J.James
    Jan 2 at 12:18



















  • this is what i want but i m getting error invalid column name 'n'

    – J.James
    Jan 2 at 12:03













  • can you please get back to me for this..

    – J.James
    Jan 2 at 12:18

















this is what i want but i m getting error invalid column name 'n'

– J.James
Jan 2 at 12:03







this is what i want but i m getting error invalid column name 'n'

– J.James
Jan 2 at 12:03















can you please get back to me for this..

– J.James
Jan 2 at 12:18





can you please get back to me for this..

– J.James
Jan 2 at 12:18


















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%2f54005453%2fsql-query-to-fetch-only-column-with-value-equals-to-current-date%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

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

SQL update select statement

'app-layout' is not a known element: how to share Component with different Modules