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

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

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:

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)

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

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];

Convert SQL DateTime format

How can I display a DATETIME value (2010-12-02 15:20:17.000) as 02/12-2010 15:20?
For SQL Server:
select stuff(convert(varchar, getdate(), 105), 3, 1, '/') + ' ' + left(convert(varchar, getdate(), 8), 5)
DateTime is a DateTime is a DateTime - it just holds a date and time and doesn't have any string representation, really.
See the CAST and CONVERT topic in the SQL Server Books Online for details - it shows all supported date formats that SQL Server supports.
For your source format (2010-12-02 15:20:17.000) you could probably use style no. 121
DECLARE #source VARCHAR(50)
SET #source = '2010-12-02 15:20:17.000'
DECLARE #Date DATETIME
SELECT #Date = CONVERT(DATETIME, #source, 121)
SELECT #Date
but your target format is a bit odd..... I don't see any "out of the box" style that would match your needs. You'll need to use some string manipulation code to get that exact format.
Use MSSQL's build-in function to convert datetime to string with format,
SELECT CONVERT(VARCHAR(8), GETDATE(), 1) AS [MM/DD/YY] --2/5/12
SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY] --5/2/2012
You need to create custom function to get various format to use like this;
SELECT dbo.ufn_FormatDateTime(GETDATE(),'YYYY-MM-DD HH:mm:SS tt')
--Output : 2012-02-05 01:58:38 AM
SELECT dbo.ufn_FormatDateTime(GETDATE(),'(dddd) mmmm dd, yyyy hh:mm:ss.fff tt')
--Output : (Sunday) February 05, 2012 01:58:38.723 AM
SELECT dbo.ufn_FormatDateTime(GETDATE(),'dd/MM/yyyy')
--Output : 05/02/2012
SELECT dbo.ufn_FormatDateTime(GETDATE(),'yyyy MMM, dd (ddd) hh:mm:ss tt')
-- Output : 2012 Feb, 05 (Sun) 01:58:38 AM
Get the code snippet from this link.
http://www.tainyan.com/codesnippets/entry-62/sql-server-date-time-format-function.html
http://msdn.microsoft.com/en-us/library/ms189491.aspx
Is this what you're looking for?
Assuming Oracle:
select TO_CHAR(SYSDATE, "dd/mm-yyyy HH24:mi")
from DUAL;
Assuming SQL Server:
select STR(DATEPART(DAY, GETDATE()), 2)
+ '/'
+ STR(DATEPART(MONTH, GETDATE()), 2)
+ '-'
+ STR(DATEPART(YEAR, GETDATE()), 4)
+ ' '
+ STR(DATEPART(HOUR, GETDATE()), 2)
+ ':'
+ STR(DATEPART(MINUTE, GETDATE()), 2);
Little example I use for Germany and Switzerland: dd.mm.yyyy hh:mm
SELECT CONVERT(varchar, GETDATE(), 104) + ' ' + LEFT(CONVERT(varchar, GETDATE(), 108), 5)