Datetime entries turns into code - sql-server-2012

Hi I was generating a script so that my mssql datas would be in a sql format. After that I have a datetime column. An example entry is 2015-02-15 09:45:39.000
It turned into CAST(0x0000A44000A0DA84 AS DateTime)
How would I turn it into 2015-02-15 09:45:39 Removing the .000 and retaining the yyyy-mm-dd format?

You only want to remove the milliseconds?
convert it:
Select convert(varchar(50), getdate(), 120)
output = 2015-08-17 12:01:26
or set ms to 000:
Select DATEADD(ms, -DATEPART(ms, getdate()), getdate())
output = 2015-08-17 12:01:26.000

Related

Converting getdate() to yyyymmdd & getting the date two years back

I have found a couple of different methods to convert it. However, I still get the yyyy-mm-dd format or yyyy-mm-dd hh:mm:ss. I am currently using SQL Server 2014.
SELECT dateadd(day, convert(int, getdate()), 112)
SELECT DATEADD(YEAR, -2, convert(DATE, GETDATE(), 112))
I am doing a date range of 2 years. Thus I need the codes to the find the date two years back.
You can also use FORMAT:
select FORMAT(getdate(), 'yyyyMMdd')
Try CONVERT(char(8), GETDATE(), 112)
Also check https://technet.microsoft.com/en-us/library/ms187928(v=sql.105).aspx

converting datetime in sqlserver like yyyy-mm-dd hh:mi:ss to yyyy-mm-dd 23:59:59

Hi I want to convert the date time like below 2015-05-12 23:59:59. The hours and secs and should come like 12:59:59 this.
Ex: I want to convert today's date like below 2015-08-17 23:59:59.
Edited
for GETDATE() in sql server I will get the datetime like this 2015-08-17 17:10:54.080 this one I want to convert into 2015-08-17 23:59:59.080
Seems this question is not about formatting. So here is a solution to get last timestamps of the day:
To get the last minute of the day:
SELECT dateadd(d, datediff(d, 0, getdate()), cast('23:59:59' as datetime))
Returns:
2015-08-17 23:59:59.000
To get the last possible timestamp of the day:
SELECT dateadd(d, datediff(d, 0, getdate()), cast('23:59:59:997' as datetime))
2015-08-17 23:59:59.997
SQL Server 2005 and Later
SELECT CONVERT(VARCHAR(19), GETDATE(), 120)
SQL Server 2012 and later versions
SELECT FORMAT(GETDATE() , 'yyyy-MM-dd HH:mm:ss')
Either will return:
2015-08-17 12:42:25
You can use CAST to TIME(0) what will return your time in following format:
HH:mm:ss
SELECT CAST('2015-08-17 12:59:59' AS TIME(0))
OUTPUT
12:59:59
CORRECTION
Misunderstood your question before, so provided how to get only time, for date + time in following format: yyyy-MM-dd HH:mm:ss you can use in following:
SELECT CONVERT(CHAR(19), '2015-08-17 12:59:59.154', 20)
OUTPUT
2015-08-17 12:59:59
You need to use convert() here, try below query
SELECT CONVERT(char(19), GetDate(),121)
Output :
2015-08-17 11:37:29

Extract date as UNIX timestamp from already heavily formatted SQL datetime string

