SQL - Email query range - sql

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>'

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

how to get today's date in sql with specific format

I currently have this format in my table:
2015-03-19 10:33:16.983
but I would like to convert it into this format:
3/18/2015 12:00:00 AM.
How can I get that format?
select myDate from myTable
You are looking for the Convert() function.
Something like
SELECT CONVERT(varchar(10),GETDATE(),3) + ' ' + CONVERT(varchar(15),CAST(getdate() AS TIME),100)
You can change 3 as per your local. 3 here will mean it in DD/MM/YYYY format i.e, British and French local.
Copy and paste this and alter as you need:
DECLARE #StartTimestamp datetime
SET #StartTimestamp = CAST((CONVERT(varchar(11), DATEADD(DAY, -1, GETUTCDATE()), 106)) AS datetime)
SELECT CONVERT(varchar, #StartTimestamp, 103) + CONVERT(varchar, #StartTimestamp, 108)
Use Convert
SELECT CONVERT(VARCHAR(25), myDate, 101) + ' ' + CONVERT(VARCHAR(25), myDate, 108) + ' ' + RIGHT(CONVERT(VARCHAR(19),myDate),2)
FROM myTable
This would give you the exact output you requested
mm/dd/yyyy hh:mi:ss AM
(Or PM depending on time)
you need to use two converts with a cast and a right function:
SELECT CONVERT(VARCHAR(10), getdate(), 101) +
' ' + CONVERT(VARCHAR(15), getdate(), 108) +
' ' + RIGHT(CONVERT(VARCHAR(20),getdate()),2)
OUTPUT: 03/19/2015 18:44:19 PM

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

Concatenating two TIME columns in SQL Server 2012

I want to concatenate two TIME columns and show as one column.
Example:
FromTime: 9:00
ToTime: 12:00
Result should be:
9:00-12:00
Generic SQL:
-- hh:mm:ss
SELECT 'result:' + CONVERT(CHAR(6), FromTime, 8) + '-' + CONVERT(CHAR(6), ToTime)
FROM yourTable
MySQL:
-- hh:mm
SELECT 'result:' + DATE_FORMAT(FromTime, '%H:%i') + '-' + DATE_FORMAT(ToTime, '%H:%i')
FROM yourTable
SQL Server:
-- hh:mm
SELECT 'result:' + convert(char(2), DATEPART(hh, FromTime)) + ':' +
CONVERT(CHAR(2), DATEPART(mm, FromTime)) + '-' +
CONVERT(CHAR(2), DATEPART(hh, ToTime)) + ':' +
CONVERT(CHAR(2), DATEPART(mm, ToTime))
FROM yourTable
declare #FromTime time
declare #ToTime time
set #FromTime='9:00'
set #ToTime='12:00'
select cast(#FromTime as varchar(10))+ '-' + cast(#ToTime as varchar(10)) as result
sql demo
You can use convert
select convert(VARCHAR(5),getdate(),108) + ' - ' + convert(VARCHAR(5),getdate()-1,108)

How can I convert a Nagios timestamp to datetime?

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.