Convert "datetime" to "MonthName DAY,YEAR" - tsql

I have a datetime field and what to covert it to "MonthName DAY, YEAR" format.
For example, the following date:
2013-01-16 00:00:00.000
will be convert to:
January 16,2013
I am doing this with the following statement:
CAST(DATENAME(MONTH,DateTime) AS VARCHAR(12)) + ' ' + CAST(DATEPART(DAY,DateTime) AS VARCHAR(2)) + ','+ CAST(YEAR(DateTime) AS VARCHAR(4))
As you can see I am using separate date function for each part of the datetime value and converting it to string in order to concatenate it with the other parts.
Is there a more clear and faster way, using only covert/cast function?

How about this:
select
CONVERT(VARCHAR(12), dt, 107) AS [Mon DD, YYYY]
from temp
Output:
Jan 16, 2013
Live Demo

Related

SQL date conversion to mm/dd/yyyy hh:mm

Can't seem to find answer to what seems like a simple question. I have a date that is returned
2022-02-07 18:53:36.000 I need to convert that date to mm/dd/yyyy hh:mm.
Is that possible?
I got the answer from another site. I've tested it and it works.
(CONVERT(VARCHAR(20), ea.event_timestamp, 101) + ' ' + LEFT(CONVERT(VARCHAR(20),ea.event_timestamp, 108) ,5)) AS EventDate
select convert(varchar, getdate(), 13);

How to merge individual date fields like order day, order month and order year to create a order date?

Ex. Order Day "1"
Order Month "Jan"
Order Year "2020"
Required Date: 1 JAN 2020
If each of these are separate fields, I would build a text version of the date and wrap it in the DATE() function, but there are probably a dozen ways to do it differently.
DATE( [OrderYear] + "-" + [OrderMonth] + "-" + [OrderDay] )
Let's assume your data is
Step-1: Convert all three fields to string type, if not so.
Step-2: create a calculated field say order date full with the following calculation
DATE(DATEPARSE("dd:MMM:yyyy", [order date] + ':' + [order month] + ':' + [order year]))
get results like this

Convert String to Date in Big Query error

I have a string field that is being pulled from a table and I'm trying to cast that as a date in a view I created. I keep getting an error when trying to Cast as a date though. The format of the field looks like this:
July 19, 2020 or
August 8, 2020 etc..
I get an error that states
"Failed to parse input string "July 19, 2020"
or one of the other dates in the data when trying to use DATE_Parse. Or I get
"Invalid Date:August 8, 2020"
if I try to use the CAST function.
Below is my query when trying to CAST the date:
select
noteattributes.value.name as name_type, noteattributes.value.value as name_value, CAST(noteattributes.value.value as DATE) as DATE_TEST, order_number
from test.orders,
unnest(note_attributes) as noteattributes
where noteattributes.value.name = 'Pickup-Date'
Convert String to Date
Below is for BigQuery Standard SQL
You should use PARSE_DATE instead of CAST as in below example
PARSE_DATE('%B %d, %Y', date_as_string)
You can test, play with this using example below
#standardSQL
WITH `project.dataset.table` AS (
SELECT 'July 19, 2020' date_as_string UNION ALL
SELECT 'August 8, 2020'
)
SELECT PARSE_DATE('%B %d, %Y', date_as_string) AS date_as_date
FROM `project.dataset.table`
with output
Row date_as_date
1 2020-07-19
2 2020-08-08

Casting DATETIME on concatenated date and time

I'm trying to concatenate a column's date to a fixed time of the day and then CAST the whole thing as DATETIME.
The fixed time is 5:30am.
The date column I'm using needs to be adjusted as it shows the end date/time of when something ran; I want to use the start date/time.
The start date/time time is not available as its own column, but I have another column that has the duration the process took in seconds, so I can use DATEADD to roll the end date/time back to the start date/time.
Here's the full statement:
CAST(CONVERT(VARCHAR(10), DATEADD(ss,-ConfTask.[LastExecutedDuration], ConfTask.[LastExecutedDate]), 103) + ' ' + '05:30' as DATETIME)
Here's the error message I'm receiving:
The conversion of a varchar data type to a datetime data type resulted
in an out-of-range value.
I've tried testing these statements to investigate the issue, but they all run OK on their own:
CAST(CONVERT(VARCHAR(10), GETDATE(), 103) + ' ' + '05:30' as DATETIME)
CAST('2017-03-02' + ' ' + '05:30' as DATETIME)
DATEADD(ss,-ConfTask.[LastExecutedDuration], ConfTask.[LastExecutedDate])
I'm a bit stuck on how to get round this issue. Any help would be much appreciated.
Clearly, you must have some unexpected values in the column.
I would suggest finding them using a query such as this:
SELECT LastExecutedDuration, LastExecutedDate
FROM ConfTask
WHERE TRY_CONVERT(datetime,
CONVERT(VARCHAR(10),
DATEADD(second,
-ConfTask.[LastExecutedDuration],
ConfTask.[LastExecutedDate]
)
103
) + ' ' + '05:30')
)
You can also simplify the logic, by just using date functions:
select dateadd(minute,
5 * 60 + 30,
convert(datetime,
convert(date,
dateadd(second,
- ConfTask.LastExecutedDuration
ConfTask.LastExecutedDate
)
)
)
)
This worked:
CONVERT(DATETIME, CONVERT(CHAR(8), DATEADD(ss,-ConfTask.[LastExecutedDuration], ConfTask.[LastExecutedDate]), 112)) + ' ' + CONVERT(CHAR(8), '05:30:00', 108)

Converting date/time in sql to string

Is there any way that I can convert the DateTime SQL value of this:
2011-10-11 00:00:00.000
to a string formatted exactly the same?
'2011-10-11 00:00:00.000'
I tried doing cast(fl_from_dt as varchar(50) but it converts it to something readable - ex. 'Oct 11 2011 12:00AM'
Are you using SqlServer?
Take a look at the style parameter of CONVERT() here: http://msdn.microsoft.com/en-us/library/ms187928.aspx - in particular have a look at the ODBC canonical (with milliseconds) format.
For short you should use style 121 in the CONVERT() command.
Style: 121
ODBC canonical (with milliseconds) default for time, date, datetime2, and datetimeoffset
yyyy-mm-dd hh:mi:ss.mmm(24h)
Try this one !
select CONVERT(VARCHAR(10), GETDATE(), 112)
+ right('0'+cast(datepart(hh, GETDATE()) as varchar(2)),2)
+ right('0'+cast(datepart(mi, GETDATE()) as varchar(2)),2)
+ right('0'+cast(datepart(ss, GETDATE()) as varchar(2)),2) as 'DateTime_STR2'