SQL Query to Fetch only column with value equals to current date
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
add a comment |
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
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
add a comment |
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
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
sql tsql
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
Try like this:
declare @sql nvarchar(max)
set @sql = 'select EID, Emp_name, [' + convert(nvarchar(2),day(getdate())) + '] as d from tableName '
exec(@sql)
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
|
show 5 more comments
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Try like this:
declare @sql nvarchar(max)
set @sql = 'select EID, Emp_name, [' + convert(nvarchar(2),day(getdate())) + '] as d from tableName '
exec(@sql)
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
|
show 5 more comments
Try like this:
declare @sql nvarchar(max)
set @sql = 'select EID, Emp_name, [' + convert(nvarchar(2),day(getdate())) + '] as d from tableName '
exec(@sql)
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
|
show 5 more comments
Try like this:
declare @sql nvarchar(max)
set @sql = 'select EID, Emp_name, [' + convert(nvarchar(2),day(getdate())) + '] as d from tableName '
exec(@sql)
Try like this:
declare @sql nvarchar(max)
set @sql = 'select EID, Emp_name, [' + convert(nvarchar(2),day(getdate())) + '] as d from tableName '
exec(@sql)
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
|
show 5 more comments
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
|
show 5 more comments
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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