Formatting Date in sql - 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';

Related

Change the format of joining date to month-day,year 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

How to convert custom string to Date in SQL Server

How to convert yyyyMMddhh (2017092018) string to Date in SQL Server 2012?
Is it possible to do without using T-SQL to put the sentence into the INSERT clause?
INSERT INTO [dbo].[Table](SomeColumn)
VALUES (CONVERT(DATETIME, '2017092018', 'yyyyMMddhh'));
Example
Declare #S varchar(50)='2017092018'
Select convert(datetime,left(#S,8)) + convert(datetime,right(#S,2)+':00')
Returns
2017-09-20 18:00:00.000
If 2012+, I would suggest try_convert() just in case you have some unexpected values.
Alternate approach using STUFF:
DECLARE #val VARCHAR(25) = '2017092018';
SELECT CONVERT(DATETIME,STUFF(#val, 9, 0, ' ') + ':00')
This adds a space before the hour, then adds :00 for the minute value.
Here is yet another approach to this. It is similar to what John Cappelletti posted.
Declare #S varchar(50)='2017092018'
Select dateadd(hour, convert(int, right(#s, 2)), left(#s, 8))
You could use DATETIMEFROMPARTS:
DECLARE #d NVARCHAR(10)='2017092018';
SELECT DATETIMEFROMPARTS(LEFT(#d,4),SUBSTRING(#d,5,2),SUBSTRING(#d,7,2),RIGHT(#d,2),0,0,0 ) ;
Rextester Demo
EDIT:
Another option:
DECLARE #S varchar(10)='2017092018'
SELECT CAST(LEFT(#s, 8) AS DATETIME) + RIGHT(#s,2)/24.0;
Rextester Demo2

How to Convert nvarchar (including time) into datetime

I have data 20160526094432, and I want to convert into datetime in SQLServer
The result will be 2016-05-26 09:44:32
Is there simple way to do that ?
Thanks
If you use MS SQL Server 2012 or newer then you can enjoy format function.
select cast(format(20160526094432,'####-##-## ##:##:##') as datetime) [date-time]
If your long number is a string then you have to convert it.
declare #d varchar(20)='20160526094432'
select cast(format(cast(#d as bigint),'####-##-## ##:##:##') as datetime) [date-time]
Hmmm. I don't think there is a really clean way, but something like this should work:
select (convert(datetime, left(col, 8) as datetime) +
convert(datetime, convert(time,
stuff(stuff(right(col, 6), 5, 0, ':'), 3, 0, ':')
)
)
)
Maybe you can try in this way, for example Date is the column of your table, 103 is the format of Date you want to convert, google for more details.
CONVERT(datetime, Date, 103)

convert String YYYY_MM_DD to date

For some reason the developer create the date column in my DB as a string and it is stored as YYYY_MM_DD.
Does anyone know how I can convert the YYYY_MM_DD to a date field via SQL. e.g
2014_06_30 to 30/6/2014.
Or any other solutions
Thank you in advance
Please try:
DECLARE #str NVARCHAR(100)='2014_06_30'
select CONVERT (DATETIME, REPLACE(#str, '_', '-'))
To convert it to format 30/6/2014, try:
select CONVERT(NVARCHAR(20), CONVERT(DATETIME, REPLACE(#str, '_', '-')), 103)
Try this,
DECLARE #str NVARCHAR(100)='2014_06_30';
select RIGHT(#str,2)+'/'+SUBSTRING(#str,6,2)+'/'+LEFT(#str,4)
var myDate = '2014_06_30';
var myNewDate = select CONVERT (datetime, Replace(myDate,'_','/'))

Getting Month and Day from a date

It is possible simplify this query (MSSQL)?
SELECT RIGHT(STR(MONTH('2014-05-02'))+100,2) + '-'
+ RIGHT(STR(DAY('2014-05-02'))+100,2)
I want month and day from a date but always with leading zeros if only have one digit in the month / day. In this date 2014-05-02 I want only 05-02. I can do that with this query but I don't know if there is a simply way...
SELECT CONVERT(CHAR(5), GETDATE(), 10)
Result:
05-23
Please try:
select RIGHT(CONVERT(nvarchar(10), GETDATE(), 126), 5)
For the example,
select RIGHT(CONVERT(nvarchar(10),CONVERT(DATETIME, '2014-5-2'), 126), 5)
Try this...
SELECT LEFT(CONVERT(VARCHAR(10), GETDATE(), 110),5)
OR
SELECT LEFT(CONVERT(VARCHAR(8), GETDATE(), 10),5)
Both return the same result as follows
RESULT: 05-23
I like using format.
If you have a string:
select format(convert(date, '2014-05-23', 126),'MM-dd');
If you have a date:
select format(GetDate(),'MM-dd');
This could really Help you.
DECLARE #A DATETIME = GETDATE()
SELECT CONVERT(VARCHAR(5),#A,110)