I searched but wasn't able to find the way to get the date in this format (DD.MM.YYYY)
Help me please change this request:
DECLARE #date datetime
set #date = '01.05.2016'
SELECT [User], cast(DATEADD(SECOND, sum(datediff(DAY, #date,[Start])),#date) as date)'Date'
,cast(DATEADD(SECOND, sum(datediff(SECOND, '00:00:00',[Period])),'00:00:00') as time)'Total time'
FROM [Table].[TableAction]
where
[Start] >= #date+'00:00:00' and [Start] <= #date+'23:59:59'
group by [USER]
DECLARE #date datetime set #date = GETDATE()
Now to output it, you need to "Format" it.
select FORMAT (#date,'MM.dd.yy') as date
The best practice is to store the datetime in datetime format in the database and whenever you need the data you can access it and format it according to your need.
DECLARE #Currentdate DATETIME;
SET #Currentdate=GETDATE(); -- Store cuurent date into variable
And then when you want to display it use the below to format it as dd.MM.YYYY
SELECT CONVERT(VARCHAR(10),GETDATE(),104); -- format the #Currentdate to the required format.
FORMAT works only in SQL Server 2012+. If your database is SQL server 2008 or 2005 FORMAT doesn't work.In that case, you can go for the CONVERT function.
So, If your database is above SQL SERVER 2012, you can go for FORMAT as suggested by Tschallacka
DECLARE #Currentdate DATETIME=GETDATE(); -- Store cuurent date into variable
And then when you want to display it use the below to format it as dd.MM.YYYY
SELECT FORMAT(#Currentdate,'dd.MM.yyyy') -- format the #Currentdate to the required format.
Related
When I'm setting datetime variable value the day and month is switched.
For example:
declare #date datetime = '2017-12-02 14:56:24.000'
select datepart(month,#date)
The result should be 12 but it's 2. The problem exists only on SQL Server 2017. The same code on 2016 returns 12.
The problem exist only on SQL Server 2017. The same code on 2016 returns 12.
It does not depend on SQL Server version, just different DATEFORMAT settings:
SET DATEFORMAT ymd;
declare #date datetime = '2017-12-02 14:56:24.000';
select datepart(month,#date);
-- 12
SET DATEFORMAT ydm;
declare #date2 datetime = '2017-12-02 14:56:24.000';
select datepart(month,#date2);
-- 2
Rextester Demo
To avoid this I suggest to use culture independent ISO-8601 date format.
declare #date datetime = '2017-12-02T14:56:24.000';
Rextester Demo2
or as marc_s suggested change datatype to DATETIME2:
DECLARE #date DATETIME2 = '2017-12-02 14:56:24.000';
Rextester Demo3
I have below string of date time
2017-09-06 00:36:32.473491+05:30
I want to store this date time into MS SQL Server table and my datatype for this column is datetime.
I tried CAST but it gives me the following error
Conversion failed when converting date and/or time from character string.
So please help me to fix this.
I am using MS SQL Server 2014
Here is a super easy alternative...
SELECT CAST(left('2017-09-06 00:36:32.473491+05:30',19) as datetime)
You need to strip out the milliseconds and time offset before you can CAST to DATETIME.
DECLARE #s NVARCHAR(100) = '2017-09-06 00:36:32.473491+05:30'
SELECT CONVERT(NVARCHAR(19), #s, 121)
SELECT CAST(CONVERT(NVARCHAR(19), #s, 121) AS DATETIME)
However, if your datatype was DATETIME2 then you wouldn't need to
DECLARE #s NVARCHAR(100) = '2017-09-06 00:36:32.473491+05:30' SELECT
CONVERT(NVARCHAR(25), #s, 121) SELECT CAST(CONVERT(NVARCHAR(25), #s,
121) AS DATETIME2)
try this
CONVERT(DATETIME,'2017-09-06 00:36:32.473') do the job but not with your precision!
I have a DATETIME variables, in this mode '2017-01-01 08:50:00'
I want to have this: '2017-01-01 08:50' excluding the seconds part .
How can I do this in t-sql?
Try this:
convert(char(16), #date, 20)
where #date is a variable with your datetime for convert. Or add column instead #date variable.
Elegant solution if SQL Server 2012 is used.
SELECT FORMAT(DateColumnName, 'yyyy-MM-dd HH:mm');
I have a field which is a string that contains a date and time, what I do is I substring the field so I could only get the date, where the output looks like this:
2015-03-05 01:00
But I need to convert this to a date format similar to this:
05-Mar-2015 01:00
How do I convert it in SQL Server? This is my current syntax:
SELECT Convert(nvarchar(50),SUBSTRING(TT.FlightArrDate,1,10))+' '+Convert(nvarchar(5),SUBSTRING(TT.FlightArrDate,12,16))as actDateT from tabl1
Is it possible for me to change the date format and at the same time retain the time that it is being concatenated into?
I am using SQL Server 2008
Query
DECLARE #dt VARCHAR(20) = '2015-03-05 01:00'
SELECT REPLACE(CONVERT(VARCHAR(20),CAST(#dt AS DATETIME),106),' ','-')
+' '
+CONVERT(VARCHAR(20),CAST(#dt AS DATETIME),108) AS actDateT;
You can use code like this:
DECLARE #str VARCHAR(20) = '2015-03-05 01:00'
SELECT CONVERT(VARCHAR(20),CAST(#str AS DATETIME),13)
But without character "-" between date parts. Is symbol "-" necessary in your result?
Changing the datetime string into a real datetime, then formatting from there should give you what you want.
declare #d varchar(32)
set #d = '2015-03-05 01:00'
SELECT Convert(nvarchar(50),SUBSTRING(#d,1,10))+' '+Convert(nvarchar(5),SUBSTRING(#d,12,16)) as actDateT
select format(convert(datetime, #d, 120), 'dd-MMM-yyyy hh:mm')
I am using MS SQL SERVER 2008.
I am trying to fetch date column from database.
This value can be null.
How can i handle isnull for datetime column in ms sql server 2008?
If value is null i want to diasplay as '-' or ' '.
You have to convert the datetime column to varchar, then you can use ISNULL.
SELECT ISNULL(LEFT(CONVERT(VARCHAR, dateTimeColumn, 120), 10), '-') AS DateAsString
FROM dbo.TableName
CAST and CONVERT
This may help, please see.
SET DATEFORMAT YMD
DECLARE #DT DATETIME
SET #DT = '2014-05-26' --GETDATE()
SELECT ISNULL(CONVERT(VARCHAR(10),#DT,105),CAST('-' AS VARCHAR(10)))
SET #DT = null
SELECT ISNULL(CONVERT(VARCHAR(10),#DT,105),CAST('-' AS VARCHAR(10)))