I want to accomplish a task that both Excel’s 75min roundup output and sql server’s 75min roundup output should be same result for the date time inputs. I used the following function in sql server
CREATE FUNCTION [dbo].[RoundTime] (#Time DATETIME, #RoundToMin INT)
RETURNS DATETIME
AS
BEGIN
RETURN ROUND(CAST(CAST(CONVERT(VARCHAR,#Time,121) AS DATETIME) AS FLOAT) * (1440/#RoundToMin),0)/(1440/#RoundToMin)
END
GO
SELECT dbo.roundtime('11/2/2012 9:22:00',75)
The result I got for the above query is "2012-11-02 08:51:00"
The same scenario I applied in Excel =MROUND (A1, TIME(0,75,0))" formula with same input('11/2/2012 9:22:00 AM')
The Excel result is "11/2/2012 8:45"
I believe that the Excel result is correct and I want to achieve the same result in SQL server..
Are there any other ways to complete this task..
Related
We have an Oracle database that we access through OpenQuery for some stuff. You apparently can't do date comparisons directly in OpenQuery using the OleDB driver, so to get around this you have to convert the dates to Julien dates and compare those numbers. I have the following query that we're trying to execute this in MS SQL Server (GPROD is a Linked Server via the OleDb driver):
SELECT *
FROM OPENQUERY(GPROD, '
SELECT *
FROM ORD_HDR_HST
WHERE (cast(to_number(to_char(SHIP_DATE ,''J'')) as numeric(10,0)) >= cast(to_number(to_char(to_date(''01-JAN-2015'') ,''J'')) as numeric(10,0)) AND
cast(to_number(to_char(SHIP_DATE ,''J'')) as numeric(10,0)) <= cast(to_number(to_char(to_date(''21-SEP-2015'') ,''J'')) as numeric(10,0)) )')
This query returns no results but also produces no error.
If I execute this query in Oracle SQL Developer, it works just fine and returns thousands of rows:
SELECT *
FROM ORD_HDR_HST
WHERE (cast(to_number(to_char(SHIP_DATE ,'J')) as numeric(10,0)) >= cast(to_number(to_char(to_date('01-JAN-2015') ,'J')) as numeric(10,0)) AND
cast(to_number(to_char(SHIP_DATE ,'J')) as numeric(10,0)) <= cast(to_number(to_char(to_date('21-SEP-2015') ,'J')) as numeric(10,0)) )
The SHIP_DATE field is of type DATE and is nullable, if that matters.
Does anyone know what I can do to get this working through OpenQuery?
Edit:
I did a test of the Julien Date conversion and there's definitely something fishy going on, but I don't know what's causing it. If I execute this in Oracle:
select cast(to_number(to_char(to_date('01-JAN-2015') ,'J')) as numeric(10,0)) from dual
I get 2457024
If I execute this on SQL Server:
select * from OPENQUERY(GPROD, 'select cast(to_number(to_char(to_date(''01-JAN-2015'') ,''J'')) as numeric(10,0)) from dual')
I get 1721443
I found a solution to the problem. By specifying a mask for the date, it will provide the proper results. Using:
to_char(to_date('01-JAN-2015','DD-MON-YYYY') ,'J')
instead of
to_char(to_date('01-JAN-2015') ,'J')
Gives the same result through OpenQuery and directly from Oracle.
I have a problem inserting a date from a VB.net Program to a SqlServer2012 instance.
First here is how i generate the data (Vb.net)
ExitTime = CDate("1.1.1970 00:00:00").AddSeconds(currentField).ToLocalTime
We add this value to a stored procedure (Vb.net)
With comsql5.Parameters.AddWithValue("#ExitTime", ExitTime)
In the Sql Server stored procedure
#ExitTime datetime, [...]
[...]
Insert into [table] ([ExitTime]) VALUES (#ExitTime)
Here is the output of the exit time in the vb.net
Exit Time : 08/07/2014 2:06:31 PM
Here is the same row in the Sql server database
2014-08-07 14:06:31.000
What I would like to see in the database is 2014-07-08 14:06:31.00
Because another part in the program does a check on the field but as a String... and it does not match because it flip the month and day
EDIT: TO be clear, I can't change the other part that does the comparison as a string. I know this is a poor way to compare datetime.
Thank for your time
Have you tried using the Convert function?
SELECT CONVERT (VARCHAR, getdate(), 121);
Check this links for more information MSDN - CAST and CONVERT and SQL Server Datetime Format
I have the following query,
SELECT * FROM LOGS
WHERE CHECK_IN BETWEEN CONVERT(datetime,'2013-10-17') AND CONVERT(datetime,'2013-10-18')
this query not returning any result, but the following query return the result,
SELECT * FROM LOGS WHERE CHECK_IN >= CONVERT(datetime,'2013-10-17')
why the first query not returning any result? If I did any mistake pls correct me.
Do you have times associated with your dates? BETWEEN is inclusive, but when you convert 2013-10-18 to a date it becomes 2013-10-18 00:00:000.00. Anything that is logged after the first second of the 18th will not shown using BETWEEN, unless you include a time value.
Try:
SELECT
*
FROM LOGS
WHERE CHECK_IN BETWEEN
CONVERT(datetime,'2013-10-17')
AND CONVERT(datetime,'2013-10-18 23:59:59:998')
if you want to search the entire day of the 18th. I set miliseconds to 998 because SQL Server was pulling in 2013-10-19 00:00:00:0000 in the query.
SQL DATETIME fields have milliseconds. So I added 999 to the field.
Does the second query return any results from the 17th, or just from the 18th?
The first query will only return results from the 17th, or midnight on the 18th.
Try this instead
select *
from LOGS
where check_in >= CONVERT(datetime,'2013-10-17')
and check_in< CONVERT(datetime,'2013-10-19')
From Sql Server 2008 you have "date" format.
So you can use
SELECT * FROM LOGS WHERE CONVERT(date,[CHECK_IN]) BETWEEN '2013-10-18' AND '2013-10-18'
https://learn.microsoft.com/en-us/sql/t-sql/data-types/date-transact-sql
You don't have any error in either of your queries.
My guess is the following:
No records exists between 2013-10-17' and '2013-10-18'
the records the second query returns you exist after '2013-10-18'
You need to convert the date field to varchar to strip out the time, then convert it back to datetime, this will reset the time to '00:00:00.000'.
SELECT *
FROM [TableName]
WHERE
(
convert(datetime,convert(varchar,GETDATE(),1))
between
convert(datetime,convert(varchar,[StartDate],1))
and
convert(datetime,convert(varchar,[EndDate],1))
)
DROP TABLE IF EXISTS #TB_PERIODO_TEMP CREATE TABLE #TB_PERIODO_TEMP (DATA DATETIME)
INSERT INTO #TB_PERIODO_TEMP VALUES
('22-12-2022 00:00:00'),
('22-12-2022 23:59:59'),
('23-12-2022 00:00:00'),
('23-12-2022 23:59:59')
SELECT * FROM #TB_PERIODO_TEMP -- ALL
SELECT * FROM #TB_PERIODO_TEMP WHERE DATA
BETWEEN '22-12-2022' AND '23-12-2022' --OLÉ!!!
--BETWEEN CONSIDERING PERIODS WITH HOURS O/
SELECT * FROM #TB_PERIODO_TEMP WHERE DATA
BETWEEN '22-12-2022' AND '23-12-2022 23:59:59:999'
I need the following query below converted for SQL Server 2005, so please let me know what I should change since SQL Server doesn't support DATE_FORMAT()
How it should look converted in SQL Server so I can display proper date on my website instead of timestamp (example: 1382016108 to be 2013-06-22 10:53:22) ?
SELECT DATE_FORMAT(TimeTransfer,'%Y-%m-%d %h:%i:%s')
FROM PREMIUM where strAccountID = ?, $_SESSION['strAccountID']);
EDIT: I checked the CONVERT() function but couldn't find out how it should be exactly in my case with my query. Help is greatly appreciated.
This will work:
SELECT CONVERT( VARCHAR(20) -- Make sure the output string is long enough
, CAST('1970-1-1' As DateTime) -- The Base Date
+ CAST(TimeTransfer As Float)/(24*60*60)
-- Scale UTC (seconds) to TSQL datetime (days)
-- by dividing by the number of seconds in a day
, 120) -- The format that you want
Check the Convert function. It allows you to give format to a datetime value
SELECT CONVERT(VARCHAR(20),TimeTransfer,120)
FROM PREMIUM
WHERE strAccountID = ?, $_SESSION['strAccountID']);
Use CONVERT() function
SELECT CONVERT(varchar(20),TimeTransfer,120)
FROM PREMIUM where strAccountID = ?, $_SESSION['strAccountID']);
Note : This gives time in 24 hour format
See this link for Date formats in SQL Server
In a SQL Server 2000 DB, I have a table which holds string representations of Oracle DB dates. They are formatted like "16-MAY-12". I need to convert these to datetime. I can not seem to find a conversion style number that matches, nor can I find a function that will allow me to specify the input format. Any ideas?
This seems to work for me:
SELECT CONVERT(DATETIME, '16-MAY-12');
You can also try using TO_CHAR() to convert the Oracle values to a more SQL Server-friendly format (best is YYYYMMDD) before pulling them out of the darker side.
Follow Aaron's advice and cast to string on the Oracle side and then did a check/recast on the MS SQL side. See example below:
;WITH SOURCE AS (
SELECT * FROM openquery(lnk,
'SELECT
TO_CHAR(OFFDATE , ''YYYY-MM-DD HH24:MI:SS'') AS OFFDATE,
FROM
ORACLE_SOURCE')),
SOURCE_TRANSFORM AS
(
SELECT
CASE
WHEN ISDATE(OFFDATE) = 1 THEN CAST(OFFDATE AS DATETIME)
ELSE NULL END AS OFFDATE
FROM
SOURCE
)
SELECT * FROM SOURCE_TRANSFORM