I have tried below code to convert the normal date to Julian date but now I want to convert HH:MM:SS to Julian time
converting a Julian date
SELECT DATEPART(YEAR, GETDATE()) * 1000 + DATEPART(dy, GETDATE())
сonverts back to a regular date
SELECT DATEADD(HOUR, CAST(RIGHT('13000', 3) AS INT) - 1, CAST(CONCAT('', LEFT('13000', 4)) AS time)) AS in_date
Related
I'm having a bit of trouble with pulling records which are due within 30 days. The database I am working with stores the date in a char(10) field in format 103 (dd/mm/yyyy or 10/12/2021). I use a convert function to make this date usable, but when I try to use it with a between query it fails:
WHERE
CONVERT(Date, SUBSTRING(TDate, 1, 10), 103)
BETWEEN DATEADD(DAY, 30, GETDATE()) AND GETDATE()
Now I suspect that it fails because GETDATE() defaults to format yyyy-mm-dd-time, so the comparison won't work. My question is, how can I convert GETDATE() to format 103 to get the correct comparison, or is it a matter of converting my TDate field to something else to get it working?
The expression x BETWEEN a AND b is same as x >= a AND x <= b.
Now GETDATE() + 30 is always going to be greater than GETDATE() so this condition can never be true for any value — just like x >= (y + 30) and x <= y cannot be true for any x and y.
Then we have another problem, you're comparing a date with datetime. If the date is 2021-12-10 and current datetime is 2021-12-10 12:00 PM then the comparison will return false when you check date >= datetime. I recommend the following:
WHERE CONVERT(DATE, tdate, 103) >= CAST(GETDATE() AS DATE)
AND CONVERT(DATE, tdate, 103) <= DATEADD(DAY, 29, CAST(GETDATE() AS DATE)) -- the range [0, 29] contains 30 days
I am trying to get a smalldatetime value of "9pm today" in a query. I thought I could use
DATEADD(HOUR, 21, CONVERT(date, GETDATE()))
but SQL Server doesn't like that - I get the error
The datepart hour is not supported by date function dateadd for data
type date.
Suggestions for a workaround?
Pretty simple, just cast date back to datetime after casting to date.
Thus you'll get current_date 00:00:00 and then add 21 hours:
select dateadd(hh, 21, cast(cast(getdate() as date) as datetime))
it is because dateadd's 3rd parameter should be datetime type, not date.
SELECT DATEADD(HOUR, 21, CONVERT(datetime,CONVERT(date, GETDATE())))
just add 21 / 24.0 to todays date
Select dateadd(day, datediff(day, 1, getDate()), 1) + (21 / 24.0)
First part, dateadd(day, datediff(day, 1, getDate()), 1), strips time from getdate(),
second part, + (21 / 24.0), adds fractional part of day equal to 9 am
This works because internally, SQL Server represents datetimes as two integers, one for the date, (number of days since 1 Jan 1900), and a second integer for the time, (number of ticks since midnight), which it combines into a decimal value where the integer part is the date integer, and the decimal part is the fraction of one day, so if you add 0.5 to a date, you get noon on that day, etc.
or, for comparison, using dateadd for hours,
Select dateadd(hour, 21, dateadd(day, datediff(day, 1, getDate()), 1))
I have a query -
SELECT * FROM TABLE WHERE Date >= DATEADD (day, -7, -getdate()) AND Date <= getdate();
This would return all records for each day except day 7. If I ran this query on a Sunday at 17:00 it would only produce results going back to Monday 17:00. How could I include results from Monday 08:00.
Try it like this:
SELECT *
FROM SomeWhere
WHERE [Date] > DATEADD(HOUR,8,DATEADD(DAY, -7, CAST(CAST(GETDATE() AS DATE) AS DATETIME))) --7 days back, 8 o'clock
AND [Date] <= GETDATE(); --now
That's because you are comparing date+time, not only date.
If you want to include all days, you can trunc the time-portion from getdate(): you can accomplish that with a conversion to date:
SELECT * FROM TABLE
WHERE Date >= DATEADD (day, -7, -convert(date, getdate())
AND Date <= convert(date, getdate());
If you want to start from 8 in the morning, the best is to add again 8 hours to getdate.
declare #t datetime = dateadd(HH, 8, convert(datetime, convert(date, getdate())))
SELECT * FROM TABLE
WHERE Date >= DATEADD (day, -7, -#t) AND Date <= #t;
NOTE: with the conversion convert(date, getdate()) you get a datatype date and you cannot add hours directly to it; you must re-convert it to datetime.
Sounds like you want to remove the time. Correct? If so then do the following.
SELECT * FROM TABLE WHERE Date >= (DATEADD (day, -7, -getdate()) AND Date DATEADD(dd, DATEDIFF(dd, 0, getdate()), 0))
Date formats are different can any one help me with different date formats
how to find datediff by using date format as ARRDAT(20140523) and DEPDAT(20140815)
The date format of the 2 values looks like yyyyMMdd which is the ISO date format.
Knowing this we can use the following:
SELECT DATEDIFF(day, ArrivalDate, DepartureDate) AS DiffInDays
FROM (
SELECT CONVERT(DATETIME, CAST(ARRDAT AS VARCHAR(8)), 112) AS ArrivalDate,
CONVERT(DATETIME, CAST(DEPDAT AS VARCHAR(8)), 112) AS DepartureDate
) AS t
CAST(expression AS VARCHAR(8)) changes the BIGINT to a VARCHAR for the CONVERT function to work.
CONVERT(DATETIME, expression, 112) specifies the format of the data is yyyyMMdd.
Try Following query:
SELECT DATEDIFF( DAY,
CAST(SUBSTRING(APPDAT,1,4)+'-'+SUBSTRING(APPDAT,5,2)+'-'+SUBSTRING(APPDAT,7,2) AS DATE),
CAST(SUBSTRING(DEPDAT,1,4)+'-'+SUBSTRING(DEPDAT,5,2)+'-'+SUBSTRING(DEPDAT,7,2) AS DATE))
FROM YourTable
I am trying to do the following:
Format GetDate() to display only the minutes
Format a varchar column to display only the minutes
Subtract the current time HH:MM:SS from GetDate() and the VarChar column
This is what I have
CONVERT(VARCHAR(5), GETDATE(), 108) - substring(convert(varchar(20), ColumnName, 9), 13, 5)
but I am getting this error and need some help please:
Operand data type varchar is invalid for subtract operator.
What you want is datepart(mi).
To get the minutes for getdate():
select datepart(mi, getdate())
To subtract a number of minutes from a datetime:
select dateadd(mi, - <minutes>, <datevalue>)
To remove the time from getdate(), just cast to date (in more recent versions of SQL Server):
select cast(getdate() as date)
To get the difference in minutes, use datediff:
select datediff(mi, <datestart>, <dateend>)
What are you really trying to accomplish?
I think this is what you want:
declare #str varchar(30)
set #str = '2012-08-14 10:12:02.690'
select datediff(minute, cast(#str as datetime), getdate())
Results when getdate() = '2012-08-14 11:21:10.250' is total minutes even over 60:
69