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
Related
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.
I want to create a function to find a DateTime format of string type.
For Example: -
I have a string as '07/09/2016 12:00 AM' and want it in DateTime format of this string and it should be like : - MM/dd/yyyy hh:mm tt format.
So, when I pass the DateTime as string to this function then function returns me a DateTime format using SQL function.
We can try using TRY_CONVERT to convert your date string into a bona fide datetime. Then, we can use FORMAT to display the datetime in the format you want to see.
SELECT FORMAT(TRY_CONVERT(datetime, '07/09/2016 12:00 AM'),
'MM/dd/yyyy hh:mm tt', 'en-US')
07/09/2016 12:00 AM
Demo
Try this i hope this will work for you
SELECT CAST(GETDATE() AS VARCHAR(19))
Also try this
SELECT CONVERT(VARCHAR(10),GETDATE(), 101) + right(CONVERT(VARCHAR(32),GETDATE(),100),8)
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
I have a column which is of datetime type.
I want to separate date and time for which i used left() & right() enclosed by cast() or convert() like below :
Select BATCH,
LEFT( CAST(S_DATE AS VARCHAR(20)),12) AS ST_DATE,
RIGHT( CAST(S_DATE AS VARCHAR(20)),8) AS ST_TIME,
LEFT( CAST(E_DATE AS VARCHAR(20)),12) AS E_DATE,
RIGHT( CAST(E_DATE AS VARCHAR(20)),8) AS E_TIME
INTO CRYST2
From Z_BATCH;
this is my actual format for datetime column :-
2015-10-01 14:00:00.000
But the problem is that it is separating the date and time accurately but it isn't returning the output in string form, as the output itself turns to date & time format respectively in their respective columns.
Following is the output I am getting:-
1 Oct 1 2015 2:00PM Oct 1 2015 2:30PM
As you can clearly see the columns I separated after date-time to string conversion is still giving me output in some date-time format only.
please help regarding it.
this is my actual format for datetime column :
2015-10-01 14:00:00.000
No, it's not. A datetime value doesn't have a format at all, it only represents a point in time. It only gets a format when you convert it to a string.
You are not specifying any format at all when you convert the datetime values, so it will be using the default format (0). You can use the format that you saw the datetime values displayed as (121) to get the desired result:
Select BATCH,
LEFT( CAST(S_DATE AS VARCHAR(19),121),10) AS ST_DATE,
RIGHT( CAST(S_DATE AS VARCHAR(19),121),8) AS ST_TIME,
LEFT( CAST(E_DATE AS VARCHAR(19),121),10) AS E_DATE,
RIGHT( CAST(E_DATE AS VARCHAR(19),121),8) AS E_TIME
INTO CRYST2
From Z_BATCH;
Cast The date and the Time in the exact format like:
CAST(S_DATE AS Time) AS ST_TIME
CAST(S_DATE AS Date) AS ST_Date
will return:
14:00:00
2015-10-01
Use CONVERT with format 120 to get the desired result, it always return the format yyyy-MM-dd hh:mm:ss
CONVERT(varchar(20), S_DATE, 120)
You can try this, if you want to get results in string format:
select convert(varchar(11),cast(S_DATE as date),106)
select convert(varchar(11),cast(S_DATE as time),100)
If you try to get answers in date and time format, try this:
CAST(S_DATE AS Time) AS ST_TIME
CAST(S_DATE AS Date) AS ST_Date
I'd like to take the current date in Sybase and set the time to 13:30:00.
Let say I do
select getdate()
12/5/2014 4:06:24.670 PM
I want to return
12/5/2014 13:30:00 PM
How do I transform that?
I did not have time to test this but i would suggest you convert the date part of getdate() into mm/dd/YYYY and then add "13:30:00 PM" before you convert it back from a string into a datetime object.
select convert(datetime,convert(varchar, getdate(), 101) + "13:30:00 PM")