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];
Related
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:
Is there a function in SQL that formats a date when given the date and format string?
Similar to how .NET's DateTime.ToString(string format) method works?
I'd like to be able to call something like FORMAT(#myDateTime, 'ddMMMyyyy') and have it give me a string formatted as such.
I know the convert function works but it doesn't quite cover all formats. As an example, SQL can do "dd MMM yyyy" but not "ddMMMyyyy"
As far as I know there is no direct function that will convert date in format passed by you if you are on older SQL version. Format function is available starting from SQL 2012.
You can use convert function but it may not always suit your need.
Refer this: https://msdn.microsoft.com/en-IN/library/ms186724.aspx
To answer your question, this will give you date in DD-MMM-YYYY Format:
select REPLACE(REPLACE(CONVERT(VARCHAR,getdate(),106), ' ','-'), ',','')
For DDMMMYYYY format:
select REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR,getdate(),106), ' ','-'), ',',''),'-','')
Also, you can check the date formats supported by convert function using this:
select convert(varchar, getdate(), 101)
select convert(varchar, getdate(), 102)
select convert(varchar, getdate(), 103)
select convert(varchar, getdate(), 104)
select convert(varchar, getdate(), 105)
select convert(varchar, getdate(), 106)
select convert(varchar, getdate(), 107)
select convert(varchar, getdate(), 108)
select convert(varchar, getdate(), 109)
select convert(varchar, getdate(), 110)
select convert(varchar, getdate(), 111)
select convert(varchar, getdate(), 112)
select convert(varchar, getdate(), 113)
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
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)
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