I'm trying to get the datetime of an event from n SQL-server and use the date to look up data in another table.
When I select the datetime variable from the 1st table it shows as...
2017-01-01 20:41:23.000
Data is parsed into a temporary table declared via
CREATE TABLE #RunData1
(
[MyDateTime] Datetime,
[Tag] nvarchar(50),
[Hours] float,
[id] INT IDENTITY(1,1),
[searched] bit
)
I store this value in a Datetime variable #startPoint (DECLARE #startPoint dateTime) and set it using SELECT #startPoint = MyDateTime FROM #RunData1 WHERE id = #currentid_inner
But when I then try and print #startpoint it shows as Jan 1 2017 8:41PM?
I thought at first this just might be some formatting in the print command. But when I try and subtract 10 seconds from this (SET #startpoint = DateAdd(SS,-10,#startPoint)) and use it to find data after this new time. I get data from after 2017-01-01 20:41:00.000 not 2017-01-01 20:41:13.000 (data sample time is 0.5 s)
Why is the date format changing from 2017-01-01 20:41:23.000 to Jan 1 2017 8:41PM causing me to lose seconds?
You are not losing seconds - this is just how the date is being displayed.
When it comes to displaying datetime values - let the application deal with that - not the database.
Related
My table in the DB has the following format yyyy-mm-dd HH:MM:SS. but the required format for a varchar data type is dd-mm-yyyy HH:MM:SS. when I change this in the query below the code runs but it returns nothing as the table date format doesn't match the varchar data type.
I assume it's a simple data conversion but I'm quite new to this and not sure on how to get around it.
DECLARE #LastxDays TABLE (DateForReport datetime)
DECLARE #DateForReport datetime
DECLARE #Results TABLE (DateForReport DateTime,
Currency VARCHAR(20),
TotalTransactionsIn Decimal(10,2),
TotalTransactionsOut Decimal(10,2),
TotalBalances Decimal(10,2))
-- SET initial date
SET #DateForReport = CAST('26-05-2022 17:00:00' as DATETIME)
PRINT #DateForReport
-- Add a row for each day of the year up to yesterday
WHILE(#DateForReport < dateadd(dd,-1, getdate()))
BEGIN
INSERT INTO #LastxDays VALUES (#DateForReport)
SET #DateForReport = DATEADD(day, 1, #DateForReport)
END
my table result is the following:
|Date | Name |Transaction in |Transaction out|
|2022/26/05|***** |***** |***** |
I'm having a strange issue with the smalldatetime data type in SQL Server.
I have a very basic table
create table datetest (
value smalldatetime not null
)
And when I run the following
insert into datetest
values ('2016-12-29 21:30:00');
I see the value is 2016-12-29 21:30:00
Then when I run the following
update datetest
set value = '2016-12-29 21:31:30'
I see the value is 2016-12-29 21:31:00
It did not include the seconds. Why is this?
This is happening because precision of smalldatetime is 1 minute. It discards any seconds in datetime value by rounding off. For e.g:
'2014-10-10 12:13:29' is rounded off to '2014-10-10 12:13:00' and '2014-10-10 12:13:30' is rounded off to '2014-10-10 12:14:00'
This is one of the characteristics of smalldatetime over datetime.
Microsoft documentation on smalldatetime
The main differentation is that it rounds to the nearest minute. If you want to see seconds (and milliseconds) then you need to consider the datetime data type.
In your example however, it should return the value 2016-12-29 21:32:00 because it rounds up from 30 seconds to the next minute. anything less than 30 seconds gets rounded down. Example;
CREATE TABLE #DateTest (ID int, DateValue smalldatetime)
INSERT INTO #DateTest (ID, DateValue)
VALUES
(1,'2016-12-29 21:31:29')
,(2,'2016-12-29 21:31:30')
SELECT * FROM #DateTest
Output
ID DateValue
1 2016-12-29 21:31:00
2 2016-12-29 21:32:00
Some further reading links;
http://blog.sqlauthority.com/2010/06/01/sql-server-precision-of-smalldatetime-a-1-minute-precision/
http://sqlcoach.blogspot.co.uk/2007/08/sql-server-storing-time-coming-soon.html
http://sqlhints.com/2016/10/10/difference-between-smalldatetime-and-datetime-data-types-in-sql-server/
When the conversion is to datetime, the smalldatetime value is copied to the datetime value. The fractional seconds will make next nearest minutes. The following code shows the results of converting a smalldatetime value to a datetime value.
DECLARE #smalldatetime smalldatetime = '1955-12-13 12:43:10';
DECLARE #datetime datetime = #smalldatetime;
SELECT #smalldatetime AS '#smalldatetime', #datetime AS 'datetime';
--Result
--#smalldatetime datetime
------------------------- -----------------------
--1955-12-13 12:43:00 1955-12-13 12:43:00.000
Take a look MSDN Link
It rounds all seconds to minutes:
Time range:
00:00:00 through 23:59:59
2007-05-09 23:59:59 will round to
2007-05-10 00:00:00
see chapter smalldatetime Description: https://msdn.microsoft.com/en-us/library/ms182418(v=sql.120).aspx
In SQL Server 2005 I am interrogating some old legacy data and I need to combine the date component of a datetime column with the time component from another column. Here's an example:
DateColumn: 2016-05-09 00:00:00.000
TimeColumn: 1899-12-30 12:26:00.000
I need the end result converted to the following DateTime:
ResultDateTime: 2016-05-09 12:26:00.000
I tried using:
CAST(DateColumn AS DATETIME) + CAST(TimeColumn AS TIME) AS ResultDateTime
But SQL Server 2005 doesn't recognize the type TIME.
Can someone please show me a way of doing that?
Many thanks!
convert the time column to string HH:MM:SS and add to the date column
ResultDatetIme = DateColumn + convert(varchar(10), TimeColumn, 108)
You can just use DATEADD and DATEDIFF, assuming that the time column's date portion is always 30/12/1899:
declare #t table (DateColumn datetime,TimeColumn datetime)
insert into #t(DateColumn,TimeColumn) values
('2016-05-09T00:00:00.000','1899-12-30T12:26:00.000')
select DATEADD(millisecond,DATEDIFF(millisecond,'18991230',TimeColumn),DateColumn)
from #t
Result:
-----------------------
2016-05-09 12:26:00.000
As you can see following data type supported in SQL 2005:
https://msdn.microsoft.com/en-us/library/ms187819(v=sql.90).aspx
Use datetime and smalldatetime, The smalldatetime data type stores dates and times of day with less precision than datetime. The Database Engine stores smalldatetime values as two 2-byte integers. The first 2 bytes store the number of days after January 1, 1900. The other 2 bytes store the number of minutes since midnight.
datetime values are rounded to increments of .000, .003, or .007 seconds, as shown in the following table.
SELECT CAST('2016-05-09 00:00:00.000' AS DATETIME) + CAST('1900-01-01 12:26:00.000' AS smalldatetime) AS ResultDateTime
Result: 2016-05-09 12:26:00.000
So you can use datetime and smalldatetime, hope fully that will work for you.
Let me know if any issue. love to resolve :)
I'm at my wits end... Hope you all can help. I have a date column entryexpire in sql table, eg:
2013-04-12
I need to convert it to a datetime with all values in entryexpire to have 24:00:00 appended to new values, eg:
2103-04-12 24:00:00
I've tried cast, covert, adding new columns and concatenation ... All failed me.
Add new column col2 (Datetime). Issue update:
update table t1 set col2 = cast(col1, datetime) // db specific function
Drop column Col1.
Rename col2 to col1
I mean, there are few other ways of doing it as well
24:00:00 is not really an option for a date - you'd be into the next day. Here's a select statement you can use to tailor a date to your needs, but about the closest you will get to the end of the day is 23:59:59.997
DECLARE #MYDATE varchar(50) = '2013-04-12'
SELECT DATEADD(DAY,1,DATEADD(Ms,-2, CAST(#MYDATE AS DATETIME)))
This returns 2013-04-12 23:59:59.997
Alternately, you could have two different varchar columns (which it seems you tried), and query them like this to get a datetime
DECLARE #MYDATE varchar(50) = '2013-04-12'
declare #mytime varchar(50) = ' 23:59:59.998'
select CAST(#mydate + #mytime as datetime)
Again, using 23:59:59.999 causes the select to return 2013-04-13 00:00:00.000. Not sure why.
If anyone had same problem this is how i solved. I created a char column
temptime
and added a value of 23:59:00 to all records in database into that column with an update command.
i then created another column as datetime
entryexpiredatetime
with my entryexpiredate as a char and temptime as char column i used #folksymAndrews code as follows:
Blockquote
update [table] set entryexpiredatetime = CAST(ENTRYEXPIREDATE+timetime as datetime)
Blockquote
then i dropped temptime column and was left with my original entryexpiredate and my needed entryexpiredatetime columns.
Thanks for all your help. This was the best way i felt i could accomplish the task but im sure there is a less invasion way.
I'm importing an access database to sql.
The original access database has a date field that imports nicely, but the time field is text (10:00 AM, for instance).
I have over 4000 records and assume there is a way to convert 10:00 AM to 10:00:00.000 (or 07:30 PM to 19:30:00.000, etc...) so that it can be combined with the pre-existing date field (which is now like 2011-11-11 00:00:00.000).
Also, if it's easy to do the conversion and concatenation in the same process, please note.
to convert the time from am or pm 10:00 PM format into time format 10:00:00.000:
select cast(timestring as time(7))
look this:
declare #timeField as varchar(10)
set #timeField = '07:30 PM'
declare #dateField as varchar(10)
set #dateField = '1900-01-01'
select CONVERT(datetime,#dateField + ' ' + CAST(CONVERT(time, #timeField ,121) AS VARCHAR(11)),121)
In your import scripts (I assume you use some sort of SQL to E.T.L your data from Access to SQL server), you can use the convert function as such:
declare #t as time = Convert(time, '10:00PM' )
print #t -- prints 22:00:00.0000000
OR
declare #t as time = Convert(time, '10:00AM' )
print #t -- prints 10:00:00.0000000
And of course if you want to control the precision as per your example:
Convert(time(3), '10:00PM' ) -- prints 22:00:00.000