Converting varchar values that were derived from a constant field to Month and Year
I have a query where I am grabbing certain text out of a string. An example of the string is:
"Captured Credit Card: MasterCard, xxxx-xxxx-xxxx-xxxx, 09/19. Set as Default."
My query is pulling the expiration month and year out of that text.
SUBSTRING(ADT.MsgText, CHARINDEX('/', ADT.MsgText, 0) -2, 2) AS [Month]
,'20' + SUBSTRING(ADT.MsgText, CHARINDEX('/', ADT.MsgText, 0) + 1, 2) AS [Year]
However, I need these number values to be recognized as date values. I tried the following method, but was unsuccessful:
UPDATE CCC
SET CCC.[Month] = convert(date, convert(varchar(4), CCC.[Year]) + ' ' + convert(varchar(2), CCC.[Month]))
FROM CC_Captured CCC
The error message I get is: "Update or insert of view or function 'CCC' failed because it contains a derived or constant field."
I'm wondering how to convert these values to be date?
Any help is appreciated!
(Also I am using SQL 2016)
sql date sql-server-2016 varchar
add a comment |
I have a query where I am grabbing certain text out of a string. An example of the string is:
"Captured Credit Card: MasterCard, xxxx-xxxx-xxxx-xxxx, 09/19. Set as Default."
My query is pulling the expiration month and year out of that text.
SUBSTRING(ADT.MsgText, CHARINDEX('/', ADT.MsgText, 0) -2, 2) AS [Month]
,'20' + SUBSTRING(ADT.MsgText, CHARINDEX('/', ADT.MsgText, 0) + 1, 2) AS [Year]
However, I need these number values to be recognized as date values. I tried the following method, but was unsuccessful:
UPDATE CCC
SET CCC.[Month] = convert(date, convert(varchar(4), CCC.[Year]) + ' ' + convert(varchar(2), CCC.[Month]))
FROM CC_Captured CCC
The error message I get is: "Update or insert of view or function 'CCC' failed because it contains a derived or constant field."
I'm wondering how to convert these values to be date?
Any help is appreciated!
(Also I am using SQL 2016)
sql date sql-server-2016 varchar
add a comment |
I have a query where I am grabbing certain text out of a string. An example of the string is:
"Captured Credit Card: MasterCard, xxxx-xxxx-xxxx-xxxx, 09/19. Set as Default."
My query is pulling the expiration month and year out of that text.
SUBSTRING(ADT.MsgText, CHARINDEX('/', ADT.MsgText, 0) -2, 2) AS [Month]
,'20' + SUBSTRING(ADT.MsgText, CHARINDEX('/', ADT.MsgText, 0) + 1, 2) AS [Year]
However, I need these number values to be recognized as date values. I tried the following method, but was unsuccessful:
UPDATE CCC
SET CCC.[Month] = convert(date, convert(varchar(4), CCC.[Year]) + ' ' + convert(varchar(2), CCC.[Month]))
FROM CC_Captured CCC
The error message I get is: "Update or insert of view or function 'CCC' failed because it contains a derived or constant field."
I'm wondering how to convert these values to be date?
Any help is appreciated!
(Also I am using SQL 2016)
sql date sql-server-2016 varchar
I have a query where I am grabbing certain text out of a string. An example of the string is:
"Captured Credit Card: MasterCard, xxxx-xxxx-xxxx-xxxx, 09/19. Set as Default."
My query is pulling the expiration month and year out of that text.
SUBSTRING(ADT.MsgText, CHARINDEX('/', ADT.MsgText, 0) -2, 2) AS [Month]
,'20' + SUBSTRING(ADT.MsgText, CHARINDEX('/', ADT.MsgText, 0) + 1, 2) AS [Year]
However, I need these number values to be recognized as date values. I tried the following method, but was unsuccessful:
UPDATE CCC
SET CCC.[Month] = convert(date, convert(varchar(4), CCC.[Year]) + ' ' + convert(varchar(2), CCC.[Month]))
FROM CC_Captured CCC
The error message I get is: "Update or insert of view or function 'CCC' failed because it contains a derived or constant field."
I'm wondering how to convert these values to be date?
Any help is appreciated!
(Also I am using SQL 2016)
sql date sql-server-2016 varchar
sql date sql-server-2016 varchar
edited Nov 15 '18 at 10:48
Ole V.V.
28.6k63352
28.6k63352
asked Nov 14 '18 at 13:31
KatelynKatelyn
134
134
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can use DATEFROMPARTS to construct a date in the month (first or last day of the month).
declare @MsgText nvarchar(1000) = N'Captured Credit Card: MasterCard, xxxx-xxxx-xxxx-xxxx, 09/19. Set as Default.'
select SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) AS [Month]
,'20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) AS [Year]
-- First day of the month
select DATEFROMPARTS(CAST('20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) as int), CAST(SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) as int), 1) as FirstDayOfMonth
-- Last day of the month - add 1 month to the date above and go back 1 day
select DATEADD(day, -1, DATEADD(month, 1, DATEFROMPARTS(CAST('20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) as int), CAST(SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) as int), 1))) as EndOfMonth
Or even better, use EOMONTH function:
select EOMONTH(DATEFROMPARTS(CAST('20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) as int), CAST(SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) as int), 1)) as EndOfMonth
Thank you! This was very helpful!
– Katelyn
Nov 23 '18 at 15:06
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%2f53301428%2fconverting-varchar-values-that-were-derived-from-a-constant-field-to-month-and-y%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use DATEFROMPARTS to construct a date in the month (first or last day of the month).
declare @MsgText nvarchar(1000) = N'Captured Credit Card: MasterCard, xxxx-xxxx-xxxx-xxxx, 09/19. Set as Default.'
select SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) AS [Month]
,'20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) AS [Year]
-- First day of the month
select DATEFROMPARTS(CAST('20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) as int), CAST(SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) as int), 1) as FirstDayOfMonth
-- Last day of the month - add 1 month to the date above and go back 1 day
select DATEADD(day, -1, DATEADD(month, 1, DATEFROMPARTS(CAST('20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) as int), CAST(SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) as int), 1))) as EndOfMonth
Or even better, use EOMONTH function:
select EOMONTH(DATEFROMPARTS(CAST('20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) as int), CAST(SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) as int), 1)) as EndOfMonth
Thank you! This was very helpful!
– Katelyn
Nov 23 '18 at 15:06
add a comment |
You can use DATEFROMPARTS to construct a date in the month (first or last day of the month).
declare @MsgText nvarchar(1000) = N'Captured Credit Card: MasterCard, xxxx-xxxx-xxxx-xxxx, 09/19. Set as Default.'
select SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) AS [Month]
,'20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) AS [Year]
-- First day of the month
select DATEFROMPARTS(CAST('20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) as int), CAST(SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) as int), 1) as FirstDayOfMonth
-- Last day of the month - add 1 month to the date above and go back 1 day
select DATEADD(day, -1, DATEADD(month, 1, DATEFROMPARTS(CAST('20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) as int), CAST(SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) as int), 1))) as EndOfMonth
Or even better, use EOMONTH function:
select EOMONTH(DATEFROMPARTS(CAST('20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) as int), CAST(SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) as int), 1)) as EndOfMonth
Thank you! This was very helpful!
– Katelyn
Nov 23 '18 at 15:06
add a comment |
You can use DATEFROMPARTS to construct a date in the month (first or last day of the month).
declare @MsgText nvarchar(1000) = N'Captured Credit Card: MasterCard, xxxx-xxxx-xxxx-xxxx, 09/19. Set as Default.'
select SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) AS [Month]
,'20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) AS [Year]
-- First day of the month
select DATEFROMPARTS(CAST('20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) as int), CAST(SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) as int), 1) as FirstDayOfMonth
-- Last day of the month - add 1 month to the date above and go back 1 day
select DATEADD(day, -1, DATEADD(month, 1, DATEFROMPARTS(CAST('20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) as int), CAST(SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) as int), 1))) as EndOfMonth
Or even better, use EOMONTH function:
select EOMONTH(DATEFROMPARTS(CAST('20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) as int), CAST(SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) as int), 1)) as EndOfMonth
You can use DATEFROMPARTS to construct a date in the month (first or last day of the month).
declare @MsgText nvarchar(1000) = N'Captured Credit Card: MasterCard, xxxx-xxxx-xxxx-xxxx, 09/19. Set as Default.'
select SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) AS [Month]
,'20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) AS [Year]
-- First day of the month
select DATEFROMPARTS(CAST('20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) as int), CAST(SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) as int), 1) as FirstDayOfMonth
-- Last day of the month - add 1 month to the date above and go back 1 day
select DATEADD(day, -1, DATEADD(month, 1, DATEFROMPARTS(CAST('20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) as int), CAST(SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) as int), 1))) as EndOfMonth
Or even better, use EOMONTH function:
select EOMONTH(DATEFROMPARTS(CAST('20' + SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) + 1, 2) as int), CAST(SUBSTRING(@MsgText, CHARINDEX('/', @MsgText, 0) -2, 2) as int), 1)) as EndOfMonth
answered Nov 16 '18 at 8:53
Andrey NikolovAndrey Nikolov
3,8883721
3,8883721
Thank you! This was very helpful!
– Katelyn
Nov 23 '18 at 15:06
add a comment |
Thank you! This was very helpful!
– Katelyn
Nov 23 '18 at 15:06
Thank you! This was very helpful!
– Katelyn
Nov 23 '18 at 15:06
Thank you! This was very helpful!
– Katelyn
Nov 23 '18 at 15:06
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%2f53301428%2fconverting-varchar-values-that-were-derived-from-a-constant-field-to-month-and-y%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