SQL - Convert varchar to datetime - sql

Any way to convert this varchar -> "18-Jan-2015 12:43:51" to datetime in SQL database?
Thanks.

Just like everybody said above, the best way to do it is either with convert or cast, besides, this is very basic sql
here are both cases:
SELECT CAST('18-Jan-2015 12:43:51' AS DATETIME) AS DATE
SELECT CONVERT (DATETIME, REPLACE('18-Jan-2015 12:43:51', '-', ' '))

Use convert with replace:
Select convert(datetime, replace('18-Jan-2015 12:43:51', '-', ' '), 113)

Try this:
SELECT (CONVERT(DATETIME,LEFT('18-Jan-2015 12:43:51',23),101))

You can use statement like below
Select convert(datetime, replace('18-Jan-2015 12:43:51', '-', ' '))

SELECT CAST('18-Jan-2015 12:43:51' AS DATETIME)

Related

SQL Server CONVERT and CAST not returning the correct date format

I have a table in SQL Server with a column RGSTR_DATE that is of varchar(10) data type, and it has values like 2016-01-23, 1998-08-12, etc...
I want to select and convert those values to 20160123, 19980812, etc...
I tried running these queries:
SELECT CONVERT(DATE, CAST(RGSTR_DATE AS DATE), 112)
FROM [project].[dbo].[my_table];
SELECT CONVERT(VARCHAR(10), RGSTR_DATE, 112)
FROM [project].[dbo].[my_table];
But the results that came back were still 2016-01-23, 1998-08-12 etc...
What am I doing wrong?
Did you try
SELECT CONVERT(VARCHAR(10),cast(RGSTR_DATE as date),112)
You're converting a varchar to a date, but all you need to do is remove the hyphens.
SELECT REPLACE(RGSTR_DATE, '-', '')
FROM [project].[dbo].[my_table]

Convert date to nvarchar and merge two columns

I am using SQL Server 2008 and I have two columns in date format:
Column_1: [2014-12-19]
Column_2: [2015-08-31]
I want to merge them and change the the data type to NVARCHAR.
I tried this code
CONVERT(NVARCHAR,[ Column_1])+CONVERT(NVARCHAR,[Column_2])AS TEST
but I get this result:
2014-12-192015-08-31
instead of 2014121920150831 without the hyphens.
Could you please help?
Thanks
Although you can fiddle around with conversion codes, just use replace:
REPLACE(CONVERT(NVARCHAR(255), Column_1) + CONVERT(NVARCHAR(255), Column_2), '-', '') AS TEST
Or, if you don't want to be dependent on the local date format:
CONVERT(NVARCHAR(255), Column_1, 112) + CONVERT(NVARCHAR(255), Column_2, 112) AS TEST
CONVERT has a third parameter which determines the format of the date/time. See here for definition. Code 112 will give you what you want.
You can also use REPLACE to remove the hyphens.
Try this
DECLARE #date1 date = '2014-12-19',
#date2 date = '2015-08-31'
SELECT CONVERT(VARCHAR(8), #date1, 112)+CONVERT(VARCHAR(8), #date2, 112)

SQL convert datetime statement

Does anyone know how do I convert the following SQL Statement into ddmmyy without has any delimiter?
convert(varchar(10), '2015-06-01 00:00:00.000', ??)
For example 2015-06-01 (yyyy-mm-dd) want to convert as 010615 (ddmmyy).
You can use Convert function providing it with the style:
SELECT REPLACE(CONVERT(varchar(10), GETDATE(), 5), '-', '')
You could use something like this
SELECT (REPLACE(CONVERT(nchar(8), GETDATE(), 3), '/', ''))
SELECT FORMAT(GETDATE(), 'ddMMyy')
Tested with MS SQL Server 2012. You can use a custom format of your choice.

how to format getdate into YYYYMMDDHHmmSS

In SQL Server how do I format getdate() output into YYYYMMDDHHmmSS where HH is 24 hour format?
I've got the YYYYMMDD done with
select CONVERT(varchar,GETDATE(),112)
but that is as far as I got.
Thanks.
Just for anyone searching for this functionality that has SQL Server 2012 you can use the FORMAT function:
SELECT FORMAT ( GETDATE(), 'yyyyMMddHHmmss') AS 'Custom DateTime'
This allows any .NET format strings making it a useful new addition.
select replace(
replace(
replace(convert(varchar(19), getdate(), 126),
'-',''),
'T',''),
':','')
Close but not exactly what you are asking for:
select CONVERT(varchar, GETDATE(), 126)
e.g.
2011-09-23T12:18:24.837
(yyyy-mm-ddThh:mi:ss.mmm (no spaces), ISO8601 without timezone)
Ref: CAST and CONVERT
There is no way to specify a custom format with CONVERT(). The other option is to perform string manipulation to create in the format you desire.
Try this:
select CONVERT(varchar, GETDATE(), 120)
e.g.
2011-09-23 12:18:24
(yyyy-mm-dd hh:mi:ss (24h) ,ODBC canonical).
Hth.
Another option!
SELECT CONVERT(nvarchar(8), GETDATE(),112) +
CONVERT(nvarchar(2),DATEPART(HH,GETDATE())) +
CONVERT(nvarchar(2),DATEPART(MI,GETDATE())) +
CONVERT(nvarchar(2),DATEPART(SS,GETDATE()));
converting datetime that way requires more than one call to convert. Best use for this is in a function that returns a varchar.
select CONVERT(varchar,GETDATE(),112) --YYYYMMDD
select CONVERT(varchar,GETDATE(),108) --HH:MM:SS
Put them together like so inside the function
DECLARE #result as varchar(20)
set #result = CONVERT(varchar,GETDATE(),112) + ' ' + CONVERT(varchar,GETDATE(),108)
print #result
20131220 13:15:50
As Thinhbk posted you can use select CONVERT(varchar,getdate(),20) or select CONVERT(varchar,getdate(),120) to get quite close to what you want.
select CONVERT(nvarchar(8),getdate(),112) +
case when Len(CONVERT(nvarchar(2),DATEPART(HH,getdate()))) =1 then '0' + CONVERT(nvarchar(2),DATEPART(HH,getdate())) else CONVERT(nvarchar(2),DATEPART(HH,getdate())) end +
case when Len( CONVERT(nvarchar(2),DATEPART(MI,getdate())) ) =1 then '0' + CONVERT(nvarchar(2),DATEPART(MI,getdate())) else CONVERT(nvarchar(2),DATEPART(MI,getdate())) end

How to convert a date 15-May-2019 in SQL to 2019/05/15 using PATINDEX

I need to convert a date in SQL. The date as is 15-May-2019 and it should display as 2019/05/15.
This is the code I have so far
CASE WHEN WHEN LEN(AgeGroup) > 1 THEN PATINDEX ('%[A-Z]%', date)
I'm not completely sure how to use Patindex. Can someone please help me to fix this?
I would recommend just converting to the date data type:
select try_convert(date, '15-May-2019')
If you want it with slashes, you can produce a string instead:
select replace(convert(varchar(10), try_convert(date, '15-May-2019'), 120), '-', '/')
Use the below code and you can refer the link for other formats.https://www.mssqltips.com/sqlservertip/1145/date-and-time-conversions-using-sql-server/
SELECT CONVERT(varchar,(cast('15-may-2019' as date)), 111)
PATINDEX is slightly overkilling.
Try:
SELECT FORMAT(
TRY_PARSE('15-May-2019' AS DATE USING 'en-US')
, 'yyyy/MM/dd')
TRY_PARSE parses the string to DATE using English culture.
FORMAT is to precisely control the output