How to read Date in 'Aug 02, 2021' format in SQL - sql

I have Date stored in DB in format: '02/08/2021' (Date selected from mobile app). I want it to be stored/returned in format: Aug 02, 2021.
This is what I have tried:
declare #value Date = '02/08/2021'
select Convert(varchar(30), #value, 107)
But, I get error saying:
Conversion failed when converting date and/or time from character
string.
How can I fix this using SQL Query?

you may convert string first
DECLARE #s nvarchar(50) = '02/08/2021'
declare #value Date = SUBSTRING(#s,4,2) + '/' + SUBSTRING(#s,1,2) + '/' + SUBSTRING(#s,7,4)
select Convert(varchar(30), #value, 107)

Related

How convert SQL Server DATE columns to DD:MM:YY and HH:MM:SS

I have a SQL Server column which is called DATE with this sample data 19452801102747.
I have this code
SELECT REPLACE(CONVERT(CHAR(10), [DATE], 103), '/', '')
I can get the date okay, is the final part of converting HH:MM:SS.
I am using SSMS 2018 and I would like to have two column separate; columns as shown on below image (DATE(DD:MM:YYYY))(TIME(HH:MM:SS))
Many thanks for your help.
We can use a simple substring to add date time separator in your varchar string,
because we cannot directly convert varchar to datetime..
DECLARE #date varchar(50) = '19452801102747' declare #date1 varchar(50)
SET
#date1 = SUBSTRING(#date, 1, 4) + '-' + substring(#date, 7, 2) + '-' + substring(#date, 5, 2) + ' ' + substring(#date, 9, 2) + ':' + substring(#date, 11, 2) + ':' + substring(#date, 13, 2)
SELECT convert(datetime, #date1)
First of all: You should not store datetime values in a non-appropriate datatype.
Your example looks like YYYYddMMHHmmss. There is no out of the box conversion for this...
The following will perform a number of string methods in order to transform your own format to a standard format. In this case I chose ISO8601, which is YYYY-MM-ddTHH:mm:ss:
DECLARE #YourDate VARCHAR(100)='19452801102747';
SELECT CONVERT(DATETIME,STUFF(LEFT(#YourDate,4) + STUFF(STUFF(STUFF(STUFF(SUBSTRING(#YourDate,7,1000),7,0,':'),5,0,':'),3,0,'T'),1,0,'-'),8,0,'-' + SUBSTRING(#YourDate,5,2)),126);
The STUFF() calls will insert some characters in the right position. Furthermore some string cutting will swap your ddMM to MMdd.

How to convert '2017-11' to 'November 2017' in SQL

I want to convert or turn a string to date, but display like 'November 2017'
My field is: [Fiscal_post_year_month] nvarchar(max)
I'm just going to put a guess that you are using MSSQL, this should do what you want.
DECLARE #THEDATE NVARCHAR(MAX)
--need to have some kind of 'DAY' but we won't use it
SET #THEDATE = '2017-11' + '-01'
--we turn the number back into a VARCHAR and then concatenate them together as per your request
SELECT DATENAME (MONTH, #THEDATE) + ' ' + CAST(DATEPART (YEAR ,#THEDATE) AS VARCHAR(MAX))
results:

How to convert varchar date into datetime in sql

I have varchar date in this format:
03/13/2015 : 2130
and i would like to convert it into datetime something like this:
2015-03-13 21:30:00.000
i have seen example like this but did not work for what i am looking for
DECLARE #Date char(8)
set #Date='12312009'
SELECT CONVERT(datetime,RIGHT(#Date,4)+LEFT(#Date,2)+SUBSTRING(#Date,3,2))
This will work assuming all date times parts are padded with 0's consistently.
DECLARE #Input VARCHAR(50);
SET #Input = '03/13/2015 : 2130';
SET #Input = LEFT(#Input, 10) + ' ' + LEFT(RIGHT(#Input, 4), 2) + ':' + RIGHT(RIGHT(#Input, 4), 2);
PRINT #Input;
PRINT CONVERT(DATETIME, #Input);
PRINT CONVERT(VARCHAR(50), CONVERT(DATETIME, #Input), 121);
Output:
03/13/2015 21:30
Mar 13 2015 9:30PM
2015-03-13 21:30:00.000
OP wants mmddyy and a plain convert will not work for that:
select convert(datetime,'12312009')
Msg 242, Level 16, State 3, Line 1
The conversion of a char data type to a datetime data type resulted in
an out-of-range datetime value
so try this:
DECLARE #Date char(8)
set #Date='12312009'
SELECT CONVERT(datetime,RIGHT(#Date,4)+LEFT(#Date,2)+SUBSTRING(#Date,3,2))
OUTPUT:
2009-12-31 00:00:00.000
(1 row(s) affected)
I think this is what you're looking for:
DECLARE #Date VARCHAR(20)
SET #Date = '03/13/2015 : 2130'
-- Format the date string
SET #Date = LEFT(#Date, 10) + ' ' + SUBSTRING(#Date, 14, 2) + ':' + SUBSTRING(#Date, 16, 2)
-- convert to date
Select CONVERT(varchar, CONVERT(DATETIME, #Date), 121)
SQL Fiddle
More Info
Sql Function:
CONVERT(data_type(length),expression,style)
you can try this:
CONVERT(datetime,#varCharDate,121)
For more see this link
If convert is not working for you then you can use mid to take date, month, year etc. And then use str_to_date to construct datetime in desired format.
In oracle use to_date and this stackoverflow link for taking substring

How to format datetime as M/D/YYYY in SQL Server?

To convert a datetime to MM/DD/YYYY, this works:
declare #datetime datetime = '2015-01-01'
select convert(varchar(10),convert(date,#datetime),101)
This evaluates to 01/01/2015. How can I have the date convert to 1/1/2015 instead?
Nothing on http://www.sql-server-helper.com/tips/date-formats.aspx matches the M/D/YYYY format.
I think the only possibility you have is to do something like this:
DECLARE #datetime DATETIME = '2015-01-01'
SELECT LTRIM(STR(MONTH(#datetime))) + '/' +
LTRIM(STR(DAY(#datetime))) + '/' +
STR(YEAR(#datetime), 4)
With SQL Server 2012 and above, you can do this:
SELECT FORMAT(#datetime, 'M/d/yyyy')
DECLARE #datetime DATETIME = '2015-01-01';
SELECT STUFF(REPLACE('/' + CONVERT(CHAR(10), #datetime, 101),'/0','/'),1,1,'')
This is how it works:
First CONVERT the DATETIME to CHAR
Then Add a '/' character at the begining
REPLACE all '/0' with '/'
With STUFF, get rid of the first '/'
There's no convert style that uses single digit day or month. This could work, though.
declare #datetime datetime = '2015-01-01'
select
cast(month(#datetime) as varchar(2)) + '/' +
cast(day(#datetime) as varchar(2)) + '/' +
cast(year(#datetime) as varchar(4))

Convert 24-Hour format datetime to varchar Sql Server 2008

I have a problem in converting sql server 2008 datetime to varchar,
Select convert(varchar(20),convert(datetime, '2013-12-11 00:59:00.000'))
the result is Dec 11 2013 12:59AM but I need it to be actually Dec 11 2013 00:59AM as in the database dates are of 24-Hour format.
How can I correct the query?
There are two functions, cast and convert that you can use.
http://msdn.microsoft.com/en-us/library/ms187928.aspx
You forgot to put in the format style.
-- Using cast
Select cast('2013-12-11 00:59:00.000' as varchar(20)) as my_casted_date
-- Using convert
Select convert(varchar(24), '2013-12-11 00:59:00.000', 113) as my_converted_date
Use format with custom date time strings for utlitmate flexibility.
http://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx
-- Using format
DECLARE #my_date datetime2 = '2013-12-11 00:59:00.000';
SELECT format(#my_date, 'MMM dd yyyy HH:MM tt', 'en-us') as str_english_date
Solution that will work with SQL Server 2008.
-- Create a date time variable
DECLARE #my_date DATETIME2 = '2013-12-11 00:59:00.000';
-- Using convert
SELECT
CONVERT(varchar(24), #my_date, 113) +
CASE
WHEN DATEPART(HH, #my_date) < 12 THEN ' AM'
ELSE ' PM'
END
AS my_converted_date;
This will be perfect for you..
declare #dt datetime
set #dt='12-Jan-2014 23:59'
SELECT Right(CONVERT(VARCHAR, #dt, 100),7) AS DateTime_In_12h_Format
It's not nice but it does its job:
DECLARE #Var DATETIME;
SET #Var = '2013-12-11T00:59:00.000';
SELECT
#Var AS SourceValue,
CONVERT(VARCHAR(50), #Var, 100) AS FormatedValue1,
CASE
WHEN CONVERT(VARCHAR(50), #Var, 100) LIKE '%[ ]12:__AM' THEN REPLACE(CONVERT(VARCHAR(50), #Var, 100), ' 12:', ' 00:')
ELSE CONVERT(VARCHAR(50), #Var, 100)
END AS FormatedValue2
SourceValue FormatedValue1 FormatedValue2
----------------------- ------------------- -------------------
2013-12-11 00:59:00.000 Dec 11 2013 12:59AM Dec 11 2013 00:59AM
Please try this
Select convert(varchar(20),convert(datetime, '2013-12-11 00:59:00.000'),113) + ' ' +RIGHT(CONVERT(VARCHAR(26), Convert(datetime,'2013-12-11 00:59:00.000'), 109), 2)
Output
11 Dec 2013 00:59:00 AM
Fiddle Demo