I have values like
'05272021' that I need change to date
I tried to use
to_date(refund_date::varchar,'MM/dd/yyyy') as refund_date
But get:
"Can't parse '05112021' as date with format 'MM/dd/yyyy'"
Provided format should match source data 05272021:
SELECT try_to_date(refund_date::varchar,'MMddyyyy') as refund_date
...
CREATE OR REPLACE TABLE TABLE_TEST( COL1 VARCHAR);
SELECT * FROM TABLE_TEST;
output:
COL1
20210408
We can use below query if we want to convert string to date format.
SELECT to_date(col1::varchar,'YYYYMMDD') AS EXPECTED FROM TABLE_TEST;
Expected output: 2021-04-08
Related
I am trying to convert the date I have got using the GETDATE() function into YY-MON-DD format such as 04 Nov 2022. I have used TO_VARCHAR() to convert into the date returned by the GETDATE() into a string. The output is correct till the TO_VARCHAR() is used i.e. SELECT TO_VARCHAR(GET_DATE, 'DD-MON-YY') returns the desired format.
When I try to wrap it around TO_DATE() function; the date format changes into 2022-11-04.
SELECT TO_DATE(TO_CHAR(GETDATE(), 'DD-MON-YY'), 'DD-MON-YY')
How can I resolve the problem and correct the format!?
A date will display in the default format for your session. If you want to display it in a different format you would need to use the TO_CHAR(date_col, fmt) construct. So your select statement just needs to be:
SELECT TO_CHAR(GETDATE(), 'DD-MON-YY')
wrapping a TO_DATE round it doesn't achieve anything if all you are trying to do is display that column as an output of your SELECT statement
You need set the desired format at session level
alter session
set date_output_format='DD-MON-YYYY';
select getdate()::date --getdate() returns a timestamp not a date
select current_date() --alternatively
As Nick suggested, use the select to format dates instead of tinkering with session parameters
I have a requirement. I have datetime field and I want data in datatype=date
Existing date: 2019-11-13 00: 00: 00: 000 ; datatype=datetime
Expected output require: 11/13/2019 (mm/dd/yyyy) ;
datatype= date
Please help me.
If the core requirement is a right type then:
SET DATEFORMAT MDY;
SELECT CAST(GETDATE() as DATE);
Explicit DATEFORMAT added becaise the output depends on a language settings, so can be yyyy/mm/dd or mm/dd/yyyy, some apps can be sensitive to this, as example SSRS.
However, if there is still a requirement to get value in a precisely right format on a database side, then consider to use a FORMAT statement:
SELECT FORMAT(GETDATE(), 'MM/dd/yyyy')
I have datetime field and I want data in datatype=date
If you want the data type as DATE, in that case you can try like following.
SELECT CAST(YourDateTimeColumn AS DATE) from [YourTable]
Formatting part you should be doing in UI.
You can use CONVERT() :
SELECT t.datecol, CONVERT(VARCHAR(10), t.datecol, 101)
FROM table t;
Your desired date format requires varchar type, if you want date only then you can do instead :
SELECT t.datecol, CONVERT(DATE, t.datecol)
FROM table t;
If you don't want to convert the type, then these type of conversation should do in presentation layer instead.
I have query database like this:
SELECT * FROM TABLE WHERE start_date = '01-10-2016' //DD-MM-YYYY
But above code error and actually in database date format is YYYY-MM-DD.
So how to create query with format date DD-MM-YYYY?
A proper date column (data type date !) has no format. Then it's enough to get your data input for a date column right, use to_date() for non-standard input format like #Shiva posted. Or better yet, always provide date literals in ISO 8601 format 'YYYY-MM-DD' to begin with, which works with any locale setting.
If you are running a broken design with dates stored as text, then combine to_date() and to_char() to transform any valid date format into any other text format:
SELECT * FROM tbl
WHERE start_date = to_char(to_date('01-10-2016', 'DD-MM-YYYY'), 'YYYY-MM-DD');
May be you can use to_date function to convert the above format into the standard format.
For instance,
SELECT to_date('01-10-2016', 'DD-MM-YYYY');
----------
2016-10-01
1 row
https://www.techonthenet.com/postgresql/functions/to_date.php
I have below code which selects a date value from an XML string in SQL Server. The string value returns format in ddmmyy and I need to convert this to T-SQL datetime type before saving it to a table.
SELECT TOP 1
list.n.value('(DOB/#value)[1]', 'datetime') AS 'DOB'
FROM
#ResultXML.nodes('/Variables') AS list(n)
XML file:
<Variables>
<DOB>111290</DOB>
</Variables>
You might try it like this:
DECLARE #XML XML=
'<Variables>
<DOB>111290</DOB>
</Variables>';
SELECT CONVERT(DATETIME,STUFF(STUFF(#XML.value('(/Variables/DOB)[1]','varchar(max)'),3,0,'/'),6,0,'/'),3)
First you use two times STUFF to get 11/12/90 instead of 111290, than you use the 3 to convert this to datetime (or any other fitting format: use . for german, - for british...) More details on CAST and CONVERT
Best was, to store date and time values properly. Within XML this should be ISO8601, which means yyyy-MM-dd or yyyy-MM-ddThh:mm:ss More details on ISO8601
If SQL 2012+
The example
Declare #String varchar(25)= '111290'
-- If MMDDYY
Select DATEFROMPARTS(iif(Right(90,2)>25,'19'+Right(90,2),'20'+Right(90,2)),Left(#String,2),Substring(#String,3,2))
-- If DDMMYY
Select DATEFROMPARTS(iif(Right(90,2)>25,'19'+Right(90,2),'20'+Right(90,2)),Substring(#String,3,2),Left(#String,2))
Returns: 1990-11-12
Returns: 1990-12-11
I can not convert field run_date on sysjobhistory table from yyyymmdd to mmddyyyy.
select CONVERT(varchar(10),run_date),101) as Date from sysjobhistory
Please correct me. Thanks.
declare #dt date ='20161025'
select #dt,CONVERT(varchar(15),#dt,101)
See here
Date type does not have yyyymmdd nor mmddyyyy. This type only valid for string (as varchar, nvarchar). Plus, you have extra ) after rundate.
The correct one should be:
select CONVERT(varchar(10),run_date,101) from sysjobhistory
where 101 is style argument: https://msdn.microsoft.com/en-us/library/ms187928.aspx
shouldn't it be: CONVERT (varchar(10), run_date, 101). What I am trying to say is that you have an extra parenthesis after run_date.