How do I convert text to datetime in SQL Server? - sql

I want to convert text to datetime in SQL Server.
I have text which in the format dd/mm/yyyy, but I want to convert this text format to datetime yyyymmdd.
I am using this method.
Convert(varchar(8), Right(text, 4) + Substring(text, 4, 2) + Left(text, 2), 112)
BUT I need a simpler query.

Answer:
SELECT FORMAT(CONVERT(date, '30/01/2018', 103), 'yyyyMMdd')
Explanation:
So this one is a little trickier because you've got DD/MM/YYYY, so you need to CONVERT first:
SELECT CONVERT(date, '30/01/2018', 103)
Result: 2018-01-30
Then if you are using SQL Server 2012+ use FORMAT()
SELECT FORMAT(CONVERT(date, '30/01/2018', 103), 'yyyyMMdd')
Result: 20180130

Related

How to convert varchar '15:01:2008 02:07:23 PM' to datetime in SQL Server

This is the format of data available
'15:01:2008 02:07:23 PM'
and I want to convert into
'2008-01-15 00:00:00'
SQL Server doesn't have very flexible date parsing capabilities. You can piece this together by parsing the date and times separately. Happily, if you do this as datetime, you can just add the results:
select (convert(datetime, replace(left(str, 10), ':', '/'), 103) +
convert(datetime, right(str, 12))
) as real_datetime
from (values ('15:01:2008 02:07:23 PM')) v(str)

I have date stored in varchar in SQL Server as '19-09-2020' and I want to convert it to '2020-09-19'

I have date stored in a varchar column in SQL Server as '19-09-2020'.
I want to convert it to '2020-09-19'.
I have tried this but it's not working:
select convert(varchar, '19-09-2020', 23)
You must specify the source format for convert:
convert(date, '19-09-2020',105)
This results in a DATE, if you actually want a VarChar again (of course, you shouldn't):
convert(varchar(10), convert(date, '19-09-2020',105), 23)
Do you want string functions?
select concat_ws('-', right(mycol, 4), substring(mycol, 4, 2), left(mycol, 2))
from mytable
On the other hand, if you want to generate a date from that string, then:
select convert(date, mycol, 105)
from mytable

Converting dd-mm-yyyy string to date

I am passing a date in string format 11-09-2013 in dd-mm-yyyy format from JSP. My query is:
select convert(varchar(10),'11-09-2013',106)
But it is not giving my expected output, which is:
11-SEP-2013
DECLARE #d CHAR(10) = '11-09-2013'; -- dd-mm-yyyy
SELECT UPPER(REPLACE(CONVERT(CHAR(11), CONVERT(DATETIME, #d, 103), 106), ' ', '-'));
-- if 2008+ you can use DATE instead:
SELECT UPPER(REPLACE(CONVERT(CHAR(11), CONVERT(DATE, #d, 103), 106), ' ', '-'));
-- if 2012 you can use:
SELECT UPPER(FORMAT(CONVERT(DATE, #d, 103), 'dd-MMM-yyyy'));
Result in all three cases:
11-SEP-2013
However, this can be very expensive to convert to strings etc. like this. Why do you need this output format, and where is it going? If this is being presented at the client you are much better off using the string formatting capabilities at the client.

SQL VarChar to Date

hi
i am trying to convert a VarChar date field (e.g. 20100320) to a real date field like
'dd/mm/yyyy' (e.g. 20/03/2010).
I have tried two ways:
a)
(SELECT MIN(CAST(A.DateOfAction AS Date)) AS Expr1
FROM ResAdm.Action A
WHERE (A.PersonID = P.PersonID))
AS 'Period From',
b)
(SELECT MIN(CONVERT(DATE, A.DateOfAction, 103)) AS Expr1
FROM ResAdm.Action A
WHERE (A.PersonID = P.PersonID))
AS 'Period From',
both producing the result like
yyyy-mm-dd (e.g. 2010-03-20)
but i want the result like
dd/mm/yyyy (e.g. 20/03/2010)
any help will be appreciated.
thanks.
Try this:
select convert(varchar(8), convert(datetime, min(a.DateOfAction), 112), 103)
Your problem is that once you have a date format, SQL Server will dump it out in its default date format, which you've discovered is yyyy-mm-dd. You need to convert from date to varchar to get the format you want. But to convert from date, you need to first convert to date! 112 is the format for yyyymmdd, and 103 is the format for dd/mm/yyyy, so this is why you need these formats. (Books Online reference for date formats)
Declare #date nvarchar(100)
set #date = '20100320'
select convert(varchar, CONVERT(datetime, #date, 109), 103)
You can use
convert(varchar, CONVERT(datetime, A.DateOfAction, 109), 103)

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)