Convert SQL DateTime format - sql

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)

Related

Extract date from datetime column - SQL Server Compact

I'm using SQL Server Compact 4.0 version, and although it might seem a simple thing to find in google, the examples I've tried none of them work.
My column signup_date is a DateTime with a value 04-09-2016 09:05:00.
What I've tried so far without success:
SELECT FORMAT(signup_date, 'Y-m-d') AS signup_date;
SELECT CONVERT(signup_date, GETDATE()) AS signup_date
SELECT CAST(data_registo, date) AS signup_date
I found that I could use DATEPART function, but that would force me to concat the values, is this the right path to follow? If so, how do I concat as Y-m-d?
SELECT DATEPART(month, signup_date)
SQL Server Compact has no date type.
If you don't want to see the time, convert the datetime value to a string:
SELECT CONVERT(nvarchar(10), GETDATE(), 120)
(This has been tested and actually works against SQL Server Compact)
you were actually on track with the CAST function just a slight error in the syntax. In the CAST function, there needs to be 'as' i.e CAST(data_registo as date)
SELECT CAST(data_registo as date) AS signup_date;
Most of the answers seek to achieve same thing but the explanation to the codes is not enough
CONVERT(date, Date_Updated, 120)
this code does the conversion with mssql. The first item 'date' is the datatype to return. it could be 'datetime', 'varchar', etc.
The second item 'Date_Updated' is the name of the column to be converted.
the last item '120' is the date style to be returned. There are various styles and the code entered will determine the output. '120' represent YYYY-MM-DD.
Hope this helps
The old fashioned way of doing this in SQL Server might work for your purposes:
select dateadd(day, datediff(day, 0, signup_date), 0)
The datediff() gets the number of days (as an integer) since time 0. The dateadd() adds this number back.
If you don't like 0 as a date, you can put any valid date in its place:
select dateadd(day, datediff(day, '2000-01-01', signup_date), '2000-01-01')
EDIT:
If you simply don't want to see the time, convert the date to a string:
select convert(nvarchar(10), signup_date, 120)
(I recommend the YYYY-MM-DD format, but others are available.)
I have tried this and many other solutions. I wanted a generic solution that would work with any LCID. My solution is a bit of convoluted code, but it works perfectly for me. It's a booking system where I needed to find out who was arriving on a particular date. ArriveDate is the column, d is the DATE I want.
SQL = "SELECT * FROM tablename WHERE dateadd(day, datediff(day, 0,
ArriveDate), 0)=' " & Format(d, "yyyy-MM-dd") & " ' "
This will return only date value in original datetime type. So you can do any comparison using the output
SELECT convert(datetime, CONVERT(nvarchar(10), GETDATE(), 120))
Just saw the Question today, a bit late I know :) but maybe this will help..,
select convert(date,(convert(varchar(20),'04-09-2016 09:05:00')))
select convert(nvarchar, getdate(), 1) = 09/25/19
select convert(nvarchar, getdate(), 2) = 19.09.25
select convert(nvarchar, getdate(), 3) = 25/09/19
select convert(nvarchar, getdate(), 4) = 25.09.19
select convert(nvarchar, getdate(), 5) = 25-09-19
select convert(nvarchar, getdate(), 6) = 25 Sep 19
select convert(nvarchar, getdate(), 7) = Sep 25, 19
select convert(nvarchar, getdate(), 10) = 09-25-19
select convert(nvarchar, getdate(), 11) = 19/09/25
select convert(nvarchar, getdate(), 12) = 190925
select convert(nvarchar, getdate(), 23) = 2019-09-25
select convert(nvarchar, getdate(), 101) = 09/25/2019
select convert(nvarchar, getdate(), 102) = 2019.09.25
select convert(nvarchar, getdate(), 103) = 25/09/2019
select convert(nvarchar, getdate(), 104) = 25.09.2019
select convert(nvarchar, getdate(), 105) = 25-09-2019
select convert(nvarchar, getdate(), 106) = 25 Sep 2019
select convert(nvarchar, getdate(), 107) = Sep 25, 2019
select convert(nvarchar, getdate(), 110) = 09-25-2019
select convert(nvarchar, getdate(), 111) = 2019/09/25
select convert(nvarchar, getdate(), 112) = 20190925
select convert(nvarchar, getdate(), 8) = 13:48:36
select convert(nvarchar, getdate(), 14) = 13:49:48:713
select convert(nvarchar, getdate(), 24) = 13:49:57
select convert(nvarchar, getdate(), 108) = 13:50:07
select convert(nvarchar, getdate(), 114) = 13:50:14:490
select convert(nvarchar, getdate(), 0) = Sep 25 2019 1:50PM
select convert(nvarchar, getdate(), 9) = Sep 25 2019 1:50:31:813PM
select convert(nvarchar, getdate(), 13) = 25 Sep 2019 13:50:39:307
select convert(nvarchar, getdate(), 20) = 2019-09-25 13:50:49
select convert(nvarchar, getdate(), 21) = 2019-09-25 13:50:58.923
select convert(nvarchar, getdate(), 22) = 09/25/19 1:51:07 PM
select convert(nvarchar, getdate(), 25) = 2019-09-25 13:51:14.473
select convert(nvarchar, getdate(), 100) = Sep 25 2019 1:51PM
select convert(nvarchar, getdate(), 109) = Sep 25 2019 1:51:32:227PM
select convert(nvarchar, getdate(), 113) = 25 Sep 2019 13:51:38:740
select convert(nvarchar, getdate(), 120) = 2019-09-25 13:51:50
select convert(nvarchar, getdate(), 121) = 2019-09-25 13:51:57.153
select convert(nvarchar, getdate(), 126) = 2019-09-25T13:52:03.627
Use this, i had the same problem
SELECT CAST(data_registo as date) AS "signup_date"
To get a string value, use CONVERT
select convert(varchar(10), signup_date, 11)
Check here for various formats:
https://msdn.microsoft.com/en-us/library/ms187928.aspx
To get a DATE, and just strip out the time, do this
Select Cast (signup_date as DATE)

