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.
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
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')
How do I convert a 7-digit Julian Date (2016092) to a regular Calendar date (MM-DD-YYYY)?
I was thinking of taking the last three digits and converting it to a regular date then appending the first four digits as the year but I'd have to consider leap years.
Expected output: 04-01-2016
My current (SQL) code which solves the problem is
DECLARE #dt char(7)
SET #dt = 2016092
SELECT DATEADD(dd, CAST(RIGHT(#dt, 3) AS int) - 1, CAST(LEFT(#dt, 4) AS datetime))
How can I implement it on C#?
You don't have any kind of Julian Day (Date) format which is
https://en.wikipedia.org/wiki/Julian_day
But a kind of custom format which can be reconstructed from the sql provided:
year * 1000 + days from 1st Jan + 1
So 2016092 means year 2016 and 092 - 1 = 91st day from the 1st of Jan (1st of Apr)
Implementation:
int source = 2016092;
DateTime result = new DateTime(source / 1000, 1, 1).AddDays(source % 1000 - 1);
Console.WriteLine($"{result:MM-dd-yyyy}");
Outcome:
04-01-2016
I have this line in a stored procedure like this :
DATENAME(MONTH, tblReg.StartDate) as [Month],
Now I want to convert this line in linq
var b = sd.tblReg;
foreach (var c in b)
{
res += "'" + c.StartDate + "',";
}
res = res.Substring(0, res.Length - 1);
res += "]";
and want to get last 3 months.. i.e. current month is Aug so with Aug i want to get last 3 months same if current month is Jan then Dec Nov Oct.. in res like this
['May' ,'June','July','Aug']
You could do something like this to find previous 3 months using Linq. DateTimeFormat.GetMonthName will help you to get month name.
int month = ..; // given a month
var result = Enumerable
.Range(-2,18) // Compute +/- 3 months for original 12 months.
.TakeWhile(x=>x <=month) // Take months until the current month
.Reverse() // Reverse the order as we need backword months.
.Take(4) // Take top 4 months (including current month)
.Select(x=>CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(x<=0?x+12: x==12? 12 : (x+12)%12))
Check this Demo
I am using Textbox with property Textmode month for year and month picker instead of date picker, how can I get the month value and year value from "2016-03" in sql?
I tried MONTH('2016-03') and YEAR('2016-03') but it does not work.
Try this MONTH('2016-03-01') and YEAR('2016-03-01')
select * from participants where
year(registerdate) = 2016
and month(registerDate) = 1
This works for me
The Question is tagged to C# also So This may also help you;
string dateString="2016-03" // will be the input
DateTime d;
if (DateTime.TryParse(dateString, out d))
{
string monthName = d.ToString("MMMM", CultureInfo.InvariantCulture); // Will gives you the Month name
int year = d.Year; // Will give you the year
}
You can use a combination of STR_TO_DATE(), YEAR() and MONTH() to achieve that.
mysql> SELECT YEAR(STR_TO_DATE('2016-03', '%Y-%m')), MONTH(STR_TO_DATE('2016-03', '%Y-%m'));
+---------------------------------------+----------------------------------------+
| YEAR(STR_TO_DATE('2016-03', '%Y-%m')) | MONTH(STR_TO_DATE('2016-03', '%Y-%m')) |
+---------------------------------------+----------------------------------------+
| 2016 | 3 |
+---------------------------------------+----------------------------------------+
1 row in set (0.00 sec)
I have a DataTable (dt) as
StartDate EndDate Price
1 3 10
4 6 12
7 10 16
11 15 20
I want to filter the Values from dates between fromdate= 2 and todate=8.
so i am expecting the result to be
StartDate EndDate Price
1 3 10
4 6 12
7 10 16
I have used the vb Datatable Select as->
dt.Select("StartDate <= #" & fromdate& "# And EndDate >= #" & todate& "#")
but i am not getting the result. Can u please suggest me..
Your query should be like that:
dt.Select("StartDate <= #" & todate & "# And EndDate >= #" & fromdate & "#")
to find records that overlap with your range.
it should be
dt.Select("StartDate >= " & fromdate & " And EndDate <= " & todate)
Two problems
Your operator were all wrong. You were taking startdate less than 2 and greater than 8 whereas it should be startdate greater than 2 and less than 8
Morever no need for # assuming that columns are of integer type.
Use the DateDiff function, my friend