String date format convert to date - sql

I need to convert this string to yyyy-MM-dd date format:
December 31, 2014 to 2014-12-31
May 31 , 2018 to 2018-05-31
Any suggestion?
Regards!

You can just use convert():
select convert(date, 'December 31, 2014')
SQL Server is pretty good about doing such conversions.
So this works in both cases:
select convert(date, datestr)
from (values ('December 31, 2014'), ('May 31 , 2018')) v(datestr);

You can also use as shown Here
Select cast('December 31, 2014' as date) as [Date]
Select cast('December 31, 2014' as date) as [Date]
or in higher version of SQL Server provide format string value for different date format as you want.
SELECT FORMAT(cast('December 31, 2014' as Date),'yyyy-MM-dd','en-US') AS[DATE IN US FORMAT]

Related

convert oracle date format based on user input

I have a oracle form that the user loads data. The date format the user uses is different than what I have in my list of values. Need help in adding this format
SELECT
TO_CHAR(TO_DATE( 'Sunday, November 4, 2018', 'DD MON YYYY' ))
FROM
DUAL;
Convert to '11/4/2018'.
You can convert a string like 'Sunday, November 4, 2018' to a DATE datatype with this expression:
TO_DATE( 'Sunday, November 4, 2018', 'Day, Month DD, YYYY' )
Then it is possible to convert the date to a string in another format with TO_CHAR().
You seem to be looking for:
SELECT
TO_CHAR(TO_DATE( 'Sunday, November 4, 2018', 'Day, Month DD, YYYY' ), 'MM/DD/YYYY')
FROM DUAL;
This yields:
11/04/2018
Demo on DB Fiddle

More date format conversion SQL

Hi I am working with SQL 2014 and I need to convert the following 2 date formats:
12/31/18 - Varchar
12/31/2018 - Varchar
to this format final format
December 31,2018 Varchar
I know that use varchar is not the correct.
any suggestion?
Try to use:
DECLARE #f varchar(50) = '12/31/18'
SELECT FORMAT(CAST(#f AS DATETIME), N'MMMM dd, yyyy')
OUTPUT:
December 31, 2018
And your second variant:
DECLARE #f varchar(50) = '12/31/2018'
SELECT FORMAT(CAST(#f AS DATETIME), N'MMMM dd, yyyy')
OUTPUT:
December 31, 2018
You can try the following query also using
create table DateValue (DateVal varchar(10))
insert into DateValue values
('12/31/18'),
('12/31/2018'),
('01/31/18'),
('01/31/2018')
Select
DateName( month , DateAdd( month , DATEPART(MONTH, DateVal) , 0 ) - 1 ) + ' ' +
Cast(DATEPART(dd, DateVal) as Varchar) + ', ' +
+ Cast (DATEPART(YYYY, DateVal) as Varchar) as VarcharDate
from DateValue
The output will be as shown below.
VarcharDate
----------------
December 31, 2018
December 31, 2018
January 31, 2018
January 31, 2018
This query will also run in lower version of SQL Server where format() is not available.
Live Demo

sorting date in sql

I have dates stored in my column as
Wednesday, November 21, 2018
Wednesday, August 22, 2018
Wednesday, August 22, 2018
Wednesday, August 22, 2018
Wednesday, August 15, 2018
Tuesday, November 27, 2018
Tuesday, November 06, 2018
Monday, November 19, 2018
I am using
ORDER BY CONVERT(varchar(100), submissionDate, 101) DESC
but its not giving me column in sorted way. My column is NVARCHAR(MAX)
If you are stuck with the column type nvarchar, you can convert the value to a date with something like this:
SELECT *, submissionDate
, convert(date
, right(submissionDate, len(submissionDate)
- charindex(',', submissionDate))
, 107) as my_date
FROM your_table
ORDER BY my_date
Or just
SELECT submissionDate
FROM your_table
ORDER BY convert(date
, right(submissionDate, len(submissionDate)
- charindex(',', submissionDate))
, 107)
The conversion is a bit cumbersome because of your date format. Sql Server does not seem to have a matching built-in format. So we get rid of the day of the week (Monday,..) by removing the part before the first comma and then the result complies with format 107 (Mon dd, yyyy).
SQL Server is pretty good about recognizing arcane formats as dates, but not good enough for this situation. I think that stuff() makes this simpler:
select convert(date,
stuff(datestr, 1, charindex(',', datestr) + 1, '')
)
from (values ('Wednesday, November 21, 2018')) v(datestr)

Date format change to dd/mm/yyyy to dd mon yy in sql server

I tried below query to get date format as 25 Sep 2018 but it is not converting the date type and actual date format is in dd/mm/yyyy format.
Please help.
select convert(varchar, FOUNDEDYEAR, 108)FROM EDELGIVE_KYC_DETAILSS
try like below
SELECT (convert(NVARCHAR, getdate(), 106))
25 Sep 2018
select (convert(NVARCHAR, FOUNDEDYEAR, 106))FROM EDELGIVE_KYC_DETAILSS
SELECT (convert(NVARCHAR, convert(datetime, '25/09/2018', 103), 106))
output
25 Sep 2018

Converting varchar to date in SQL Server

I have a varchar column which has values like "Aug 07 2017, 04:14 AM,EDT".
I need to convert this to a date column so that its possible to take the maximum value of the date.
I tried this:
select CONVERT(datetime, #dateField, 108)
from table
But I am getting the following error:
Conversion failed when converting date and/or time from character string.
You can just use left() and convert():
select convert(date, left('Aug 07 2017, 04:14 AM,EDT', 11))
If you want a datetime then convert the date and time separately, then:
select ( convert(datetime,
left('Aug 07 2017, 04:14 AM,EDT', 11)
) +
convert(datetime,
convert(time,
substring('Aug 07 2017, 04:14 AM,EDT', 14, 8)
)
)
)
Note: This is not taking the time zone into account.
Better Datetime solution
DECLARE #dateField AS NVARCHAR(50) = 'Aug 07 2017, 04:14,EDT'
-- Get index of last comma
DECLARE #validDateField AS NVARCHAR(20) = REPLACE(LEFT(#dateField, LEN(#dateField)- CHARINDEX(',', reverse(#dateField))), ',','')
SELECT CONVERT(DATETIME, #validDateField, 108)
Here is a solution:
select CONVERT(datetime, REPLACE(LEFT(#dateField, LEN(#dateField) - 3),',','') ,108 )