Im trying to convert date value into this MMMYYDD String format, using native Sql server 2008 r2 function,
I checked here http://www.sql-server-helper.com/tips/date-formats.aspx
and still I can't find matching format.
any ideas?
something like this?
select convert(varchar(3), #date, 7) + convert(varchar(4), #date, 12)
sql fiddle demo
Related
I created a SELECT using the following in SQL Server 2012:
SELECT
CAST(FORMAT(CONVERT(DATETIME, date_time, 127), 'yyyy-MM-ddTHH:mm:ssZ') AS NVARCHAR(20)) TimeStamp,
FROM myTable
This will result in a date formatted like 2019-03-15T13:25:19Z
How can I achieve the same result using SQL Server 2008 R2 or older?
You can achieve this far more easily by just using CONVERT:
SELECT CONVERT(varchar(19),GETDATE(),127) + 'Z';
As I mentioned in my comment FORMAT is actually an awful function, it performs terribly. I posted an answer to another question earlier today on just how badly it does compared to a CONVERT. Don't just use this expression on your 2008- servers, replace the FORMAT expression on your 2012+ servers with this one too.
I think this does what you want:
select replace(convert(varchar(255), getdate(), 120), ' ', 'T') + 'Z'
Code 127 returns milliseconds, which you do not seem to want, so 120 seems more appropriate.
Hi good day I have the following code I am trying to convert from sql server to oracle, It works perfectly on sql but not on oracle. How can I convert the ff?
This is the code from sql
Select CONVERT(VARCHAR, RLL.dbo.AlphaToDate4(A.DATE_R), 103) CaptureDate
from Employees
This is the format of Date_R (78684)
I am trying to write the following code in oracle but gives me an error
select TO_DATE((A.DATE_R),'DD/MM/YYYY') CAPTUREDATE
Assuming that your number represents the number of days from 31/12/1799 (are you sure?), you may need to add the column value to this date, that is
select date '1799-12-31' + date_R
If you need a formatted string:
select to_char( date '1799-12-31' + date_R, 'dd/mm/yyyy')
I have the date in this format 2017-02-03 and I want that my date should be in this format 01-Mar-2016 while making select command.
I am using SQL Server 2012
Check this website. This will be helpful.
select replace(convert(varchar, getdate(), 106),' ','-')
What is done here is: First convert it into '01 Mar 2016' and then replace white spaces(' ') with '-'. Which will give desired output as '01-Mar-2016'
Use SQL Server Format function where you can specify .NET format string
For instance for current date
SELECT FORMAT(GETDATE(), 'dd-MMM-yyyy');
Also you can specify culture here if needed
SELECT FORMAT(GETDATE(), 'dd-MMM-yyyy', 'en-US');
If you dates are stored as string you'd better fix this but you can just CAST or CONVERT them instead. I.e.
SELECT FORMAT(CAST('2017-10-11' AS date), 'dd-MMM-yyyy');
I am trying to port MySQL function DATE_FORMAT(date,'%y-%m-%d %h:%i:%s %p') to MsSQL equivalent by using CONVERT().
How to format equivalent datetime string in SQL SERVER 2012 to give this output '2014-05-24 01:24:37 AM'?
In SQL Server 2012 and up you can use FORMAT():
SELECT FORMAT(CURRENT_TIMESTAMP, 'yyyy-MM-dd hh:mm:ss tt')
In prior versions, you might need to concatenate two or more different datetime conversions to get what you need, for example:
SELECT
CONVERT(CHAR(10), CURRENT_TIMESTAMP, 23) + ' ' +
RIGHT('0' + LTRIM(RIGHT(CONVERT(CHAR(20), CURRENT_TIMESTAMP, 22), 11)), 11);
See the Date and Time Styles section of CAST and CONVERT (Transact-SQL) for all of the built-in formatting styles.
I would keep in mind that unless you have a good reason for it, I mean a really good reason, formatting is usually a better job for the technology displaying the data.
In a SQL Server 2000 DB, I have a table which holds string representations of Oracle DB dates. They are formatted like "16-MAY-12". I need to convert these to datetime. I can not seem to find a conversion style number that matches, nor can I find a function that will allow me to specify the input format. Any ideas?
This seems to work for me:
SELECT CONVERT(DATETIME, '16-MAY-12');
You can also try using TO_CHAR() to convert the Oracle values to a more SQL Server-friendly format (best is YYYYMMDD) before pulling them out of the darker side.
Follow Aaron's advice and cast to string on the Oracle side and then did a check/recast on the MS SQL side. See example below:
;WITH SOURCE AS (
SELECT * FROM openquery(lnk,
'SELECT
TO_CHAR(OFFDATE , ''YYYY-MM-DD HH24:MI:SS'') AS OFFDATE,
FROM
ORACLE_SOURCE')),
SOURCE_TRANSFORM AS
(
SELECT
CASE
WHEN ISDATE(OFFDATE) = 1 THEN CAST(OFFDATE AS DATETIME)
ELSE NULL END AS OFFDATE
FROM
SOURCE
)
SELECT * FROM SOURCE_TRANSFORM