Custom sequence of reference Id on the basis of DateTime in Sql Query
Is it possible to write an sql query that returns
18315-0921
The result is time based where
18 means 2018
315 means day of the year
0921 means present time.
c# sql sql-server datetime
|
show 4 more comments
Is it possible to write an sql query that returns
18315-0921
The result is time based where
18 means 2018
315 means day of the year
0921 means present time.
c# sql sql-server datetime
Please tag your question with the database you are using.
– Gordon Linoff
Nov 20 '18 at 11:47
added @GordonLinoff
– Awais Zafar
Nov 20 '18 at 11:48
Do you need to handle dates before the year 2000? If so, what should the YY component of your date look like?
– JohnLBevan
Nov 20 '18 at 11:49
I'm guessing18318
should be18315
, and0921
means09:21:00
- so hours and minutes. Right?
– Zohar Peled
Nov 20 '18 at 11:50
2
What have you tried? Where did it fail? SO Is not a code-writing service....
– Zohar Peled
Nov 20 '18 at 11:51
|
show 4 more comments
Is it possible to write an sql query that returns
18315-0921
The result is time based where
18 means 2018
315 means day of the year
0921 means present time.
c# sql sql-server datetime
Is it possible to write an sql query that returns
18315-0921
The result is time based where
18 means 2018
315 means day of the year
0921 means present time.
c# sql sql-server datetime
c# sql sql-server datetime
edited Nov 20 '18 at 13:57
Luis
5,11922645
5,11922645
asked Nov 20 '18 at 11:45
Awais Zafar
84
84
Please tag your question with the database you are using.
– Gordon Linoff
Nov 20 '18 at 11:47
added @GordonLinoff
– Awais Zafar
Nov 20 '18 at 11:48
Do you need to handle dates before the year 2000? If so, what should the YY component of your date look like?
– JohnLBevan
Nov 20 '18 at 11:49
I'm guessing18318
should be18315
, and0921
means09:21:00
- so hours and minutes. Right?
– Zohar Peled
Nov 20 '18 at 11:50
2
What have you tried? Where did it fail? SO Is not a code-writing service....
– Zohar Peled
Nov 20 '18 at 11:51
|
show 4 more comments
Please tag your question with the database you are using.
– Gordon Linoff
Nov 20 '18 at 11:47
added @GordonLinoff
– Awais Zafar
Nov 20 '18 at 11:48
Do you need to handle dates before the year 2000? If so, what should the YY component of your date look like?
– JohnLBevan
Nov 20 '18 at 11:49
I'm guessing18318
should be18315
, and0921
means09:21:00
- so hours and minutes. Right?
– Zohar Peled
Nov 20 '18 at 11:50
2
What have you tried? Where did it fail? SO Is not a code-writing service....
– Zohar Peled
Nov 20 '18 at 11:51
Please tag your question with the database you are using.
– Gordon Linoff
Nov 20 '18 at 11:47
Please tag your question with the database you are using.
– Gordon Linoff
Nov 20 '18 at 11:47
added @GordonLinoff
– Awais Zafar
Nov 20 '18 at 11:48
added @GordonLinoff
– Awais Zafar
Nov 20 '18 at 11:48
Do you need to handle dates before the year 2000? If so, what should the YY component of your date look like?
– JohnLBevan
Nov 20 '18 at 11:49
Do you need to handle dates before the year 2000? If so, what should the YY component of your date look like?
– JohnLBevan
Nov 20 '18 at 11:49
I'm guessing
18318
should be 18315
, and 0921
means 09:21:00
- so hours and minutes. Right?– Zohar Peled
Nov 20 '18 at 11:50
I'm guessing
18318
should be 18315
, and 0921
means 09:21:00
- so hours and minutes. Right?– Zohar Peled
Nov 20 '18 at 11:50
2
2
What have you tried? Where did it fail? SO Is not a code-writing service....
– Zohar Peled
Nov 20 '18 at 11:51
What have you tried? Where did it fail? SO Is not a code-writing service....
– Zohar Peled
Nov 20 '18 at 11:51
|
show 4 more comments
2 Answers
2
active
oldest
votes
Per @Rob's suggesion in the comments you can do this via: Example
declare @myDateTime DateTime = getutcdate()
select format(@myDateTime, 'yy')
+ format(DatePart(dy, @myDateTime), '000')
+ format(@myDateTime, '-HHmm')
However, the ddd
part isn't available this way, since ddd
would convert to the weekday name: i.e per documentation. That said, you can use this to simplify the above: Example
Original solution:
Here's a solution: Example
declare @myDateTime DateTime = getutcdate()
--to get the component parts
select DatePart(YEAR, @myDateTime) - 2000 YY
, DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime) DDD
, DatePart(Hour, @myDateTime) HH
, DatePart(Minute, @myDateTime) MM
--to format as a single string
select right('0' + cast(DatePart(YEAR, @myDateTime) - 2000 as varchar(10)), 2)
+ right('00' + cast(DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime) as varchar(10)), 3)
+ '-'
+ right('0' + cast(DatePart(Hour, @myDateTime) as varchar(10)), 2)
+ right('0' + cast(DatePart(Minute, @myDateTime) as varchar(10)), 2)
DatePart(YEAR, @myDateTime) - 2000
gives us a 2 digit year by getting the full year (e.g.2018
as a number, then pulling back just those digits we're interested in by subtracting2000
)
DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime)
gives us the number of days since the 1st Jan in the date's year (given byDateFromParts(DatePart(YEAR, @myDateTime),1,1))
up to the given date (so 1st Jan would be0
; if you wanted 1st Jan to be 1, just add 1 to this result).- The hour and the minute value are just given by pulling back those date parts.
- When we create the string we cast these numeric results to strings (varchar) and concatenate them.
- The
right('0' + /*...*/)
part allows us to right pad the digits with leading zeros, so if the time's09:05
we get0905
instead of95
.
You can get almost what you're after by using the native format
command; e.g.
select format(@myDateTime, 'yy-HHmm')
However, the ddd
part isn't available this way, since ddd
would convert to the weekday name: i.e per documentation. That said, you can use this to simplify the above: Example
select format(@myDateTime, 'yy')
+ right('00' + cast(DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime) as varchar(10)), 3)
+ format(@myDateTime, '-HHmm')
Thank you @John :)
– Awais Zafar
Nov 20 '18 at 12:01
No worries; FYI: simplified version available; see update.
– JohnLBevan
Nov 20 '18 at 12:06
1
@JohnLBevan, perhaps I've missed a nuance when skimming the question and your answer, but would notDATEPART(dy, @myDateTime)
satisfy the ddd component?
– Rob
Nov 20 '18 at 12:11
1
Good shout @Rob; I wasn't familiar withdy
(don't normally need to play with dates in SQl beyond storing or querying)... Thanks for the tip.
– JohnLBevan
Nov 20 '18 at 12:13
add a comment |
declare @TDATE as datetime2 = getdate();
select Right('0' + CAST(YEAR(@TDATE) % 100 as varchar(2)) , 2)
+ RIGHT('00' + CAST(DATEPART(y,@TDATE) as varchar(3)) , 3)
+ '-'
+ REPLACE(CONVERT(varchar(5),@TDATE, 114), ':', '');
so I'm getting the Year number in 2 digits by taking the modulus 100 of the year , e.g. 2014 % 100 is 14
then I'm changing that to a string, with a leading '0' if needed
To get the day number of the year, I'm using DatePart with 'y' to indicate 'day of year number' and that is padded with up to 2 digits if needed.
To get the time I'm converting to hh:mi:ss:mmm, but only keeping the first 5 chars, then I'm replacing the ':' with nothing
It also works .Thank you @Cato.
– Awais Zafar
Nov 20 '18 at 12:02
you are welcome - the best method to follow in these cases is try and work out how to do each bit, then join it together
– Cato
Nov 20 '18 at 12:03
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%2f53392325%2fcustom-sequence-of-reference-id-on-the-basis-of-datetime-in-sql-query%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
Per @Rob's suggesion in the comments you can do this via: Example
declare @myDateTime DateTime = getutcdate()
select format(@myDateTime, 'yy')
+ format(DatePart(dy, @myDateTime), '000')
+ format(@myDateTime, '-HHmm')
However, the ddd
part isn't available this way, since ddd
would convert to the weekday name: i.e per documentation. That said, you can use this to simplify the above: Example
Original solution:
Here's a solution: Example
declare @myDateTime DateTime = getutcdate()
--to get the component parts
select DatePart(YEAR, @myDateTime) - 2000 YY
, DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime) DDD
, DatePart(Hour, @myDateTime) HH
, DatePart(Minute, @myDateTime) MM
--to format as a single string
select right('0' + cast(DatePart(YEAR, @myDateTime) - 2000 as varchar(10)), 2)
+ right('00' + cast(DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime) as varchar(10)), 3)
+ '-'
+ right('0' + cast(DatePart(Hour, @myDateTime) as varchar(10)), 2)
+ right('0' + cast(DatePart(Minute, @myDateTime) as varchar(10)), 2)
DatePart(YEAR, @myDateTime) - 2000
gives us a 2 digit year by getting the full year (e.g.2018
as a number, then pulling back just those digits we're interested in by subtracting2000
)
DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime)
gives us the number of days since the 1st Jan in the date's year (given byDateFromParts(DatePart(YEAR, @myDateTime),1,1))
up to the given date (so 1st Jan would be0
; if you wanted 1st Jan to be 1, just add 1 to this result).- The hour and the minute value are just given by pulling back those date parts.
- When we create the string we cast these numeric results to strings (varchar) and concatenate them.
- The
right('0' + /*...*/)
part allows us to right pad the digits with leading zeros, so if the time's09:05
we get0905
instead of95
.
You can get almost what you're after by using the native format
command; e.g.
select format(@myDateTime, 'yy-HHmm')
However, the ddd
part isn't available this way, since ddd
would convert to the weekday name: i.e per documentation. That said, you can use this to simplify the above: Example
select format(@myDateTime, 'yy')
+ right('00' + cast(DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime) as varchar(10)), 3)
+ format(@myDateTime, '-HHmm')
Thank you @John :)
– Awais Zafar
Nov 20 '18 at 12:01
No worries; FYI: simplified version available; see update.
– JohnLBevan
Nov 20 '18 at 12:06
1
@JohnLBevan, perhaps I've missed a nuance when skimming the question and your answer, but would notDATEPART(dy, @myDateTime)
satisfy the ddd component?
– Rob
Nov 20 '18 at 12:11
1
Good shout @Rob; I wasn't familiar withdy
(don't normally need to play with dates in SQl beyond storing or querying)... Thanks for the tip.
– JohnLBevan
Nov 20 '18 at 12:13
add a comment |
Per @Rob's suggesion in the comments you can do this via: Example
declare @myDateTime DateTime = getutcdate()
select format(@myDateTime, 'yy')
+ format(DatePart(dy, @myDateTime), '000')
+ format(@myDateTime, '-HHmm')
However, the ddd
part isn't available this way, since ddd
would convert to the weekday name: i.e per documentation. That said, you can use this to simplify the above: Example
Original solution:
Here's a solution: Example
declare @myDateTime DateTime = getutcdate()
--to get the component parts
select DatePart(YEAR, @myDateTime) - 2000 YY
, DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime) DDD
, DatePart(Hour, @myDateTime) HH
, DatePart(Minute, @myDateTime) MM
--to format as a single string
select right('0' + cast(DatePart(YEAR, @myDateTime) - 2000 as varchar(10)), 2)
+ right('00' + cast(DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime) as varchar(10)), 3)
+ '-'
+ right('0' + cast(DatePart(Hour, @myDateTime) as varchar(10)), 2)
+ right('0' + cast(DatePart(Minute, @myDateTime) as varchar(10)), 2)
DatePart(YEAR, @myDateTime) - 2000
gives us a 2 digit year by getting the full year (e.g.2018
as a number, then pulling back just those digits we're interested in by subtracting2000
)
DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime)
gives us the number of days since the 1st Jan in the date's year (given byDateFromParts(DatePart(YEAR, @myDateTime),1,1))
up to the given date (so 1st Jan would be0
; if you wanted 1st Jan to be 1, just add 1 to this result).- The hour and the minute value are just given by pulling back those date parts.
- When we create the string we cast these numeric results to strings (varchar) and concatenate them.
- The
right('0' + /*...*/)
part allows us to right pad the digits with leading zeros, so if the time's09:05
we get0905
instead of95
.
You can get almost what you're after by using the native format
command; e.g.
select format(@myDateTime, 'yy-HHmm')
However, the ddd
part isn't available this way, since ddd
would convert to the weekday name: i.e per documentation. That said, you can use this to simplify the above: Example
select format(@myDateTime, 'yy')
+ right('00' + cast(DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime) as varchar(10)), 3)
+ format(@myDateTime, '-HHmm')
Thank you @John :)
– Awais Zafar
Nov 20 '18 at 12:01
No worries; FYI: simplified version available; see update.
– JohnLBevan
Nov 20 '18 at 12:06
1
@JohnLBevan, perhaps I've missed a nuance when skimming the question and your answer, but would notDATEPART(dy, @myDateTime)
satisfy the ddd component?
– Rob
Nov 20 '18 at 12:11
1
Good shout @Rob; I wasn't familiar withdy
(don't normally need to play with dates in SQl beyond storing or querying)... Thanks for the tip.
– JohnLBevan
Nov 20 '18 at 12:13
add a comment |
Per @Rob's suggesion in the comments you can do this via: Example
declare @myDateTime DateTime = getutcdate()
select format(@myDateTime, 'yy')
+ format(DatePart(dy, @myDateTime), '000')
+ format(@myDateTime, '-HHmm')
However, the ddd
part isn't available this way, since ddd
would convert to the weekday name: i.e per documentation. That said, you can use this to simplify the above: Example
Original solution:
Here's a solution: Example
declare @myDateTime DateTime = getutcdate()
--to get the component parts
select DatePart(YEAR, @myDateTime) - 2000 YY
, DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime) DDD
, DatePart(Hour, @myDateTime) HH
, DatePart(Minute, @myDateTime) MM
--to format as a single string
select right('0' + cast(DatePart(YEAR, @myDateTime) - 2000 as varchar(10)), 2)
+ right('00' + cast(DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime) as varchar(10)), 3)
+ '-'
+ right('0' + cast(DatePart(Hour, @myDateTime) as varchar(10)), 2)
+ right('0' + cast(DatePart(Minute, @myDateTime) as varchar(10)), 2)
DatePart(YEAR, @myDateTime) - 2000
gives us a 2 digit year by getting the full year (e.g.2018
as a number, then pulling back just those digits we're interested in by subtracting2000
)
DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime)
gives us the number of days since the 1st Jan in the date's year (given byDateFromParts(DatePart(YEAR, @myDateTime),1,1))
up to the given date (so 1st Jan would be0
; if you wanted 1st Jan to be 1, just add 1 to this result).- The hour and the minute value are just given by pulling back those date parts.
- When we create the string we cast these numeric results to strings (varchar) and concatenate them.
- The
right('0' + /*...*/)
part allows us to right pad the digits with leading zeros, so if the time's09:05
we get0905
instead of95
.
You can get almost what you're after by using the native format
command; e.g.
select format(@myDateTime, 'yy-HHmm')
However, the ddd
part isn't available this way, since ddd
would convert to the weekday name: i.e per documentation. That said, you can use this to simplify the above: Example
select format(@myDateTime, 'yy')
+ right('00' + cast(DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime) as varchar(10)), 3)
+ format(@myDateTime, '-HHmm')
Per @Rob's suggesion in the comments you can do this via: Example
declare @myDateTime DateTime = getutcdate()
select format(@myDateTime, 'yy')
+ format(DatePart(dy, @myDateTime), '000')
+ format(@myDateTime, '-HHmm')
However, the ddd
part isn't available this way, since ddd
would convert to the weekday name: i.e per documentation. That said, you can use this to simplify the above: Example
Original solution:
Here's a solution: Example
declare @myDateTime DateTime = getutcdate()
--to get the component parts
select DatePart(YEAR, @myDateTime) - 2000 YY
, DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime) DDD
, DatePart(Hour, @myDateTime) HH
, DatePart(Minute, @myDateTime) MM
--to format as a single string
select right('0' + cast(DatePart(YEAR, @myDateTime) - 2000 as varchar(10)), 2)
+ right('00' + cast(DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime) as varchar(10)), 3)
+ '-'
+ right('0' + cast(DatePart(Hour, @myDateTime) as varchar(10)), 2)
+ right('0' + cast(DatePart(Minute, @myDateTime) as varchar(10)), 2)
DatePart(YEAR, @myDateTime) - 2000
gives us a 2 digit year by getting the full year (e.g.2018
as a number, then pulling back just those digits we're interested in by subtracting2000
)
DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime)
gives us the number of days since the 1st Jan in the date's year (given byDateFromParts(DatePart(YEAR, @myDateTime),1,1))
up to the given date (so 1st Jan would be0
; if you wanted 1st Jan to be 1, just add 1 to this result).- The hour and the minute value are just given by pulling back those date parts.
- When we create the string we cast these numeric results to strings (varchar) and concatenate them.
- The
right('0' + /*...*/)
part allows us to right pad the digits with leading zeros, so if the time's09:05
we get0905
instead of95
.
You can get almost what you're after by using the native format
command; e.g.
select format(@myDateTime, 'yy-HHmm')
However, the ddd
part isn't available this way, since ddd
would convert to the weekday name: i.e per documentation. That said, you can use this to simplify the above: Example
select format(@myDateTime, 'yy')
+ right('00' + cast(DateDiff(Day, DateFromParts(DatePart(YEAR, @myDateTime),1,1), @myDateTime) as varchar(10)), 3)
+ format(@myDateTime, '-HHmm')
edited Nov 20 '18 at 12:16
answered Nov 20 '18 at 11:55
JohnLBevan
14.3k145105
14.3k145105
Thank you @John :)
– Awais Zafar
Nov 20 '18 at 12:01
No worries; FYI: simplified version available; see update.
– JohnLBevan
Nov 20 '18 at 12:06
1
@JohnLBevan, perhaps I've missed a nuance when skimming the question and your answer, but would notDATEPART(dy, @myDateTime)
satisfy the ddd component?
– Rob
Nov 20 '18 at 12:11
1
Good shout @Rob; I wasn't familiar withdy
(don't normally need to play with dates in SQl beyond storing or querying)... Thanks for the tip.
– JohnLBevan
Nov 20 '18 at 12:13
add a comment |
Thank you @John :)
– Awais Zafar
Nov 20 '18 at 12:01
No worries; FYI: simplified version available; see update.
– JohnLBevan
Nov 20 '18 at 12:06
1
@JohnLBevan, perhaps I've missed a nuance when skimming the question and your answer, but would notDATEPART(dy, @myDateTime)
satisfy the ddd component?
– Rob
Nov 20 '18 at 12:11
1
Good shout @Rob; I wasn't familiar withdy
(don't normally need to play with dates in SQl beyond storing or querying)... Thanks for the tip.
– JohnLBevan
Nov 20 '18 at 12:13
Thank you @John :)
– Awais Zafar
Nov 20 '18 at 12:01
Thank you @John :)
– Awais Zafar
Nov 20 '18 at 12:01
No worries; FYI: simplified version available; see update.
– JohnLBevan
Nov 20 '18 at 12:06
No worries; FYI: simplified version available; see update.
– JohnLBevan
Nov 20 '18 at 12:06
1
1
@JohnLBevan, perhaps I've missed a nuance when skimming the question and your answer, but would not
DATEPART(dy, @myDateTime)
satisfy the ddd component?– Rob
Nov 20 '18 at 12:11
@JohnLBevan, perhaps I've missed a nuance when skimming the question and your answer, but would not
DATEPART(dy, @myDateTime)
satisfy the ddd component?– Rob
Nov 20 '18 at 12:11
1
1
Good shout @Rob; I wasn't familiar with
dy
(don't normally need to play with dates in SQl beyond storing or querying)... Thanks for the tip.– JohnLBevan
Nov 20 '18 at 12:13
Good shout @Rob; I wasn't familiar with
dy
(don't normally need to play with dates in SQl beyond storing or querying)... Thanks for the tip.– JohnLBevan
Nov 20 '18 at 12:13
add a comment |
declare @TDATE as datetime2 = getdate();
select Right('0' + CAST(YEAR(@TDATE) % 100 as varchar(2)) , 2)
+ RIGHT('00' + CAST(DATEPART(y,@TDATE) as varchar(3)) , 3)
+ '-'
+ REPLACE(CONVERT(varchar(5),@TDATE, 114), ':', '');
so I'm getting the Year number in 2 digits by taking the modulus 100 of the year , e.g. 2014 % 100 is 14
then I'm changing that to a string, with a leading '0' if needed
To get the day number of the year, I'm using DatePart with 'y' to indicate 'day of year number' and that is padded with up to 2 digits if needed.
To get the time I'm converting to hh:mi:ss:mmm, but only keeping the first 5 chars, then I'm replacing the ':' with nothing
It also works .Thank you @Cato.
– Awais Zafar
Nov 20 '18 at 12:02
you are welcome - the best method to follow in these cases is try and work out how to do each bit, then join it together
– Cato
Nov 20 '18 at 12:03
add a comment |
declare @TDATE as datetime2 = getdate();
select Right('0' + CAST(YEAR(@TDATE) % 100 as varchar(2)) , 2)
+ RIGHT('00' + CAST(DATEPART(y,@TDATE) as varchar(3)) , 3)
+ '-'
+ REPLACE(CONVERT(varchar(5),@TDATE, 114), ':', '');
so I'm getting the Year number in 2 digits by taking the modulus 100 of the year , e.g. 2014 % 100 is 14
then I'm changing that to a string, with a leading '0' if needed
To get the day number of the year, I'm using DatePart with 'y' to indicate 'day of year number' and that is padded with up to 2 digits if needed.
To get the time I'm converting to hh:mi:ss:mmm, but only keeping the first 5 chars, then I'm replacing the ':' with nothing
It also works .Thank you @Cato.
– Awais Zafar
Nov 20 '18 at 12:02
you are welcome - the best method to follow in these cases is try and work out how to do each bit, then join it together
– Cato
Nov 20 '18 at 12:03
add a comment |
declare @TDATE as datetime2 = getdate();
select Right('0' + CAST(YEAR(@TDATE) % 100 as varchar(2)) , 2)
+ RIGHT('00' + CAST(DATEPART(y,@TDATE) as varchar(3)) , 3)
+ '-'
+ REPLACE(CONVERT(varchar(5),@TDATE, 114), ':', '');
so I'm getting the Year number in 2 digits by taking the modulus 100 of the year , e.g. 2014 % 100 is 14
then I'm changing that to a string, with a leading '0' if needed
To get the day number of the year, I'm using DatePart with 'y' to indicate 'day of year number' and that is padded with up to 2 digits if needed.
To get the time I'm converting to hh:mi:ss:mmm, but only keeping the first 5 chars, then I'm replacing the ':' with nothing
declare @TDATE as datetime2 = getdate();
select Right('0' + CAST(YEAR(@TDATE) % 100 as varchar(2)) , 2)
+ RIGHT('00' + CAST(DATEPART(y,@TDATE) as varchar(3)) , 3)
+ '-'
+ REPLACE(CONVERT(varchar(5),@TDATE, 114), ':', '');
so I'm getting the Year number in 2 digits by taking the modulus 100 of the year , e.g. 2014 % 100 is 14
then I'm changing that to a string, with a leading '0' if needed
To get the day number of the year, I'm using DatePart with 'y' to indicate 'day of year number' and that is padded with up to 2 digits if needed.
To get the time I'm converting to hh:mi:ss:mmm, but only keeping the first 5 chars, then I'm replacing the ':' with nothing
edited Nov 20 '18 at 12:02
answered Nov 20 '18 at 11:56
Cato
2,525210
2,525210
It also works .Thank you @Cato.
– Awais Zafar
Nov 20 '18 at 12:02
you are welcome - the best method to follow in these cases is try and work out how to do each bit, then join it together
– Cato
Nov 20 '18 at 12:03
add a comment |
It also works .Thank you @Cato.
– Awais Zafar
Nov 20 '18 at 12:02
you are welcome - the best method to follow in these cases is try and work out how to do each bit, then join it together
– Cato
Nov 20 '18 at 12:03
It also works .Thank you @Cato.
– Awais Zafar
Nov 20 '18 at 12:02
It also works .Thank you @Cato.
– Awais Zafar
Nov 20 '18 at 12:02
you are welcome - the best method to follow in these cases is try and work out how to do each bit, then join it together
– Cato
Nov 20 '18 at 12:03
you are welcome - the best method to follow in these cases is try and work out how to do each bit, then join it together
– Cato
Nov 20 '18 at 12:03
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53392325%2fcustom-sequence-of-reference-id-on-the-basis-of-datetime-in-sql-query%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
Please tag your question with the database you are using.
– Gordon Linoff
Nov 20 '18 at 11:47
added @GordonLinoff
– Awais Zafar
Nov 20 '18 at 11:48
Do you need to handle dates before the year 2000? If so, what should the YY component of your date look like?
– JohnLBevan
Nov 20 '18 at 11:49
I'm guessing
18318
should be18315
, and0921
means09:21:00
- so hours and minutes. Right?– Zohar Peled
Nov 20 '18 at 11:50
2
What have you tried? Where did it fail? SO Is not a code-writing service....
– Zohar Peled
Nov 20 '18 at 11:51