Concatenate Two dates and their times in SQL Server 2008 - sql

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

Related

sql date format order dd-mm-yyyy

I am using this code to format date with SQL:
convert(VARCHAR, DT, 20)
The output is:
2016-01-01 20:40:12
But how do I get the day first.
I would like to get: dd-mm-yyyy:
01-01-2016 20:40:12
When I look in this table:
http://www.blackwasp.co.uk/sqldatetimeformats.aspx
I do not see this formatting.
Thx!
For dd/mm/yyyy hh:mi:ss
(CONVERT(varchar, DT, 103) + ' ' + CONVERT(varchar, DT, 108)) AS MyEuropeanDate
For dd-mm-yyyy hh:mi:ss
(CONVERT(varchar, DT, 105) + ' ' + CONVERT(varchar, DT, 108)) AS MyEuropeanDate
And to understand it right for the next time, look at the official doc that Gordon wisely linked in the comments :
https://msdn.microsoft.com/en-us/library/ms187928.aspx
Could not find a native tSQL function but you can do something like this:
DECLARE #Date DATETIME = '20160101';
SELECT CASE
WHEN LEN(CAST(DAY(#Date) AS VARCHAR)) = 1 THEN '0'+CAST(DAY(#Date) AS VARCHAR)
ELSE CAST(DAY(#Date) AS VARCHAR)
END
+'-'+
CASE
WHEN LEN(CAST(MONTH(#Date) AS VARCHAR)) = 1 THEN '0'+CAST(MONTH(#Date) AS VARCHAR)
ELSE CAST(MONTH(#Date) AS VARCHAR)
END
+'-'+
CAST(YEAR(#Date) AS VARCHAR)
+' '+
SUBSTRING(CONVERT(VARCHAR, #Date, 114), 1, 8);
OR you can do this:
DECLARE #Date DATETIME = '20160101';
SELECT (REPLACE(CONVERT(varchar, #Date, 103),'/','-') + ' ' + CONVERT(varchar, #Date, 108))
RESULT:

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

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

Remove seconds from datetime

I have query as below:
select
lower(convert(varchar(10), GETDATE(), 101) +
' '+
substring(convert(varchar(20), GETDATE(), 22),9,Len(GETDATE())))
In this query i am getting result as:
06/27/2014 6:23:57 pm
I just want to remove seconds (i.e. 57) from above.
My intentded result is:
06/27/2014 6:23 pm
How can i obtain it??
I tried adding Length in the query as:
select
lower(convert(varchar(10), GETDATE(), 101) +
' '+
substring(convert(varchar(20), GETDATE(), 22),9,Len(GETDATE())+1))
But it didnt help.
Plase help me.
Try the below
SELECT CAST(DATEPART(DD,GETDATE()) AS VARCHAR)+'/'
+CAST(DATEPART(MM,GETDATE()) AS VARCHAR)
+'/'+CAST(DATEPART(YYYY,GETDATE()) AS VARCHAR)
+' '+CAST(DATEPART(HH,GETDATE()) AS VARCHAR)
+':'+CAST(DATEPART(MI,GETDATE()) AS VARCHAR)
also look at this Custom Date/Time formatting in SQL Server
Try this!
select
lower(convert(varchar(10), GETDATE(), 101) +
' '+
LEFT((substring(convert(varchar(20), GETDATE(), 22),9,Len(GETDATE())+1)), 6)
+
(RIGHT(substring(convert(varchar(20), GETDATE(), 22),9,Len(GETDATE())+1),2)))
declare #date datetime
select #date = GetDate()
select convert(nvarchar, #date, 101) + ' ' + right(convert(nvarchar, #date, 100), 6)

Combine two DateTimes

In SQL I have the following code fragment :
DECLARE
#DayPart as datetime,
#TimePart as datetime
SET #DayPart='2012-01-10 00:00:00.000'
SET #TimePart='2012-08-30 15:41:10.403'
Now I Need :
'2012-01-10 15:41:10.403'
How can I get it?
SELECT CONVERT(VARCHAR(10),#DayPart,111) + ' ' +
CONVERT(VARCHAR(10),#TimePart,108);
You should get #DayPart in 'yyyy-mm-dd' format and #TimePart in 'HH:MI:SS:MMM(24H)' format and concatenate two strings.
Try this
SELECT
CONVERT(char(10), #DayPart,126) + ' ' +
CONVERT(VARCHAR(12), #TimePart, 114)
More on SQL Server date formatting
SQL Server Date Formats
SQL2K8;
select #DayPart + cast(#TimePart as time)
SELECT REPLACE(CONVERT(VARCHAR(10),#DayPart,102),'.','-') + ' ' +
CONVERT(VARCHAR(10),#TimePart,108);
Other Date Formatting
But if you are using SQL Server 2008+
SELECT CONVERT(date, #DayPart) + ' ' + CONVERT(time, #TimePart)