I am having issues trying to calculate the difference in Minutes between a Field and a literal string. However the output I am getting doesnt seem right at all, it looks to me like you can't seem to compare a Field and a literal string, however if they both the same then it seems fine i.e both are column values or literal strings.
Here is the SQL
select datediff(minute,enquiry,cast('17:00:00' as time))
from [dbo].[Test_Dates]
Is there anyway to get the right results, given the enquiry Field has values of hours within business hours. The result from the above query I am getting is as follows: -61536240
You should make both fields in time format
SELECT DATEDIFF(minute, CONVERT(CHAR(5), enquiry, 108) , CAST('17:00:00' as time))
FROM [dbo].[Test_Dates]
or
SELECT DATEDIFF(minute, CAST(enquiry as time) , CAST('17:00:00' as time))
FROM [dbo].[Test_Dates]
select datediff(minute,cast (enquiry as time),cast('17:00:00' as time)) from test_dates as t
Related
I have a column in a table with this format '2017-05-09 14:52:32.000' I would appreciate a way to convert it to a date format like MM/DD/YYYY
I have tried with:
select DATEADD(column, DATEDIFF(column, 0, getdate()), 0)
FROM table
but I get this error:
Column is not a recognized datediff option.
I've also tried another way and failed.
Please advise.
I'm assuming from the error message you reported that you're on SQL Server.
In SQL Server, the DATEADD and DATEDIFF functions take a "date part" as the first argument: YEAR, MONTH, DAY, HOUR, MINUTE, SECOND or MILLISECOND:
SELECT DATEADD(DAY, DATEDIFF(DAY, [COLUMN], GETDATE()), [COLUMN])
FROM [TABLE]
might be want what you want.
However,
SELECT CONVERT(DATE, [COLUMN]) AS [COLUMNDATE]
Might be even easier.
I have two variables 'triage_date_time' and 'checkin_date_time'. Both are formatted as, for example, 2018-12-31 14:13:00. Showing the year-month-day and hour-minute-second both within one cell.
I wanted to create a variable that calculates the time it takes from check-in to triage.
I attempted to use the following code:
SELECT DISTINCT datediff(minute, 'triage_date_time', 'checkin_date_time') as
checkin_to_triage
However, when running this code I get the following error... "Conversion failed when converting date and/or time from character string".
Any suggestions of how I can write a code that would calculate the minute difference of these two variables.
Thanks!
One problem is obviously the single quotes. Assuming that you are using SQL Server, variables start with #. So:
select datediff(minute, #triage_date_time, #checkin_date_time) as checkin_to_triage
If you are confused and really mean columns in a table, then:
select datediff(minute, triage_date_time, checkin_date_time) as checkin_to_triage
from t;
could it be that your field is a CHARACTER data type ?
cast your char to datetime
SELECT DISTINCT datediff(minute, CAST(triage_date_time AS datetime), CAST(checkin_date_time AS datetime)) as checkin_to_triage
Try with this query
DECLARE #triage_date_time DATETIME = '20181231 14:13:00'
DECLARE #checkin_date_time DATETIME = '20181231 16:13:00'
SELECT DATEDIFF (MINUTE, #triage_date_time, #checkin_date_time) AS 'checkin_to_triage'
Output :
checkin_to_triage
120
I have a script that I am using to populate a time dimension table and I would like to have a column for the time in 12 hour format.
I know this can be done by doing something along the lines of
SELECT CONVERT(varchar(15),[FullTime],100)
Where [FullTime] is a column containing a TIME field in HH:MM:SS format.
But this gives the following result 2:30pm and I would like 2:30:47PM, note the inclusion of seconds.
I know I could build this up using substrings etc. but I wondered if there was a prettier way of doing it.
Thanks
SELECT GETDATE() 'Today',
CONVERT(VARCHAR(8), [FullTime], 108) 'hh:mi:ss'
Taken from here
This will give you a column 'today' followed by the time value you seek 'hh:mi:ss'
If having also milliseconds is not a problem for you, you can use
SELECT CONVERT(VARCHAR, [FullTime], 109)
Declare #tstTime datetime
set #tstTime = GetDate()
Select
#tstTime
,CONCAT(SUBSTRING(CONVERT(varchar(26), #tstTime, 109),1,20),RIGHT(CONVERT(varchar(26), #tstTime, 109),2))
I am using below SQL query to get data by mentioning date range without year.
Select * from table where Left(Convert(Varchar(10),Cast(createddate As Date),101),5) between '11/01' and '12/31'
The above query works say when the user enter the date range as '11/01' to '12/31'. But, when the user enters anything like '11/01' to '01/31' or '05/31' to 02/28' etc, the query is not returning any data.
Is it possible to make it work for above ranges?
I would use the month() function:
Select *
from table
where month(nm_birthdate) in (11, 12);
Your query, however, should work because 101 zero-pads days and months.
If you want to find birthdays between two dates in MM/DD format, you could do:
where (#startmmdd < #endmmdd) and convert(varchar(5), nm_birthdate, 101) between #startmmdd and #endmmdd) or
(#startmmdd > #endmmdd) and convert(varchar(5), nm_birthdate, 101) not between #endmmdd and #startmmdd)
There might be a little adjustment depending on whether or not you want to include the end points.
Notes:
The variables #startmmdd and #endmmdd need to be in MM/DD format. Leading zeros are needed for months and days less than 10.
Convert conveniently uses the length of the data type, so left() is not necessary.
Use DATEPART, something like this:
SELECT * FROM table WHERE datepart(month, birthdate) IN (11, 12)
Of course, you can combine it with:
datepart(day, birthdate)
in order to get more complex condition.
Hope it helps
I am trying to compare time in my SQL query. However, when I run the query, I get zero result but I can see that in the table, there are records that should appear.
The query is as such:
SELECT *
FROM dbo.Alarms
WHERE StartDate <= '26/08/2015'
AND StartTime <= CONVERT(varchar(5), GETDATE(), 108)
The StartDate is stored in the database as YYYY-MM-DD and it seems to work fine when I query only with the date.
However, when I add the StartTime is when things don't work. StartTime stores the value in the 24 hour clock format.
What am not doing right?
Thanks
Use a correct datetime format:
SELECT *
FROM dbo.Alarms
WHERE StartDate <= '2015-08-26' AND StartTime <= cast(GETDATE() as date)
Don't compare date/time values as strings. The data types are built into the language. Use them.
I have not explicitly used this scenario but comparing dates can be a problem depending on how the fields are compared.
eg: '28/07/2015' is not less than your startdate as 28 > 26.
You could try comparing dates reformatted into a YYYYMMDD format.
Cheers.