Change the format of joining date to month-day,year sql - sql

Change the format of joining date to month-day,year for e.g. January 31,1992.
Query
select ename, to_char(joining_date,'Month DD,YYYY.') from emp_demo ;
Error
'to_char' is not a recognized built-in function name.
Table emp_demo format
joining_date
1992-01-31

You seem to be looking to display your date in a given format (which is what to_char() does in Oracle).
In SQL Server, you can use format():
select ename, format(joining_date, 'MMMM dd,yyyy.')
Demo on DB Fiddle:
select format(getdate(),'MMMM dd,yyyy.')
Yields:
May 04,2020.

SQL Server is pretty good about converting dates without a format. Try:
select cast(joining_date as date)
from emp_demo;
Here is a db<>fiddle.
For the inverse transformation, you want to use format() with the format 'MMMM dd,yyyy'.

You can try this query.
Select Convert(Varchar(7), CONVERT(Varchar(20), getdate(), 100))
+ ', ' + Cast(year(getdate()) as varchar(4)) as StatusOn
Select CAST(GETDATE() AS CHAR(3)) + ' ' + CONVERT(char(2), getdate(), 103) + ', '+
CONVERT(char(4), year(getdate())) as StatusOn
Output

Related

Convert MonthName-Year to Year-MonthNumber

