Custom sequence of reference Id on the basis of DateTime in Sql Query












1














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.










share|improve this question
























  • 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 be 18315, and 0921 means 09: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
















1














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.










share|improve this question
























  • 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 be 18315, and 0921 means 09: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














1












1








1







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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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




    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










  • 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 be 18315, and 0921 means 09: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












2 Answers
2






active

oldest

votes


















0














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 subtracting 2000)


  • 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 by DateFromParts(DatePart(YEAR, @myDateTime),1,1)) up to the given date (so 1st Jan would be 0; 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's 09:05 we get 0905 instead of 95.


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')





share|improve this answer























  • 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 not DATEPART(dy, @myDateTime) satisfy the ddd component?
    – Rob
    Nov 20 '18 at 12:11






  • 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



















1














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






share|improve this answer























  • 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











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%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









0














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 subtracting 2000)


  • 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 by DateFromParts(DatePart(YEAR, @myDateTime),1,1)) up to the given date (so 1st Jan would be 0; 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's 09:05 we get 0905 instead of 95.


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')





share|improve this answer























  • 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 not DATEPART(dy, @myDateTime) satisfy the ddd component?
    – Rob
    Nov 20 '18 at 12:11






  • 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
















0














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 subtracting 2000)


  • 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 by DateFromParts(DatePart(YEAR, @myDateTime),1,1)) up to the given date (so 1st Jan would be 0; 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's 09:05 we get 0905 instead of 95.


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')





share|improve this answer























  • 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 not DATEPART(dy, @myDateTime) satisfy the ddd component?
    – Rob
    Nov 20 '18 at 12:11






  • 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














0












0








0






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 subtracting 2000)


  • 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 by DateFromParts(DatePart(YEAR, @myDateTime),1,1)) up to the given date (so 1st Jan would be 0; 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's 09:05 we get 0905 instead of 95.


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')





share|improve this answer














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 subtracting 2000)


  • 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 by DateFromParts(DatePart(YEAR, @myDateTime),1,1)) up to the given date (so 1st Jan would be 0; 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's 09:05 we get 0905 instead of 95.


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')






share|improve this answer














share|improve this answer



share|improve this answer








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 not DATEPART(dy, @myDateTime) satisfy the ddd component?
    – Rob
    Nov 20 '18 at 12:11






  • 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


















  • 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 not DATEPART(dy, @myDateTime) satisfy the ddd component?
    – Rob
    Nov 20 '18 at 12:11






  • 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
















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













1














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






share|improve this answer























  • 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
















1














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






share|improve this answer























  • 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














1












1








1






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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








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


















  • 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


















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.





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.




draft saved


draft discarded














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





















































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

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?