SQL Server - converting date to D-MMM-YYYY format

How can I get the string from GETDATE() in D-MMM-YYYY format, e.g 3 May 2016
If I use CONVERT(VARCHAR, GETDATE(), 106), I would get a leading zero on day which is not what I want.
If you are on SQL Server 2012 or later, use FORMAT:
SELECT FORMAT(GETDATE(), 'd MMM yyyy')
Edit: some of the answers below are just flat-out wrong so I'm adding a solution for older versions of SQL Server. 2005 is the earliest that I can get my hands on:
SELECT CASE
WHEN CONVERT(varchar(20), GETDATE(), 106) LIKE '0%'
THEN SUBSTRING(CONVERT(varchar(20), GETDATE(), 106), 2, 20)
ELSE CONVERT(varchar(20), GETDATE(), 106)
END
SELECT case when left(convert(varchar(20),[DateColumn],106),1) ='0'
then right(convert(varchar(20),[DateColumn],106),len(convert(varchar(20),[DateColumn],106))-1)
else convert(varchar(20),[DateColumn],106)
end
FROM [DB].[dbo].[Table]
Some sample output :
29 Apr 2016
2 Apr 2016
If you cannot use FORMAT (Below SQL Server 2012)
DECLARE #date DATE = '20160503'
SELECT REPLACE(DATEPART(DAY, #date),' 0','') + ' ' +
CONVERT(CHAR(3), #date, 0) + ' ' +
CAST(DATEPART(YEAR, #date) AS CHAR(4))

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

Date Format conversion in SQL

I want to convert date format from 01/09 to January 2009 , 09/03 to September 2003 etc. Is this possible in SQL? Please let me know if there is a API for the same.
if you have a DateTime column in your table, it's possible
SELECT DATENAME(MM, YOUR_DATE_COLUMN) + ' ' + CAST(YEAR(YOUR_DATE_COLUMN) AS VARCHAR(4)) AS [Month YYYY]
http://www.sql-server-helper.com/tips/date-formats.aspx
You should look here.
It's rather simple.
You should first convert it to a datetime. Then you can easily apply any formating when you read it later.
declare #d varchar(10);
set #d = '01/09'
select
--cast(#d as datetime) as d1, --syntax error converting char string
cast('20' + right(#d, 2) + '-' + left(#d, 2) + '-01' as datetime) as d2
then convert it to mmm yyyy using rm's answer
select datename(month, GETDATE()) + ' '+ substring(convert(varchar, GETDATE(), 100),8,4)