Convert SQL datetime to specific format - sql

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

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

Getting only date time mm dd yyy hh mm ss

How to get this: 2011-02-09 13:09:00
I want this format in sql:
MM DD YYYY HH:MM:SS
07/22/2014 01:51:57 AM/PM
You may try like this:
QUERY
SELECT CONVERT(VARCHAR(10), GETDATE(), 104) + ' ' + CONVERT(VARCHAR(8), GETDATE(), 108)
or try this:
DECLARE #dt DATETIME = Getdate();
DECLARE #time VARCHAR(32) = CONVERT(VARCHAR, #dt, 109);
SELECT CONVERT(CHAR(10), #dt, 101) + ' '+RIGHT('0'+
Substring(#time, Len(#time) -
Charindex(' ', Reverse(#time)) +2,
Charindex(' ', Reverse(#time)) -
Charindex(':', Reverse(#time))-1),8)
+ ' '+RIGHT(#time, 2);
You can run this sql, if you are querying the data:
alter session set nls_date_format='DD-MON-RR HH:MI:SS'

SQL date format: dd/mm/yyyy hh:ss

I have been trying to find a date format: dd/mm/yyyy hh:mm.
I am using convert (varchar, rc.CreatedDateTime, ???). 131 is not what i need either.
I seem to be able to find an awful lot that are very close, but not this particular one, am I missing something glaringly obvious or is there another function I can use??
You can use this, just replace the getdate() with your field:
select convert(char(10), getdate(), 103) + ' '
+ convert(char(8), getdate(), 108)
which gives you the results:
14/03/2012 17:05:47
If you just want the hour and seconds then:
select convert(char(10), getdate(), 103) + ' '
+ Left(convert(char(8), getdate(), 108), 2) + ':'
+ Right(convert(char(8), getdate(), 108), 2)
Here is a helpful link with date/time conversions:
http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/
Based on your comment that it should be dd/mm/yyyy hh:mm then the query would be:
select convert(char(10), getdate(), 103) + ' '
+ Left(convert(char(8), getdate(), 108), 5)
OR without the LEFT()
select convert(varchar(10), getdate(), 103) + ' '
+ convert(char(5), getdate(), 108)
Assuming that you need dd/mm/yyyy hh:mm not dd/mm/yyyy hh:ss
SELECT convert (varchar(10), rc.CreatedDateTime, 103) + ' ' +
convert(varchar(5), rc.CreatedDateTime, 108)
SELECT CONVERT(CHAR(10), rc.CreatedDateTime, 103) + ' '
+ CONVERT(CHAR(5), rc.CreatedDateTime, 108)
FROM dbo.[table_name];