sybase check if a datetime is within time - sybase

I have a column with datetime as the data type
I need to check if that datetime column is between 2:30pm to 3pm, "ignoring" the date
I saw an example in sql server that cast the datetime to float but that did not work in sybase...
does anyone have any idea
thanks

Floor your datetime to midnight of that date, Then use datediff() to get the elapsed time between the two.
DECLARE #dt DATETIME
SET #dt='10/06/2011 2:37:17.390PM'
SELECT
CASE
WHEN DATEDIFF(ms,CONVERT(DATETIME,CONVERT(DATE,#dt)),#dt) BETWEEN 52200000 AND 54000000
THEN 1
ELSE 0
END

Related

Converting a nonstandard US datetime string to datetime in SQL

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

SQl query datetime from nvarchar error

In SQL,
declare #date datetime ='Jul15'
select CONVERT(varchar(10),#date,105)
getting error
Conversion failed when converting date and/or time from character
string.
Please help
It is because Jul15 is not a valid SQL Server Date or DateTime value, hence the error when it is trying to convert it to date/datetime to assign it to a datetime variable.
Ideally you should be using ANSI-Standard when working with date or datetime values in SQL Server, ANSI-Standard Date value has the format of YYYYMMDD, therefore your statement should look something like this.....
declare #date datetime ='20150701'
select CONVERT(varchar(10),#date,105)

Why can't I use a datetime parameter in ssrs?

I have an SSRS Date/Time parameter generated from a shared dataset query against a SQL Server datetime field. The parameter displays correctly in a report textbox but it will not work in an embedded dataset query, even against the same table that the datetime value was generated from.
In order to use the parameter for a dataset query I have to parse both sides of a where clause to get it to work in Preview in SSDT:
(convert(varchar,invoice.snapshot_datetime,120)) = (convert(varchar,#snapshotdatetime,120))
This is tremendously inefficient.
How can I get my where clause to work without parsing the invoice.snapshot_datetime column?
Server Details
The SQL Server Language is English (United States).
SQL Server dateformat is mdy (from dbcc useroptions).
Getdate() returns '2015-05-20 10:27:56.687' in SSMS
Assuming your date range is between 1900-01-01 and 2079-06-06 you can cast to SmallDateTime to truncate the seconds out of your datetime variable:
DECLARE #DateTime datetime
SET #DateTime = CAST(CAST(#snapshotdatetime as SmallDateTime) as DateTime)
(thanks to t-clausen.dk for his answer here)
Now, since your actual column is of type DateTime, it does keep seconds (and milliseconds), and you will need to eliminate them as well.
However, using functions on your column will prevent the SQL Server from using any indexes you might have on this column, so a better approach would be to use a DateTime range:
DECLARE #FromDateTime datetime, #ToDateTime datetime
SET #FromDateTime = CAST(CAST(#snapshotdatetime as SmallDateTime) as DateTime)
Since the cast will round the minutes of the small date time up if it's over 29.998 seconds, and down if it's below 29.999 seconds. You always want to round down since it's From datetime, you need to cheke if you need to decrease a minute:
IF datepart(second, #snapshotdatetime) > 29
OR (datepart(second, #snapshotdatetime) = 29
AND datepart(millisecond, #snapshotdatetime) > 998)
SET #FromDateTime = DATEADD(minute, -1, #FromDateTime)
SET #ToDateTime = DATEADD(minute, 1, #FromDateTime)
and then, in your where clause, use this:
invoice.snapshot_datetime <= #FromDateTime
AND invoice.snapshot_datetime >= #ToDateTime
If you haven't found solution yet, try this:
select (convert(varchar,GETDATE(),112))
it will return 20180206 (yyymmdd)

converting start date to datetime as beginning of the day and end date to end of the day

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

Add a Specific Time to a Date Value

I have a stored procedure that receives a date value like this:
#AsOf date
In the query I am using this date to filter a datetime field. I need to convert the date passed in to a datetime field with the time portion set to 09:00:00 PM so I can add a filter in the WHERE clause like this:
(DateCreated < #TimeAsOfNinePM)
I know this is easy, but it's just escaping me at the moment. I'm using SQL Server 2008.
Try this out:
DECLARE #TimeAsOfNinePM datetime
DECLARE #AsOf date
SET #AsOf = '2010-10-01'
SET #TimeAsOfNinePM = dateadd(hh,21, cast(#AsOf as datetime))
PRINT #TimeAsOfNinePM
The trick is you need to convert the date datatype to a datetime datatype to add hours to it.

Resources