Convert DateTime into desired format in SQL Server - sql

In my project we are migrating from an Oracle database to SQL Server.
In Oracle, to get the required date format, we will use the to_char function - it will return the desired format as we mentioned in the second param:
TO_CHAR(TXNDT,'dd/MM/yyyy hh24:mi:ss')
In SQL Server, the same thing can be achieved like below.
convert(varchar(255), TXNDT, 131)
My problem is: I want to get the date format dd-MM-yyyy hh24:mi:ss
I can do this in Oracle:
TO_CHAR(TXNDT,'dd-MM-yyyy hh24:mi:ss')
But how can I achieve the samething in SQL Server?
Any help will be greatly appreciated!

Use format(). I think this does what you want:
format(TXNDT,'dd-MM-yyyy HH:mm:ss')
If you need this as a varchar():
convert(varchar(255), format(TXNDT,'dd-MM-yyyy HH:mm:ss')

CONVERT(Transact-SQL) has all of the style codes for CONVERT. Note, also, that you need to CONVERT to a varchar, not a datetime. datetime has a fixed display format of yyyy-MM-dd HH:mm:ss.sss.
So, for you, it would be CONVERT(varchar(20),TXNDT,120). notice the use of varchar, rather than datetime.

Related

Converting from Date format to date format in oracle

I have a date in a format like **23-APR-20** in oracle SQL and I wanna make it 23-04-2020 in date format also not string,how can I handle this please?
Please use below type conversion,
select to_date(to_char(date_column, 'DD-MON-YY'), 'DD-MM-YYYY') from table_name;
This is where you NLS_DATE_FORMAT works.
Try this:
ALTER SESSION SET NLS_DATE_FORMAT='DD-MM-YYYY';
Then all your dates will be in dd-mm-yyyy format.
Please note that oracle do not store dates in any format. It stores it in its own binary structure. NLS setting just make the format visible to your client.

How change format mask in XMLELEMENT function from Oracle DB 11g CORE 11.2.0.3.0?

I use function XMLELEMENT and XMLATTRIBUTES in my sql query, but I have problems with format date.
Example:
SELECT XMLELEMENT("triggers", XMLATTRIBUTES(3.2 AS "version"),
XMLELEMENT("request", XMLATTRIBUTES(1 AS "num"),
XMLELEMENT("lastname", trigg.last_name),
XMLELEMENT("firstname", trigg.first_name),
XMLELEMENT("middlename", trigg.middle_name),
XMLELEMENT("birthday", trigg.birth_date).....
Field XMLELEMENT("birthday", trigg.birth_date) output to console date in format:
<birthday>1980-01-05</birthday>
I need convert in format mask:
<birthday>05.01.1980</birthday>
Data about date in my datebase saved like 00.00.0000 and have type date.
I tried used function TO_DATE(date, 'DD.MM.YYYY'), TO_TIMESTAMP but this useless
Please, tell me how convert to needed format? Thanks.
You want to convert it FROM a date TO a CHAR, so use TO_CHAR
XMLELEMENT("birthday", TO_CHAR(trigg.birth_date, 'DD.MM.YYYY'))

How to get datepart from datetime in a Column

I want to convert the column (EXECUTION_LOCAL_DATE_TIME) which has datetime format as (YYYY-MM-DD hh:mm:ss.nnnnnnn) to format (YYYY-MM-DD). Ho do i get this. I am working on SQL server management studio
If your intention is to just get the DATE part of a DATETIME then you can just convert the format to DATE (note that this will return a 'Date' datatype, not specifically formatted to a string 'YYYY-MM-DD'.)
eg:
DECLARE #Dt DATETIME = '2019-01-25T12:00:00'
SELECT CONVERT(DATE, #Dt)
Will return '2019-01-25'
You want the CAST() function:
https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql?view=sql-server-2017
Ideally you should return the datetime format from your query and let your presentation layer handle formatting.
However if you are using SQL server 2012 or higher then you can use the Format() function. See the below answer:
Convert Date format into DD/MMM/YYYY format in SQL Server
If you're using SQL Server 2012 or higher you can use the FORMAT() function.
In your case you'd need
SELECT FORMAT(EXECUTION_LOCAL_DATE_TIME, 'yyyy-MM-dd') FROM TABLE_NAME
You can find additional info here
https://www.mssqltips.com/sqlservertip/2655/format-sql-server-dates-with-format-function/
https://learn.microsoft.com/en-us/sql/t-sql/functions/format-transact-sql?view=sql-server-2017
Here Is the Code Worked For me
SELECT convert(varchar, EXECUTION_LOCAL_DATE_TIME, 111) from Tablename
The Execution_Local_Date_Time will be Converted to yyyy/mm/dd format.

How to get date from sql server in user specified format

I want get date from sql server in user specified format using GETDATE() function.
if i give this query
select GETDATE()
then it is displating output date in this format
2015-03-17 07:29:58.377
but i want output like this .
2015-03-17
what statement should be added with query to get the result.
help me from this problem.
Just use convert():
select convert(varchar(10), getdate(), 121)
Just have a look to the following link. which provides more conversion options.
https://msdn.microsoft.com/en-us/library/ms187928.aspx
In SQL Server 2012 onward you may use FORMAT() which is a bit more intuitive than recalling style numbers.
e.g.
select FORMAT( GETDATE() , 'yyyy-MM-dd' )
see: https://msdn.microsoft.com/en-AU/library/hh213505.aspx
however do note the second parameter is case sensitive
'yyyy-MM-dd' works
'YYYY-MM-DD' would only work for the MM

How do I convert this oracle date

I am trying to convert from one date format to another. I am not sure how to write the functions.
My source date looks like 01/15/2009 01:23:15
The format I need is 01152009.
Thanks
Try this.
TO_CHAR(TO_DATE('01/15/2009 01:23:15','MM/DD/YYYY HH:MI:SS'),'MMDDYYY')
More info here,
http://psoug.org/reference/date_func.html
Does this work for you? It assumes the date is in date format but will work with timestamp
select to_char(YourDateField,'DDMMYYYY') from dual;
You can always convert it back to a date using the TO_DATE function if you need that format.
select TO_CHAR(TO_DATE('01/15/2009 01:23:15','MM/DD/YYYY MI:HH:SS'),'MMDDYYYY') from dual
if your field is already of data type date then you should only do:
select TO_CHAR(<fieldname>,'ddmmyyyy') ...