How can I convert a Nagios timestamp to datetime? - sql

i have got an sql server 2008. i get a icinga/nagios timestamp for server downtimes.
It is exactly in this format Wed Apr 10 14:45:00 CEST 2013.
Thats a wierd format for me. I want to cast/convert it into t-sql datetime - but dont know how.

Since CEST is +02:00
select
CONVERT(VARCHAR(3), DATENAME(WEEKDAY,
switchoffset(CAST(date_column_name as datetimeoffset),'+02:00')), 100)
+ ' ' +
CONVERT(VARCHAR(3), DATENAME(MM,
switchoffset(CAST(date_column_name as datetimeoffset),'+02:00')), 100)
+ ' ' +
DATENAME(DAY,
switchoffset(CAST(date_column_name as datetimeoffset),'+02:00'))
+ ' ' +
CONVERT(VARCHAR(8),
switchoffset(CAST(date_column_name as datetimeoffset),'+02:00'), 108)
+ ' CEST ' +
DATENAME(YEAR,
switchoffset(CAST(date_column_name as datetimeoffset),'+02:00'))
from adm_co_users
I am first Converting the Date to CEST Time Zone and then extracting the date as required.

How about this:
select Cast(
-- Day
substring(#sTimestamp, 9, 2) + ' '
-- Month
+ substring(#sTimestamp, 5, 3) + ' '
-- Year
+ substring(#sTimestamp, 26, 4) + ' '
-- Time
+ substring(#sTimestamp, 12, 8) + ' '
as datetime)
This doesn't do anything for the timezone though - I suggest you put something in for that.

Related

Concatenate Two dates and their times in SQL Server 2008

I want to concatenate two dates with their times like below in SQL Server 2008. Something like this:
2015-09-09 08:30 - 2015-09-09 09:30
I tried this method but didn't work and I used casting as well.
CONVERT(DATETIME, CONVERT(CHAR(8), S.StartTime, 112)+ '-' + CONVERT(CHAR(8), S.endtime, 108)) AS 'OccupiedTime'
it is showing result like this
2015-09-09 09:30:00:000
CONVERT(CHAR(16), s.StartTime, 120) + '-' +
CONVERT(CHAR(16), s.EndTime, 120) AS OccupiedTime
You need 2 parts of date - date only + time. You can have 2 strings and concatenate them:
SELECT
REPLACE(CONVERT(VARCHAR(50),s.StartTime,103),'/','-') + ' ' +
CONVERT(VARCHAR(5),s.StartTime,114) + ' - ' +
REPLACE(CONVERT(VARCHAR(50),s.EndTime,103),'/','-') + ' ' +
CONVERT(VARCHAR(5),s.EndTime,114) AS OccupiedDateTime
You can make quick check how it looks using:
SELECT
REPLACE(CONVERT(VARCHAR(50),GETDATE(),103),'/','-') + ' ' +
CONVERT(VARCHAR(5),GETDATE(),114) + ' - ' +
REPLACE(CONVERT(VARCHAR(50),GETDATE(),103),'/','-') + ' ' +
CONVERT(VARCHAR(5),GETDATE(),114) AS OccupiedDateTime
You can use SQL Convert function and the conversion style parameter together as follows
declare #StartTime datetime = getdate()
declare #EndTime datetime = getdate()
select convert(varchar(16), #StartTime, 120) + ' - ' + convert(varchar(16), #EndTime, 120)
If you check the code, I used varchar(16) which removes unwanted milllisecond information after conversion.
For more on SQL Server Convert datetime function on action please refer to given tutorial

select query to display Date format as DAY MMM DD YYYY in SQL server 2008

I tried like this
SELECT DATENAME(DW,GETDATE()) + ' ' + CONVERT(VARCHAR(12), SYSDATETIME(), 107)
result is
Thursday Dec 11, 2014
Required OutPut:
SELECT QUERY TO DISPLAY DATE AS SHOWN BELOW
Thu Dec 11, 2014
All you need is to take only left 3 symbols of your day of the week name:
SELECT LEFT(DATENAME(DW,GETDATE()),3) + ' ' + CONVERT(VARCHAR(12), SYSDATETIME(), 107)
SELECT CONVERT(VARCHAR(3), DATENAME(DW,GETDATE())) + ' '
+ CONVERT(VARCHAR(12), SYSDATETIME(), 107)
SELECT LEFT(DATENAME(DW,GETDATE()),3) + ' ' + CONVERT(VARCHAR(12), SYSDATETIME(), 107)
FROM yourtable

SQL - Email query range

I have a sql job which queries the database. The job is scheduled to run every 24 hours and it sends out an email with required data which has a query range from 07:30 today to 07:30 the previous day. Here is the code for the heading of my email :
INSERT INTO #ReportContentBuilder VALUES('<h4>Query Range : ' + DATENAME(WEEKDAY,#StartTimestamp)
+ ', ' + CONVERT(varchar, #StartTimestamp, 106) + ' ' + CONVERT(varchar, #StartTimestamp, 108)
+ ' (UTC) to ' + DATENAME(WEEKDAY,#EndTimestamp) + ', ' + CONVERT(varchar, #EndTimestamp, 106)
+ ' ' + CONVERT(varchar, #EndTimestamp, 108) + ' (UTC)</h4>')
Here is the value I have for #StartTimestamp:
SET #StartTimestamp = CAST((CONVERT(varchar(11), DATEADD(DAY, -1, #EndTimestamp), 106) + ' 07:30') as datetime)
Here is the expected output for my email heading :
Query Range : Wednesday, 19 Nov 2014 07:30:00 (UTC) to Thursday, 20 Nov 2014 07:30:00 (UTC)
My question is what value do I use for #EndTimestamp??
If I use GETUTCDATE() and the job runs 2 mins late then the range is incorrect. I also don't want to hardcode it because of the changes needed for daylight savings each time.
The trick is that the "dynamic" part is the difference between current time (GETUTCDATE()) and 7:30 . So with #timeDiff you can handle this dynamic part of the ecuation. Try this (replace last select with your needed insert; I only tested the output):
DECLARE #StartTimestamp datetime
DECLARE #EndTimestamp datetime = GETUTCDATE()
DECLARE #timeDiff time
SET #timeDiff = CONVERT(time, (#EndTimestamp - cast('1900-01-01 07:30:00.000' as datetime)))
SELECT #EndTimestamp = dateadd(second,
datepart(hour,#timeDiff) * -3600 +
datepart(minute,#timeDiff) * -60 +
datepart(second,#timeDiff) * -1,
#EndTimestamp)
SET #StartTimestamp = DATEADD(DAY, -1, #EndTimestamp)
SELECT #StartTimestamp, #EndTimestamp
SELECT '<h4>Query Range : ' + DATENAME(WEEKDAY,#StartTimestamp)
+ ', ' + CONVERT(varchar, #StartTimestamp, 106) + ' ' + CONVERT(varchar, #StartTimestamp, 108)
+ ' (UTC) to ' + DATENAME(WEEKDAY,#EndTimestamp) + ', ' + CONVERT(varchar, #EndTimestamp, 106)
+ ' ' + CONVERT(varchar, #EndTimestamp, 108) + ' (UTC)</h4>'

Convert SQL datetime to specific format

I have one table with one datetime colomn as below:
tbTest
ID int
SetDate datetime
and one entry like:
ID SetDate
1 04/10/2014 2:38:16 PM
I want to convert SetDate to dd-mm-yyyy hh:mm:ss PM or dd-mm-yyyy hh:mm PM
Desired Output:
ID SetDate
1 10-04-2014 2:38:16 PM
or
1 10-04-2014 2:38 PM
You can use sql convert method for getting Desired output :-23-04-2014 10:16 AM
SELECT CONVERT(VARCHAR(10), getdate(), 105) + ' ' + REPLACE(REPLACE(RIGHT('0'+LTRIM(RIGHT(CONVERT(varchar,getDate(),100),7)),7),'AM',' AM'),'PM',' PM')
You can use convert
select convert(varchar(10), getdate(), 105) + ' ' +
SUBSTRING(CONVERT(varchar, getdate(), 100), 14, 4)+ ' ' +
RIGHT(convert(varchar, getdate(), 100), 2)
SELECT replace(convert(NVARCHAR(100), getdate(), 106), ' ', '-')
+' ' + CONVERT(VARCHAR(30), CAST(GETDATE()AS Time), 100)
Use CONVERT with the appropriate styles:
SELECT CONVERT(CHAR(10), #SetDate, 110)
+ ' ' + SUBSTRING(CONVERT(CHAR(19), #SetDate, 100), 14, 4)
+ ' ' + SUBSTRING(CONVERT(CHAR(19), #SetDate, 100), 18, 2)
The first is for the date part, the second for the 12h time part and the last for the am/pm designator.
Demo
The shortest and best looking query you can use is:
SELECT CONVERT(VARCHAR(10), SetDate, 105) + ' ' +
CONVERT(VARCHAR(8), CAST(SetDate AS TIME), 100)
Output:
31-07-2001 12:00AM
No string manipulation required.
Another example:
DECLARE #Date AS DATETIME
SET #Date = '2014-31-12 18:00:00'
SELECT CONVERT(VARCHAR(10), #Date, 105) + ' ' +
CONVERT(VARCHAR(8), CAST(#Date AS TIME), 100) AS FormatDate
Outputs:
31-12-2014 6:00PM

Formatting a date/time in SQL Server 2005

I have a datetime field in a SQL Server 2005 table that has values like this:
2012-04-23 09:00:00.000
2012-04-23 14:00:00.000
The minutes, seconds, and microseconds are always zero.
I need to display a "time slot" (basically, the time plus one hour) like this:
2012/04/23 09:00 AM - 10:00 AM
2012/04/23 02:00 PM - 03:00 PM
I got what I needed using this:
SELECT SUBSTRING(CONVERT(VARCHAR, TimeSlot, 20), 1, 10) + ' ' +
RIGHT('00000' + LTRIM(SUBSTRING(CONVERT(VARCHAR(24), TimeSlot, 109), 13, 5)), 5) + ' ' +
SUBSTRING(CONVERT(VARCHAR(19), TimeSlot, 100),18,2) + ' - ' +
RIGHT('00000' + LTRIM(SUBSTRING(CONVERT(VARCHAR(24), DATEADD(hh, 1, TimeSlot), 109), 13, 5)), 5) + ' ' +
SUBSTRING(CONVERT(VARCHAR(19), DATEADD(hh, 1, TimeSlot), 100), 18, 2) AS TimeSlot
FROM MyTable
It works, but I feel so dirty.
I know I could do it easier in the code (VB .NET), but assume I have to do it in SQL.
Is there any cleaner way to do this?
Taking the built-in conversions, I would use something like this:
SELECT
convert(varchar, [TimeSlot], 111) + ' ' +
RIGHT(convert(varchar, [TimeSlot], 0), 7) + ' - ' +
RIGHT(convert(varchar, dateadd(hour, 1, [TimeSlot]), 0), 7)
FROM
[MyTable];
this gives you the line you want as
2012/04/23 9:00AM - 10:00AM
in detail:
convert(varchar, #dt, 111) converts the datetime to the Japanese format yy/mm/dd
convert(varchar, #dt, 0) converts the datetime to Apr 23 2012 9:00AM so we can use the time part
dateadd(hour, 1, #dt) adds one hour to the current datetime value
you can test it without tables with:
DECLARE #dt DATETIME
SET #dt = '2012-04-23 09:00:00'
PRINT convert(varchar, #dt, 111) + ' ' +
RIGHT(convert(varchar, #dt, 0), 7) + ' - ' +
RIGHT(convert(varchar, dateadd(hour, 1, #dt), 0), 7);
The built in date/time formats in SQL Server will only format to an actual date format, so you're up for custom formatting anyway. If you're feeding SSRS with this then you might be able to use an expression to format it within SSRS. Otherwise you're up for formatting it with the query using something like the technique you're describing.
If you're actually displaying it with a VB app then you could do that client-side.
If your date/time values are always on the hour or some consistent interval you could also make a reference table keyed on the time with the formatted interval stored as a varchar column and use that. That would save you in-lining the formatting expression into every query that needed to produce the interval description.
FollowingConcernedOfTunbridgeW's suggestion, if you maintain a two-column table with a primary key int column [hr] that goes from 0 to 23 and a char(20) column [rng] containing the space-prefixed range string for each hour
' 12:00 AM - 01:00 AM' (in the row where hr = 0)
...
' 11:00 PM - 12:00 AM' (in the row where hr = 23)
this simply query should produce what you want:
select
convert(char(10),TimeSlot, 111) + rng as TimeRange
from T join RangesByHr
on datepart(hour,TimeSlot) = hr;