SQL Server, Incorrect syntax, Convert to datetime - sql-server

Using ASP classic, I want to convert a date Tue, 21 Apr 2020 12:23:00 GMT to the format AAAA-MM-DD HH:MM:SS to insert it into a SQL Server 2008 database.
My code:
CONVERT('Tue, 21 Apr 2020 12:23:00 GMT', GETDATE(), 20)
I get this error:
Incorrect syntax to 'Tue, 21 Apr 2020 12:23:00 GMT'.

Note that dates in SQL Server don't actually have any internal string format. Rather, they are stored as binary. One option here would be to take the following substring of the your input timestamp, and then use TRY_CONVERT to marshall it over to a bona fide datetime inside SQL Server:
Tue, 21 Apr 2020 12:23:00 GMT <-- start with this input
21 Apr 2020 12:23:00 <-- convert this string to datetime
Sample code:
WITH yourTable AS (
SELECT 'Tue, 21 Apr 2020 12:23:00 GMT' AS dt
)
SELECT
dt,
TRY_CONVERT(datetime, SUBSTRING(dt, 6, LEN(dt) - 9)) AS dt_out
FROM yourTable;
Demo
Edit:
If you are using an earlier version of SQL Server, then you can use CONVERT with format mask 106, using the same substring as above:
SELECT
dt,
CONVERT(datetime, SUBSTRING(dt, 6, LEN(dt) - 9), 106) AS dt_out
FROM yourTable;

Related

Convert date format in SQL SERVER - TSQL

I am stuck in a weird requirement. I am just hoping that I might get some help.
My requirement is that the incoming data in table is like
'Thu Feb 18 11:03:18 GMT 2016' and it's output must be
2016-02-18 i.e. in YYYY-MM-DD format.
Any ideas!!
try this with some other sample data,
DECLARE #input varchar(50)='Thu Feb 18 11:03:18 GMT 2016'
select cast(replace(substring(#input,4,len(#input)),'GMT','') as datetime)
select cast(cast(replace(substring(#input,4,len(#input)),'GMT','') as datetime) as date)
Alongside with #KumarHarsh's answer, you can use this too:
DECLARE #input VARCHAR(MAX)= 'Thu Feb 18 11:03:18 GMT 2016';
SELECT FORMAT(CONVERT(DATETIME, REPLACE(SUBSTRING(#input, 4, LEN(#input)), 'GMT', ''), 20),'yyyy-MM-dd')

Convert "12:37:37.641 UTC Tue Apr 5 2016" date format into datatime in sql server 2008

I want to convert "12:37:37.641 UTC Tue Apr 5 2016" this string into DateTime in sql server 2008. Can anyone help me to convert this.
You might try it like this:
DECLARE #d VARCHAR(100)='12:37:37.641 UTC Tue Apr 5 2016';
SELECT CONVERT(DATETIME, SUBSTRING(#d,22,1000) + ' ' + SUBSTRING(#d,1,12),109);
The result
2016-04-05 12:37:37.640

yyyy/mm/dd format in SQL Server

I have a date column in SQL Server and I want to get date in yyyy/mm/dd format.
My code is:
select
convert(date, Consumption_FDate, 111)
from
dbo.TblStuff_Consumption
Consumption_FDate is my date column and dbo.TblStuff_Consumption is my table.
My problem is this code is not working; the output is same every time, though I change '111' into other numbers.
SELECT CONVERT(VARCHAR(10), Consumption_FDate, 111)
FROM dbo.TblStuff_Consumption
In SQL serve 8 working fine
select convert(varchar,getDate(),112)
CONVERT(VARCHAR(19),GETDATE()) output Nov 04 2014 11:45 PM
CONVERT(VARCHAR(10),GETDATE(),10) output 11-04-14
CONVERT(VARCHAR(10),GETDATE(),110) output 11-04-2014
CONVERT(VARCHAR(11),GETDATE(),6) output 04 Nov 14
CONVERT(VARCHAR(11),GETDATE(),106) output04 Nov 2014
CONVERT(VARCHAR(24),GETDATE(),113) output04 Nov 2014 11:45:34:24

Convert Date: Day Mon YYYY hh:mm AM CET

I have these dates, that I need converted to date:
Sat Nov 22 2014 01:01 AM CET
Mon Aug 18 2014 06:32 PM CEST
All the convert or cast functions I tried didn't work, maybe someone has an idea what to do?
In the end, I would need something like
YYYY-MM-DD HH:MM:SS or DD.MM.YYYY HH:MM:SS that doesn't really matter, but I would need them in the same timezone if at all possible...
Thank You for any ideas
SQL Server can convert that if you get rid of the day of the week at the beginning and the time zone at the end:
SELECT CONVERT(DATETIME, SUBSTRING('Sat Nov 22 2014 01:01 AM CET',4,LEN('Sat Nov 22 2014 01:01 AM CET')-7))
SELECT CONVERT(DATETIME, SUBSTRING('Mon Aug 18 2014 06:32 PM CEST',4,LEN('Mon Aug 18 2014 06:32 PM CEST')-7))
I'm not sure what you mean that you need them in the same time zone.

Convert datetime to yyyy-mm-dd:hh in SQL Server

How can I convert yyyy-mm-dd:hh:mm:ss to yyyy-mm-dd:hh in SQL Server?
I want to convert 2013-04-30:10:34:23 to 2013-04-30:10:00:00?
Please help me to resolve this issue.
can you tried this
SELECT REPLACE(CONVERT(VARCHAR(13), GETDATE(), 120), ' ', ':')
for more help please refer this link
If both input and result are in datetime format, you could just use substring to get the desired output.
Read this, this will help you how to convert or short any date format in SQL sever
Example
The following script uses the CONVERT() function to display different formats. We will use the GETDATE() function to get the current date/time:
CONVERT(VARCHAR(19),GETDATE())
CONVERT(VARCHAR(10),GETDATE(),10)
CONVERT(VARCHAR(10),GETDATE(),110)
CONVERT(VARCHAR(11),GETDATE(),6)
CONVERT(VARCHAR(11),GETDATE(),106)
CONVERT(VARCHAR(24),GETDATE(),113)
The result would look something like this:
Nov 04 2011 11:45 PM
11-04-11
11-04-2011
04 Nov 11
04 Nov 2011
04 Nov 2011 11:45:34:243

Resources