I have a column that holds a string time (i.e. 8:00 AM) and I need to convert that to an actual Time and be able to append it onto a DateTime field (which is in DateTime format, not string).
Example:
Date field = 2019-06-25 00:00:00.000
Time field = 8:00 AM
Desired result: 2019-06-25 08:00:00.000
Does anyone know how I can accomplish this in SQL?
I know some people still suggest doing it in code, but I'm writing a query that does a comparison between two DateTime fields (one of which has the integrated DateTime with the proper time and the other in which the date and time are separate)
Using DATETIME + CAST(timevariable AS DATETIME) will return your expected result:
DECLARE #Time AS VARCHAR (10) = '8:00 AM'
DECLARE #DateField AS DATETIME = '2019-06-25 00:00:00.000';
SELECT #DateField + CAST(#time AS DATETIME)
Demo on db<>fiddle
It is also work with 1:00 PM
Related
I want to create a function to find a DateTime format of string type.
For Example: -
I have a string as '07/09/2016 12:00 AM' and want it in DateTime format of this string and it should be like : - MM/dd/yyyy hh:mm tt format.
So, when I pass the DateTime as string to this function then function returns me a DateTime format using SQL function.
We can try using TRY_CONVERT to convert your date string into a bona fide datetime. Then, we can use FORMAT to display the datetime in the format you want to see.
SELECT FORMAT(TRY_CONVERT(datetime, '07/09/2016 12:00 AM'),
'MM/dd/yyyy hh:mm tt', 'en-US')
07/09/2016 12:00 AM
Demo
Try this i hope this will work for you
SELECT CAST(GETDATE() AS VARCHAR(19))
Also try this
SELECT CONVERT(VARCHAR(10),GETDATE(), 101) + right(CONVERT(VARCHAR(32),GETDATE(),100),8)
I have a dataset with dates in the format as follows:
10/18/2007 8:00 A.M.
10/20/2007 10:00 A.M.
etc..
I'm having a lot of trouble finding a consistent query to convert a set of varchars in this format to datetime for insertion into a datetime column. I have tried many of the CONVERT styles (found here https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql) but none really works.
In Sql Server 2012 and up: each of these will return null when the conversion fails instead of an error.
try_convert(datatype,val)
try_cast(val as datatype)
try_parse(val as datatype [using culture])
declare #str varchar(32) = '10/20/2007 10:00 A.M.'
select try_parse(replace(#str,'.M.','M') as datetime using 'EN-us')
returns: 2007-10-20 10:00:00
rextester demo: http://rextester.com/KWCF9843
You just need to strip the periods out and then simply convert
Select try_convert(datetime,replace('10/18/2007 8:00 A.M.','.',''))
Returns
2007-10-18 08:00:00.000
Here's what I use to get a HH:MM string:
CONVERT(varchar(5), time_requested, 108)
I'm thinking there may be a more elegant way, even maybe more efficient (see similar question on How to remove the time portion of a datetime value (SQL Server)?).
Requirements:
the final result has to be easily convertible to a string (in order to be able to concatenate some field time_created with a field such as date_created).
the following 2 cases must be covered: HH:MM and HH:MM:SS.
Declare #D datetime = GetDate() -- Data Type could be Time as well
Select CONVERT(varchar(8), #D, 108) -- for HH:MM:SS 11:27:26
Select CONVERT(varchar(5), #D, 108) -- for HH:MM 11:27
Failed to mention, if 2012+ you could also use Format()
Declare #D DateTime = '2016-10-22 13:30:25'
Select Format(#D,'HH:mm') -- 13:30
Select Format(#D,'hh:mm:ss tt') -- 01:30:25 PM
Your method is fine. It produces a string representation of the value.
You can also convert to a time data type:
select cast(time_requested as time)
But note that this is "format-less" -- it contains hours, minutes, seconds, and fractions of seconds. So, your method is probably the better approach.
I'd like to take the current date in Sybase and set the time to 13:30:00.
Let say I do
select getdate()
12/5/2014 4:06:24.670 PM
I want to return
12/5/2014 13:30:00 PM
How do I transform that?
I did not have time to test this but i would suggest you convert the date part of getdate() into mm/dd/YYYY and then add "13:30:00 PM" before you convert it back from a string into a datetime object.
select convert(datetime,convert(varchar, getdate(), 101) + "13:30:00 PM")
Store PROCEDURE
--para--
#StartingDate DateTime = NULL,
#EndingDate DateTime = NULL
--condition --
dbo.callentry.CallDateTime BETWEEN ISNULL(#StartingDate,
dbo.callentry.CallDateTime) and ISNULL(#EndingDate,dbo.callentry.CallDateTime)
Question :
when i pass date '2012-09-17' from date picker as para #StartingDate, and the same as ending date . it is comparing 2012-09-17 00:00:00.000 to 2012-09-17 00:00:00.000 - will return no records
what i want is records in whole day 2012-09-17
Why not just use #StartingDate-1 then?
Or even DATEADD(d,-1,#StartingDate)
Or #EndDate + 1
Or even DATEADD(d,1,#EndDate)
DATEADD (Transact-SQL)
Returns a specified date with the specified number interval (signed
integer) added to a specified datepart of that date.
Try this:
dbo.callentry.CallDateTime >=ISNULL(#StartingDate,
dbo.callentry.CallDateTime) and dbo.callentry.CallDateTime <=ISNULL(#EndingDate,dbo.callentry.CallDateTime)
Also make sure dbo.callentry.CallDateTime this column datatype is also datetime
OR
Also reading from your question. I think when strt and end date are same you just need all the records for that day. if start and end date are same why cant you just use like below:
convert(date,dbo.callentry.CallDateTime) =convert(date,ISNULL(#StartingDate,
dbo.callentry.CallDateTime))
In case of sql server 2008 if below just convert both the sides to just date format annd compare