I am using SQL Server and I need to convert this date value 2018-02-23
to something like this Feb 23 2018.
Any ideas?
SQL Server 2012+ has a FORMAT() function which could help you to format the date
SELECT FORMAT(getdate(), 'MMM dd yyyy')
You could also use conversion function convert()
SELECT CONVERT(varchar(12), getdate(), 100)
Try it this way:
SELECT dbo.fnFormatDate (getdate(), ‘MON DD, YYYY’) — JAN 03, 2012
See the link below.
https://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/
Related
I am having difficulty converting dates in a column from YYYY-MM-DD to Mon DD, YYYY
I think I first need to reorganize the dates and then use a case when statement to specify 01 = Jan and so on? Is that correct?
SELECT to_date(column_name, 'MM/DD/YYYY')
FROM table
gives me some incorrect dates
i.e. previous = 2012-01-29 and
result from query = 0197-06-26
Any suggestions? Thanks
I figured it out!
SELECT to_char(date(column_name), 'Mon dd, yyyy')
FROM table
gives me exactly what I need without the need of a case statement.
So I have data that looks like this:
DATE
2019 04 19 03:00:00
I want it to look like this:
DATE
2019 04 19
OR
DATE
04-19-2019
The only catch is the field holding the data is a varchar so what I tried below doesn't work.
select to_char(DATE, 'YYYY/MM/DD') as dateonly, from table_name;
I've also tried using the following:
select trunc(DATE) as date_only from table_name;
But I get values that look like this:
2019 04 19 00:00:00
I guess the above could work but is there a way I can get rid of the trailing 00:00:00s that result from the trunc method that I used above?
If your data format is always like yyyy mm dd hh24:mi:ss then you can use following query to get the sub string:
SELECT SUBSTR('2019 04 19 03:00:00', 1, 10)
Ref: https://docs.oracle.com/database/121/SQLRF/functions196.htm#S
You have text. You want a date.
So use TO_DATE.
select trunc(to_date('2019 04 19 03:00:00','YYYY MM DD HH:MI:SS') )
from dual;
The trunc will remove the time period. And then you'll have your data in a proper DATE format so you can use all of the date specific functions offered in the database.
First, convert to a DATE:
SELECT TO_DATE('2019 04 19 03:00:00', 'YYYY MM DD HH24:MI:SS') FROM dual;
Then, from a DATE you can use TRUNC() or you can use TO_CHAR() to format it the way you want it to appear:
SELECT TO_CHAR( TO_DATE('2019 04 19 03:00:00', 'YYYY MM DD HH24:MI:SS'), 'YYYY MM DD' ) FROM dual;
Hope this helps.
SELECT
Warehouses.Name, CONVERT(TIME,AirwayBillTrucks.CheckOutTime) AS CheckOutTime,
Assuming you are using SQL Server -
This will fetch you 12hr format
SELECT CONVERT(VARCHAR, Your_column_Name, 100) AS 12_hr_format
To show just the time
SELECT RIGHT(CONVERT(VARCHAR, Your_column_Name, 100), 7) AS time_in_12hr_format
OR simply use the code 108
SELECT CONVERT(VARCHAR, Your_column_Name, 108) AS time_in_12hr_format
Conversion -
100 - mon dd yyyy hh:miAM (or PM)
121 - yyyy-mm-dd hh:mi:ss.mmm(24h)
You can see all the type of format conversion here at Microsoft CAST and CONVERT
If SQL Server 2012+
Select Format(GetDate(),'hh:mm:ss tt')
Returns
03:55:30 PM
I am needing to convert a date/time stamp via SQL to the following format:
April 4, 2016
Using
SELECT
DATENAME(MM, GETDATE()) +
RIGHT(CONVERT(VARCHAR(12), GETDATE(), 107), 9) AS [PaymentDate]
gets me almost what I need, however, it puts a leading 0 on the day, leaving me with:
April 04, 2016
How can I get rid of that leading 0 on the day?
Alternatively, if you are using SQL Server 2012 or later, you can use the FORMAT() function for this:
Select Format(GetDate(), 'MMMM d, yyyy')
April 4, 2016
this will work:
QUERY:
SELECT DATENAME(MM, GETDATE())+' '+DATENAME(dd, GETDATE())+' '+DATENAME(yy, GETDATE()) AS [PaymentDate];
RESULT:
I want the date in DD-MMM-YYYY format eg 29-JAN-2015.
I have tried with:
SELECT TRIM(TO_DATE('29 Jan 2015'
,'DD MON YY')) FROM DUAL
I got result as: 29-JAN-15
But I am expecting: 29-JAN-2015 in date format not in char format
Im assuming Oracle DB:
select to_char(SYSDATE, 'dd-Mon-yyyy') from dual
Returns
29-Jan-2015
Thanks for answers.
I got the solution. First we need to alter the session as below:
alter session set nls_date_format='DD-MON-YYYY';
then run the query:
SELECT TRIM(TO_DATE('29 Jan 2015'
,'DD MON YYYY'))
FROM DUAL
Now I got result as:29-JAN-2015
What you are doing is take the string '29 Jan 2015' and make it a date using the format 'DD MON YY'. This should fail of course for '2015' not matching 'yy', but Oracle is lenient here.
Then you use TRIM on the date. But TRIM is for strings. What happens is that you get shown '29 Jan 15'. I am getting shown '29.01.15' instead of the usual '29.01.2015'. However the behavior: Don't use TRIM on dates, its behavior is nowhere specified as far as I am aware. Use TO_CHAR to format a date in output.
If you only select a date without TO_CHAR you get the date shown in some standard format, which can be '29 Jan 2015' or '29 Jan 15' or '29.01.2015' or '01/29/2015' depending on the app you are using and possibly some setting therin.
For completeness sake:
TO_DATE takes a string ('29 Jan 2015' in your case) and converts it to a date. If the string contains names, make sure that you specify the appropriate language setting:
TO_DATE('29 Jan 2015', 'dd mon yyyy', ''NLS_DATE_LANGUAGE=AMERICAN')
To get a formatted string from a date, use TO_CHAR:
TO_CHAR(sysdate, 'dd MON yyyy', 'NLS_DATE_LANGUAGE=AMERICAN')
Usually you don't do that, however. You select a date as is and have your app (written in PHP, Java or whatever) care about how to display it appropriately to the user's computer's settings.
In SQL Server the query
select CONVERT(nvarchar, GETDATE(), 106) as [Converted Date]
returns:
29 Jan 2015
Manu is correct. Oracle publish a full list of date format specifiers.
http://docs.oracle.com/cd/B28359_01/server.111/b28286/sql_elements004.htm#CDEHIFJA
Use CONVERT(VARCHAR(11),GETDATE(),106)
See detailed explanation here:
http://www.w3schools.com/sql/func_convert.asp
Can you try:
SELECT TRIM(TO_DATE(SYSDATE ,'DD MON YYYY')) FROM DUAL
YY will only show 2 ciphers, instead of YYYY that will show 4.