I have the following piece of SQL code that does the following:
Obviously looks at a datetime field (stored every 5 minutes)
Averages and rounds above-mentioned datetime field to hour. The result looks like this:
2012-02-11 16:00:00.000
2012-02-11 17:00:00.000
2012-02-11 18:00:00.000
2012-02-11 19:00:00.000
And here's the code:
DATEADD(hh, DATEPART(hh, LogDetails.lastupdated),
DATEADD(d, DATEDIFF(d, 0, LogDetails.lastupdated), 0)) AS TimePeriod
This works like a charm and the related objects average out just fine. I now need to turn this datestamp into UNIX Timestamp (for Flot charting purposes). Can someone help a bit here? Whatever I seem to be doing isn't working, all the UNIX timestamps come in the same.
EDIT: This is finally working !!
DATEDIFF(second, '1970/01/01 00:00:00',
DATEADD(hh, DATEPART(hh, LogDetails.lastupdated),
DATEADD(d, DATEDIFF(d, 0, LogDetails.lastupdated), 0))) as UXTIME
Working Example:
DECLARE #gmtOffset AS INT = DATEDIFF(second, GETDATE(), GETUTCDATE());
DECLARE #someDates TABLE (d DATETIME);
INSERT INTO #someDates(d) VALUES('2012-02-11 16:00:00.000');
INSERT INTO #someDates(d) VALUES('2012-02-11 17:00:00.000');
INSERT INTO #someDates(d) VALUES('2012-02-11 18:00:00.000');
INSERT INTO #someDates(d) VALUES('2012-02-11 19:00:00.000');
SELECT DATEADD(second, #gmtOffset, d) AS time_in_gmt,
DATEDIFF(second, '1970/01/01 00:00:00', DATEADD(second, #gmtOffset, d)) AS unix_timestamp
FROM #someDates;

Convert seconds to datetime in SQL Server

How to convert seconds to datetime? I try this, but results are not correct:
CONVERT(datetime, DATEADD(ms, dateTimeInSeconds, 0))
Here is an example: 1900-01-15 21:58:16.287 it's should be something like this 2010-11-02 14:56:50.997
Given your example, try this:
select DATEADD(s, dateTimeInMilliseconds, '19700101')
When you use the value zero for date, this is converted to 1900-01-01. Use the specific date that you have selected as epoch:
convert(datetime, dateadd(ms, dateTimeInMilliseconds, '2010-01-01'))
Note that the datetime data type doesn't have millisecond precision, the resolution is 1/300 second. If you for example have four milliseconds and convert it, you get 2010-01-01 00:00:00.003 rather than 2010-01-01 00:00:00.004. If you need to preserve the millisecond resolution, you need to use the datetime2 data type:
convert(datetime2, dateadd(ms, dateTimeInMilliseconds, cast('2010-01-01' as datetime2)))
Edit:
To use seconds instead of milliseconds, use s instead of ms in the dateadd call:
convert(datetime, dateadd(ms, dateTimeInSeconds, '1970-01-01'))
Informative time span string:
declare #seconds int = 93825
convert(varchar,(#seconds/86400)) + 'd:' + format(dateadd(ss,#seconds,0),'HH\h:mm\m:ss\s')
Result: 1d:02h:03m:45s

sql server: what's wrong with my date data?

i have a column with dates, but it is a varchar:
8/31/2010 9:48
8/31/2010 9:49
8/31/2010 9:51
8/31/2010 9:52
8/31/2010 9:55
8/31/2010 9:59
8/31/2010 10:11
8/31/2010 10:13
8/31/2010 10:16
8/31/2010 10:37
8/31/2010 10:42
i made sure that none of these will be a BAD date:
SELECT *
FROM qcvalues.dbo.batchinfo
WHERE ISDATE(reporttime) <> 1
this returned 0 results
question:
i need to return dates between a certain range:
select rowid from qcvalues.dbo.batchinfo where CONVERT(DATE, Substring( reporttime, 1, LEN(reporttime)), 103)
between cast('2010-08-01' as datetime) and CAST('2010-08-31' as datetime)
and i am getting this error;
Msg 241, Level 16, State 1, Line 2
Conversion failed when converting date and/or time from character string.
what is wrong with my conversion?
If you need to store dates then use a datetime column in the future
does this work?
WHERE CONVERT(DATE,RTRIM(reporttime))
BETWEEN '2010-08-01' and '2010-08-31'
If not use SET DATEFORMAT MDY before running it
And if you have to store it in a varchar column then use YYYYMMDD format...that way you can do
WHERE reporttime like '201008%' if you want August 2010
This will solve your problem:
select rowid
from qcvalues.dbo.batchinfo
where
CONVERT(DATE, reporttime, 101) >= '20100801'
-- style 101, not 103
-- also notice date conversion invariant format YYYYMMDD with no separators
AND CONVERT(DATE, reporttime, 101) < '20100901'
-- using BETWEEN with an end date of '8/31/2010' will skip
-- times between '8/31/2010 00:00:00.003' and '8/31/2010 23:59:59.997'
Try this to see what the problem is:
select convert(datetime, '8/31/2010 9:48', 103)
select convert(datetime, '8/31/2010 9:48', 101)
put SET DATEFORMAT MDY before your query.
This will strip out the time portion too
select rowid
from qcvalues.dbo.batchinfo
Where cast(floor(cast(cast(reportTime as datetime)as float))as datetime)
between cast('2010-08-01' as datetime)
and cast('2010-08-31' as datetime)
Remember, this CAST('2010-08-31' as datetime) will have its time portion as 00:00.
Consider casting your varchar data as smalldatetime, and being specific about the boundaries of times in your between. No need to be converting, substring, etc. Just one CAST will do.
Consider this as a potential solution:
SELECT rowid from qcvalues.dbo.batchinfo
WHERE CAST(reporttime as smalldatetime)
BETWEEN '2010-08-01' AND '2010-08-31 23:59'