Converting String field to a custom datetime format in SQL Server - sql-server

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')

Related

Trouble formatting date and time values in SQL Server in a T-SQL stored procedure

I have a simple stored procedure where part of it I want to set 2 variables, 1 for the current time and the other for the current date. I need them in hhmm format for the time and yyyyMMdd format for the date.
Here is the code I have so far:
BEGIN
DECLARE #d AS DATETIME
DECLARE #t as TIME
SET #d = GETDATE()
SET #t = SYSDATETIME()
But everything I've tried to use to change the format of those 2 variables does not help me out. The only examples I've found online is for formatting values in regular queries.
Can anyone point me in the right direction as far as what I should do to get these values? Thanks in advance.
If 2012+ you can use Format()
Example
DECLARE #d as varchar(8) = format(GetDate(),'yyyyMMdd')
DECLARE #t as varchar(4) = format(SYSDATETIME(),'HHmm') -- use hh for 12 hour time
Select #d,#t
Returns
(No column name) (No column name)
20180511 1738
For 2005
DECLARE #d as varchar(8) = convert(varchar(10),GetDate(),112)
DECLARE #t as varchar(8) = replace(left(convert(varchar(25),SYSDATETIME(),108),5),':','')

MS SQL Server String to Datetime

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!

How to convert datetime to datetime never second (TSQL)

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');

Getting the current date in SQL Server

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.

DateTime convert in SQL

I have DateTime saved as Integer in row of table in format 20111111.
I want to display it by select to : 2011-11-11.
Please give me some as simpliest solution.
The Datetime is in table and looks like:
Table_name
20111212
20111113
20111212
and i want to display it like
Table_name
2011-12-12
2011-11-13
2011-12-12
MS SQL Server 2012
Please try
declare #a int
set #a ='20111212'
select stuff(stuff(#a,len(#a)-3,0,'-'),len(#a)-0,0,'-')
--> 2011-12-12
fiddle demo
You can cast it to varchar, then to datetime, then use CONVERT with the proper style:
SELECT CONVERT(VARCHAR(10),CAST(CAST(20111111 AS VARCHAR(10))AS DATETIME), 120)
Demo
Try this
CONVERT(varchar, Year(column)) +'_' + CONVERT(varchar, Month(column)) +'_'+
CONVERT(varchar, Date(column))
Problem solved by:
CONVERT(date, CAST(dbo.database_name.table_name AS CHAR(12)), 112) AS table_name

Resources