I have a custom date string in SQL. I need to convert the date into custom format. Below is the example for it.
SQL Input : 'January-2019' (Month Name-Year format)
Output : '201901' (Year-Month Number format, Month Number must be two digit)
Since you're using SQL 2008 you can use CONVERT(DATE, ...) + CONVERT(VARCHAR, ...):
DECLARE #input AS VARCHAR(20) = 'January-2019'
SELECT CONVERT(VARCHAR(6), CONVERT(DATE, '01-' + #input), 112)
Note that you need to prepend 01- to the string.
You can also try this.
DECLARE #input AS VARCHAR(20) = 'January-2019'
Select Convert(Varchar(6), Cast(Replace(#input, '-', ' 01 ') as DATE), 112)
Live Demo

Formatting Date in sql

Just have an sql query that has a date in the format:
"2018-05-31"
Need to convert it to:
"May-18"
You can use Format function
Select format(getdate(), 'MMM-yyyy')
Try this:
SELECT LEFT(DATENAME(MONTH,GETDATE()),3) + '-' + RIGHT('00' + CAST(YEAR(GETDATE()) AS VARCHAR),2)
SELECT FORMAT(GETDATE(),'MMM yy')
you can try below way
Declare #Date DateTime = '2018-05-31'
Select Format(#Date, N'MMM-yy')
http://sqlfiddle.com/#!18/433d6/194
FORMAT can be very slow. This looks a little less intuative, however, if you have a large dataset, will probably be much quicker:
SELECT STUFF(STUFF(CONVERT(varchar(11),GETDATE(),13),1,3,''),4,3,'-');
This here could work:
declare #date date = '2018-05-31'
select *, FORMAT(DATEFROMPARTS(1900, right(YearMonthKey,2), 1), 'MMMM', 'en-US') +'-'+ substring(YearMonthKey,3,2) as MonthYearName from (
select LEFT(CAST(CONVERT(VARCHAR(8), #date, 112) AS INT),6) as YearMonthKey
)x
I would use convert() with style code 6 :
select replace(substring(convert(varchar(12), datecol, 6), 4, 6), ' ', '-')
You can change the way that a date or timestamp column is display at any time by altering your session to re-set nls_date_format. This will work in SQL*Plus or PL/SQL:
alter session set nls_date_format = 'MON-YY';

how to Select getdate () in yyyy/M format

I want to select getdate() in the format yyyy/M. I tried to write a query
SELECT FORMAT(GETDATE(), 'yyyy/M')
but it is throwing an error.
I am a beginner in SQL. How do I get the yyyy/m format if there is only single digit month? E.g. the query should return 2016/1 when there is only one digit month (it should not return 2016/01) and should return 2016/10 when the month has two digits
How about getting the YEAR and MONTH part of the date and just concatenate them:
SELECT
CAST(DATEPART(YEAR, GETDATE()) AS VARCHAR(4)) + '/' +
CAST(DATEPART(MONTH, GETDATE()) AS VARCHAR(2))
Try this:
SELECT FORMAT(GETDATE(),'yyyy/MM')
SELECT FORMAT(CAST('2015-11-15' AS smalldatetime),'yyyy/M'),
FORMAT(CAST('2015-01-15' AS smalldatetime),'yyyy/M')
Gives:
2015/11 | 2015/1
SELECT CONVERT(VARCHAR(7), GETDATE(), 111) AS [YYYY/MM]
OR
SELECT CONVERT(VARCHAR(7), GETDATE(), 111)

SQL server 2008 date conversion formats

I produce a CSV file but cannot figure out the proper date format.
I am aware of the MSDN site codes for datetime conversions:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
It seems there is no code to convert my datetime into this format:
MM/DD/YYYY HH:MMAM
e.g.:
12/28/2014 4:33AM
How do you achieve such format?
Platform:
Microsoft SQL server 2008
SELECT CONVERT(VARCHAR(10), GETDATE(), 101) +
RIGHT(CONVERT(VARCHAR, GETDATE(), 100), 7)
This is what you can use and is probably the most straightforward:
SELECT
RIGHT('0' + cast(month(dateColumn) AS NVARCHAR(2)), 2) + '/' -- generate the day
+ RIGHT('0' + cast(day(dateColumn) AS NVARCHAR(2)), 2) + '/' -- generate the month
+ cast(year(dateColumn) AS NVARCHAR(4)) + ' ' -- generate the year
+ convert(VARCHAR, cast(dateColumn AS TIME)), 100) -- generate the time
FROM TABLE
SELECT CONVERT(VARCHAR, GetDate(), 101) + ' ' +
CONVERT(VARCHAR, DATEPART(hh, GetDate())) + ':' +
RIGHT('0' + CONVERT(VARCHAR, DATEPART(mi, GetDate())), 2) AS TIME
EDIT: This gets the AM/PM also
SELECT CONVERT(CHAR(11),GETDATE(),101)
+ CONVERT(CHAR( 5),GETDATE(),114)
+ RIGHT(CONVERT(CHAR( 5),GETDATE(),109), 2)
SELECT CONVERT(VARCHAR(10),GETDATE(),3) as 'dd/MM/yy'
SELECT CONVERT(VARCHAR(10),GETDATE(),103) as 'dd/MM/yyyy'
SELECT CONVERT(VARCHAR(10),GETDATE(),4) as 'dd.MM.yy'
SELECT CONVERT(VARCHAR(10),GETDATE(),104) as 'dd.MM.yyyy'
SELECT CONVERT(VARCHAR(10),GETDATE(),5) as 'dd-MM-yy'
SELECT CONVERT(VARCHAR(10),GETDATE(),105) as 'dd-MM-yyyy'
SELECT CONVERT(VARCHAR(10),GETDATE(),6) as 'ddMonthyy'
SELECT CONVERT(VARCHAR(10),GETDATE(),106) as 'ddMonthyyyy'
SELECT CONVERT(VARCHAR(10),GETDATE(),7) as 'Monthdd.yy'
SELECT CONVERT(VARCHAR(10),GETDATE(),107) as 'Monthdd.yyyy'
SELECT CONVERT(VARCHAR(10),GETDATE(),8) as 'hh.mm.ss'
SELECT CONVERT(VARCHAR(10),GETDATE(),108) as 'hh.mm.ss'
SELECT CONVERT(VARCHAR(100),GETDATE(),9) as 'Monthddyy hh.mm.ss.mss'
SELECT CONVERT(VARCHAR(100),GETDATE(),109) as 'Monthddyyyy hh.mm.ss.mss'
SELECT CONVERT(VARCHAR(100),GETDATE(),10) as 'mm-dd-yy'
SELECT CONVERT(VARCHAR(100),GETDATE(),110) as 'mm-dd-yyyy'
SELECT CONVERT(VARCHAR(100),GETDATE(),11) as 'yy/MM/dd'
SELECT CONVERT(VARCHAR(100),GETDATE(),111) as 'yyyy/MM/dd'
SELECT CONVERT(VARCHAR(100),GETDATE(),12) as 'yyMMdd'
SELECT CONVERT(VARCHAR(100),GETDATE(),112) as 'yyyyMMdd'
SELECT CONVERT(VARCHAR(100),GETDATE(),13) as 'ddMonthyy hh.mm.ss.mss'
SELECT CONVERT(VARCHAR(100),GETDATE(),113) as 'ddMonthyyyy hh.mm.ss.mss'

Get Hours and Minutes (HH:MM) from date

I want to get only hh:mm from date.
How I can get this?
I have tried this :
CONVERT(VARCHAR(8), getdate(), 108)
Just use the first 5 characters...?
SELECT CONVERT(VARCHAR(5),getdate(),108)
You can easily use Format() function instead of all the casting for sql 2012 and above only
SELECT FORMAT(GETDATE(),'hh:mm')
This is by far the best way to do the required conversion.
Another method using DATEPART built-in function:
SELECT cast(DATEPART(hour, GETDATE()) as varchar) + ':' + cast(DATEPART(minute, GETDATE()) as varchar)
If you want to display 24 hours format use:
SELECT FORMAT(GETDATE(),'HH:mm')
and to display 12 hours format use:
SELECT FORMAT(GETDATE(),'hh:mm')
Following code shows current hour and minutes in 'Hour:Minutes' column for us.
SELECT CONVERT(VARCHAR(5), GETDATE(), 108) +
(CASE WHEN DATEPART(HOUR, GETDATE()) > 12 THEN ' PM'
ELSE ' AM'
END) 'Hour:Minutes'
or
SELECT Format(GETDATE(), 'hh:mm') +
(CASE WHEN DATEPART(HOUR, GETDATE()) > 12 THEN ' PM'
ELSE ' AM'
END) 'Hour:Minutes'
The following works on 2008R2+ to produce 'HH:MM':
select
case
when len(replace(replace(replace(right(cast(getdate() as varchar),7),'AM',''),'PM',''),' ','')) = 4
then '0'+ replace(replace(replace(right(cast(getdate() as varchar),7),'AM',''),'PM',''),' ','')
else replace(replace(replace(right(cast(getdate() as varchar),7),'AM',''),'PM',''),' ','') end as [Time]
You can cast datetime to time
select CAST(GETDATE() as time)
If you want a hh:mm format
select cast(CAST(GETDATE() as time) as varchar(5))
Here is syntax for showing hours and minutes for a field coming out of a SELECT statement. In this example, the SQL field is named "UpdatedOnAt" and is a DateTime. Tested with MS SQL 2014.
SELECT Format(UpdatedOnAt ,'hh:mm') as UpdatedOnAt from MyTable
I like the format that shows the day of the week as a 3-letter abbreviation, and includes the seconds:
SELECT Format(UpdatedOnAt ,'ddd hh:mm:ss') as UpdatedOnAt from MyTable
The "as UpdatedOnAt" suffix is optional. It gives you a column heading equal tot he field you were selecting to begin with.
TO_CHAR(SYSDATE, 'HH')
I used this to get the current hour in apex PL/SQL