How to convert string dd-mmm-yyyy to date in sql/snowflake? - snowflake-cloud-data-platform

I am getting date in the format Sat Dec 31 07:56:31 UTC 2020 in snowflake in string format.
Ideally I would like to convert it to Timestamp. But if not, I can work with converting dd-mmm-yyyy to date format in sql.
I tried using substr to extract dd, mmm , yyyy from the string, and want to convert it to timestamp.

SELECT TO_TIMESTAMP(REPLACE('Sat Dec 31 07:56:31 UTC 2022','UTC',''), 'DY MON DD HH24:MI:SS YYYY')
This worked for me.

Related

Parse date to different format in snowflake

In Snowflake I have the date format in following
Thu Nov 12 00:00:00 UTC 2020
If I want to make it as follows:
MM-DD-YYYY HH24:MI:SS
Can this be achieved. I have tried to_date function doesn't seems to work
https://docs.snowflake.com/en/sql-reference/functions/to_date.html
any suggestions
I assume that this data is held in a column with a TIMESTAMP-type datatype? If that is the case then it is stored as a number and how it is displayed is controlled by *_OUTPUT_FORMAT parameters.
If you want to display a date/time in anything other than the default format then just use TO_CHAR with the appropriate format e.g.
select col3
, to_char(col3, 'MM-DD-YYYY HH24:MI:SS')
from casttb;
gives:
COL3 TO_CHAR(COL3, 'MM-DD-YYYY HH24:MI:SS')
2017-01-23 20:34:52.000 01-23-2017 20:34:52
2016-10-05 00:15:53.000 10-05-2016 00:15:53
2017-01-23 08:26:27.000 01-23-2017 08:26:27
2016-10-05 10:16:47.000 10-05-2016 10:16:47
If your date is held as text then first you would need to cast it to a date/time (TO_DATE) and then to text with the required format

Converting a particular date format into string in SQLServer

I am trying to achieve this formt from sql server. I have tried with various things but not able to achieve this
Ultimately this should be a string datatype
'23 Apr, 2018 2:59 PM'
I am trying to achieve this as below:
CONVERT(CHAR(12), dbo.fn_GetDateIntime(cl.createdDate), 107)holla,
convert(char(3), dbo.fn_GetDateIntime(cl.createdDate), 0)MonthPart,
CONVERT(VARCHAR(5), dbo.fn_GetDateIntime(cl.createdDate), 114) TimePart,
CONVERT(VARCHAR(8),GETDATE(),108),
We can use format function -
select format(getdate(), 'dd MMM, yyyy hh:mm tt')
Output -
25 Apr, 2018 01:59 AM
You can use convert() function:
select convert(varchar(20), date, 113) as datestring
You can also use format() function but not best for performance perspective
select format(getdate(), 'dd MMM, yyyy hh:mm tt') as datestring
Please try this
select convert(varchar(20),format(getdate(),'dd MMM yyyy HH:MM tt'))
We can achieve this by two methods CONVERT and FORMAT below is
example of both. I hope this will help you...
1st way
select CONVERT(varchar,getdate(),100) as [Date Time String]
--100 is datetime default sql format 'mon dd yyyy hh:miAM/PM'
Output -
Apr 25 2018 1:23PM
Second way
select FORMAT(getdate(), 'dd MMM, yyyy hh:mm tt') as [Date Time String]
-- In first parameter we have to pass date
-- In second parameter we need to pass date forma in string
Output -
25 Apr, 2018 01:59 AM

SSRS Format string to date time DD MMM YYYY HH MM

Display in reporting services text box - data is grouped.
Best way to format grouped data 20170518110610 to DD MMM YYYY HH MM?
I.e.
18 May 2017 11:06
Tried Text box properties - number - custom format - dd-mmm-yyyy hh:mm the result is the same 20170518110610
You have problems because 20170518110610 is a varchar. You need to convert it to date before format it.
Possible solutions:
1) Return a datetime not varchar from datasource (preferred)
2) Parse value in SSRS then format value:
=Format(DateTime.ParseExact("20170518110610", "yyyyMMddHHmmss", Nothing), "dd-mmm-yyyy hh:mm")

I need a SQL server query to convert date to Mon DD, YYYY HH:MM:SS AM/PM

I need a SQL server query to convert date to Mon DD, YYYY HH:MM:SS AM/PM
for e.g. the date is :-
2016-11-21 16:58:57.797
I need the SQL output as
Nov 21 2016 4:58:57 PM
I tried --> CONVERT(VARCHAR(19),GETDATE(),109)
but it is displaying results like --> Nov 21 2016 4:58:5
Increase the length of VARCHAR in your statement like below
select CONVERT(VARCHAR(30),GETDATE(),109)
This is giving me result as Nov 21 2016 12:55:31:390PM
Hope this should work out for you.
For mysql you can do this:
SELECT DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p');
For ms sql you can do this:
SELECT CONVERT(VARCHAR(19),GETDATE())
Reference:
MySQL DATE_FORMAT() Function
SQL Server CONVERT() Function
Use SUBSTRING and CHARINDEX to get the left part of the string excluding the milliseconds part. Then concatenate the last AM/PM with that.
Query
declare #date as varchar(30) = convert(varchar(30),getdate(),109);
select substring(#date, 1, len(#date) - charindex(':', reverse(#date), 1))
+ ' ' + right(#date, 2) as [datetime];
you can use this
select FORMAT(CAST(GETDATE() AS DATETIME),'MMM dd yyyy hh:mm:ss tt') AS DateTimeAMPM

Converted date does not changes in angularjs

I declare my date element in this format in view.
<li>{{dt.Joindate | date:'dd-MM-yyyy'}}</li> //output: Wed Feb 05 2014 15:39:46 GMT+0530 (India Standard Time) comes like this
My data comes as a string,i converted the string date in javascript but the date field does not shows like MM-dd-yyyy
Javascript code:
angular.forEach(response.Data,function(item){
item.Joindate=Date(item.Joindate);
$scope.retrieveddata.push(item);;
});

Resources