Datetime format help - sql

I want only Hr and Min in time format so i did
select convert(varchar(20),GETDATE(),108) ===>> output is "12:57:54"
but i want only "12:57" this much so how i will do that in sql server

select convert(varchar(5),GETDATE(),108)

Select cast(DatePart(hour, getdate()) as varchar(2)) + ':' + cast(DatePart(minute, getdate())as varchar(2))

Related

SQL SERVER -- YEAR

Select (SELECT YEAR(GETDATE())+(Select('-'))+(SELECT YEAR(GETDATE())+1));
I want output as "2018-2019".
Anyone?
You can use + to append string since you are working with SQL 2008, higher version has CONCAT() function :
SELECT CAST(YEAR(GETDATE()) AS VARCHAR(4)) + ' - ' + CAST(YEAR(DATEADD(YEAR, 1, GETDATE())) AS VARCHAR(4))
Use datename():
select (datename(year, getdate()) + '-' + datename(year, dateadd(year, 1, getdate()))
That way, you don't have to deal with converting integers to strings.
SELECT CONCAT(YEAR(GETDATE()), '-', YEAR(GETDATE()) + 1)
CONCAT takes care of conversion to string, so you don't have to worry about that.
edit: Unfortunately I have been made aware that CONCAT is only available in sql server 2012 and higher.
You can also use
SELECT CAST(YEAR(GETDATE()) AS VARCHAR(4)) +
'-' +
CAST(YEAR(GETDATE()) + 1 AS VARCHAR(4))
OR
SELECT CAST(DATEPART(Year,GETDATE()) AS VARCHAR(4)) +
'-' +
CAST(DATEPART(Year, GETDATE()) + 1 AS VARCHAR(4))

How to convert separated Year(int), Month(int) and Day(int) columns into DateTime

Our ERP system holds year, month and day in separate int columns. I want to combine those 3 int columns into one DateTime, How can I convert it in the SQL Server.
Year | Month | Day
--------------------
2016 | 1 | 23
You could use DATEFROMPARTS(SQL Server 2012+):
SELECT DATEFROMPARTS([Year], [Month], [Day]) AS DateTim
FROM table_name
SQL Server 2008 (assuming that year is 4-digit):
SELECT CAST(CAST([year] AS VARCHAR(4)) + '-' +
CAST([month] AS VARCHAR(2)) + '-' +
CAST([day] AS VARCHAR(2))
AS DATE) AS DateTim
FROM table_name;
LiveDemo
As Gordon Linoff proposed in comment to utilize YYYYMMDD as INT to convert to DATE:
SELECT CAST(CAST([year] * 10000 + [month]*100 + [day] AS VARCHAR(8))
AS DATE) AS DateTim
FROM table_name;
LiveDemo2
Addendum
To address ypercubeᵀᴹ concerns about dateformat we could utilize ISO-8601 which is not affected by DATEFORMAT or LANGUAGE settings:
yyyy-mm-ddThh:mm:ss[.mmm]
SELECT CAST(RIGHT('000'+ CAST([year] AS VARCHAR(4)),4) + '-' +
RIGHT('0' + CAST([month] AS VARCHAR(2)),2) + '-' +
RIGHT('0' + CAST([day] AS VARCHAR(2)),2) + 'T00:00:00' AS DATE)
FROM table_name
LiveDemo3
To handle years before 1000 should be padded with zeros.
We can use SQL's CONVERT function to get Datetime value :
SELECT CONVERT(DATETIME, CONVERT(VARCHAR(2), Day) + '/' + CONVERT(VARCHAR(2), Month) + '/' + CONVERT(VARCHAR(4), Year),103) FROM Table_Name

Sql Server select datetime without seconds

I have datetime column value below
2015-01-04 20:37:00.000
I tried below
cast(cast(MyDateColumn as date) as datetime)+cast(datepart(hour,MyDateColumn ) as float)/24
as MyDateColumn
and
CAST(CONVERT(CHAR(16),MyDateColumn,113) AS datetime) as MyDateColumn
These are did not work for me
How can i get above datetime as 01-04.2015 20:37 ?
Since MS SQL 2012, you can use FORMAT,
SELECT FORMAT([MyDateColumn], 'dd-MM.yyyy HH:mm')
In MYSQL it will work
SELECT DATE_FORMAT(date, '%Y-%m-%d %H:%i') AS formated_date FROM table;
In MS SQL It will work
SELECT FORMAT(getdate(), 'dd-mm-yyyy HH:mm')
In SQL Server this will work:
DECLARE #now [datetime];
SET #now = GETDATE();
SELECT
CONVERT([varchar](10), #now, 105) + ' ' +
RIGHT('0' + CONVERT([varchar](2), DATEPART(HOUR, #now)), 2) + ':' +
RIGHT('0' + CONVERT([varchar](2), DATEPART(MINUTE, #now)), 2);
In SQL Server this should do the trick:
declare #dt datetime = '2015-01-04 20:37:00.000'
select right('0' + cast(DATEPART(MM, #dt) as varchar), 2) + '-'
+ right('0' +cast(DATEPART(DAY, #dt) as varchar), 2) + '.'
+ cast(DATEPART(YEAR, #dt) as varchar) + ' '
+ right('0' +cast(DATEPART(HOUR, #dt) as varchar), 2) + ':'
+ right('0' +cast(DATEPART(MINUTE, #dt) as varchar), 2)
Simply,
SELECT CAST(CONVERT(varchar, GETDATE(), 100) as datetime)
Here's another way and you get a datetime in return.
SELECT DATEADD(
MILLISECOND,
DATEPART(MILLISECOND, '2016-02-16 13:45:24.573') * -1,
DATEADD(SECOND, DATEPART(SECOND,'2016-02-16 13:45:24.573') * -1,
'2016-02-16 13:45:24.573')
)
this is the way i do it. I needed to get -2 minutes
select CONVERT(datetime, CONVERT(CHAR(18), DATEADD(minute, -2, getdate()) , 113) + '00')
Format(Cast(Convert(varchar(15),Cast(timeval as Time),100) as DateTime),'hh:mm tt') As newtime
This will remove seconds from time as well as add AM,PM with time.

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'

Concatenating two TIME columns in SQL Server 2012

I want to concatenate two TIME columns and show as one column.
Example:
FromTime: 9:00
ToTime: 12:00
Result should be:
9:00-12:00
Generic SQL:
-- hh:mm:ss
SELECT 'result:' + CONVERT(CHAR(6), FromTime, 8) + '-' + CONVERT(CHAR(6), ToTime)
FROM yourTable
MySQL:
-- hh:mm
SELECT 'result:' + DATE_FORMAT(FromTime, '%H:%i') + '-' + DATE_FORMAT(ToTime, '%H:%i')
FROM yourTable
SQL Server:
-- hh:mm
SELECT 'result:' + convert(char(2), DATEPART(hh, FromTime)) + ':' +
CONVERT(CHAR(2), DATEPART(mm, FromTime)) + '-' +
CONVERT(CHAR(2), DATEPART(hh, ToTime)) + ':' +
CONVERT(CHAR(2), DATEPART(mm, ToTime))
FROM yourTable
declare #FromTime time
declare #ToTime time
set #FromTime='9:00'
set #ToTime='12:00'
select cast(#FromTime as varchar(10))+ '-' + cast(#ToTime as varchar(10)) as result
sql demo
You can use convert
select convert(VARCHAR(5),getdate(),108) + ' - ' + convert(VARCHAR(5),getdate()-1,108)