Convert String "Friday June 1, 2018" to Date - tsql

I have a column in database from imported file in the format Friday June 1, 2018 and trying to format to a valid date conversion from varchar to date.
I have tried cast and convert with no success
SELECT CAST([Course date] as DATETIME)
FROM [dbo].[Test]
SELECT CONVERT(datetime, [Course date], 103)
FROM [dbo].[Test]
I expected conversion to type 103 UK dd/mm/yyyy

TRY_CONVERT seems to be able to handle your date string. Note that the name of the day is superfluous, and is not needed to determine exactly what the date is. So, we can remove it using base string functions.
WITH yourTable AS (
SELECT 'Friday June 1, 2018' AS datecol
)
SELECT
datecol AS input,
TRY_CONVERT(datetime, STUFF(datecol, 1, CHARINDEX(' ', datecol), '')) AS output
FROM yourTable;
Demo

Related

How to fetch only year from date in postgresql table

I have a table like this in postgresql:
Name
DOB
ABC
'2011-03-03'
XYZ
'2009-01-01'
What is the query that I should use to get the output data in the below format(only year instead of date) also I want to retrieve data that is greater than 2010 only:
Name
DOB
ABC
'2011'
Format DOB using to_char.
select "Name", to_char(DOB, 'yyyy') DOB
from the_table
where extract('year' from DOB) > 2010;
If DOB is character rather than date type then it has to be first cast to date:
select "Name", to_char(DOB::date, 'yyyy') DOB
from the_table
where extract('year' from DOB::date) > 2010;
If your date represented as text has "month/day/year" format then use to_date to convert it to date.
select "Name", to_char(to_date(DOB, 'mm/dd/yyyy'), 'yyyy') DOB
from the_table
where extract('year' from to_date(DOB, 'mm/dd/yyyy')) > 2010;
Unrelated but do not store dates as formatted text. You have data type date for this.

Conversion failed when converting date and/or time from character string. T-Sql

I have a varchar column in my database called ref003 which stores datetime like the below
2021-04-04 20:01:03
Here, date format is like yyyy-MM-DD
When I execute the below select query I am getting error
SELECT *
FROM FileIndex
WHERE CAST(CONVERT(CHAR(30), CONVERT(DATETIME, Ref003, 105), 101) AS DATE)
BETWEEN CAST(CONVERT(CHAR(10), CONVERT(DATETIME, '01-01-2021', 105), 101) AS DATE)
AND CAST(CONVERT(CHAR(10), CONVERT(DATETIME, '31-12-2021', 105), 101) AS DATE)
And the error is
Msg 241, Level 16, State 1, Line 5 Conversion failed when converting
date and/or time from character string.
What is the problem here and how can I solve this problem?
First, for the conversion you need to convert to VARCHAR, not DATE. Note this expression:
CONVERT(VARCHAR(10), CAST(<your date value> AS DATE), 20)
With that in mind, you can clean your query up like so:
--==== Easily consumable sample data
DECLARE #thetable TABLE (someid INT, thedate DATETIME);
INSERT #thetable
VALUES(1,'2021-04-04 20:01:03'),(2,'2021-06-04 22:01:05'),(1,'2021-04-29 10:31:11');
--==== Solution
SELECT t.*, FormattedDate = fmt.Dt
FROM #thetable AS t
CROSS APPLY (VALUES(CONVERT(VARCHAR(10), CAST(t.thedate AS DATE), 20))) AS fmt(Dt)
WHERE t.thedate BETWEEN '20210401' AND '20210501';
Returns:
someid thedate FormattedDate
----------- ----------------------- -------------
1 2021-04-04 20:01:03.000 2021-04-04
1 2021-04-29 10:31:11.000 2021-04-29

how to get hour, month from timestamp in postgresql

timestamp with timezone is this - 2020-05-31T10:05:07Z
this is not working, despite referencing official documentation. I need to extract may 2020 or separate month and year to compare against May 2020
SELECT date_trunc('hour', TIMESTAMP '2020-05-31T10:05:07Z')
SELECT date_part('day', TIMESTAMP '2020-05-31T10:05:07Z');
If you want to check if a timestamp value is "may 2020", you have different options.
to_char(the_value, 'yyyy-mm') = '2020-05'
or
extract(month from the_value) = 5
and extract(year from the_value) = 2020
or
(extract(month from the_value), extract(year from the_value)) = (5, 2020)
extract() and date_part() are the same thing - but I prefer the standard compliant extract() version.
demo:db<>fiddle
You need to_char() to format a date or timestamp. Mon gives you the first three letters of a month name:
SELECT
to_char(
TIMESTAMP '2020-05-31T10:05:07Z',
'Mon YYYY'
)
Returning the entire month name you can use Month instead of Mon. But, for some reasons, the length of the Month value is fixed to the longest month name available. That means May is returned with right padded spaces. To avoid this, you need to add the modifier FM:
SELECT
to_char(
TIMESTAMP '2020-05-31T10:05:07Z',
'FMMonth YYYY'
)

PostgreSQL convert month name to number

I have a query that returns a field as month and year for example "Nov 2015" I want to get the number for the month, here I have to get number as "11".
You can use the to_date function that can parse input in a certain format into a 'real' date. Then with this date you can extract the month from it.
select EXTRACT(MONTH FROM to_date('Nov 2015', 'Mon YYYY'))
See http://www.postgresql.org/docs/current/static/functions-formatting.html for the formatting syntax and http://www.postgresql.org/docs/current/static/functions-datetime.html for the other datetime functions etc..
to_char()
select to_char(to_date('Nov 2015', 'Mon YYYY'), 'mm') month_no
and the query can be
select to_char(to_date(your_date_col, 'Mon YYYY'), 'mm') month_no
from table_name
If your date is timestamp , for example:"2017-01-24 23:00:00-03" (this is timestamp with time zone)
select to_char(your_date, 'MM') from your_table
Then the result would be in this case: "01"
You can use the function to_timestamp(text, text) and extract(field from timestamp):
SELECT EXTRACT(MONTH FROM TO_TIMESTAMP("Nov 2015", "Mon YYYY"));
see the documentation for to_timestamp and extract

Change Month and Year in a datetime

I have a datetime field in my query.
Select Account, Period, JEDate
From table
I would like to replace the year of the "JEDate" datetime field with a Parameter "#Year" (int).
I would also like to replace the month with the "Period" (int) field.
Example:
If the JEDate was 1900-01-01 00:00:00.000 (or any other date), the year was 2013 and the month was 2, I would like the JEDate to be 2013-02-01 00:00:00.000.
How do I do this?
Thank you
Perhaps:
UPDATE dbo.TableName
SET JEDate = CONVERT(DATETIME, CAST(#Year as varchar(4)) + '-' + CAST(Period as VARCHAR(2)) + '-' + '01', 102)
WHERE JEDate IS NOT NULL
Demo
An alternative approach that adds the difference between the original year and month, and the new year and month:
select dateadd(year, #year-Year(JEDATE), dateadd(month, Period-Month(JEDATE), JEDATE))