I want to store a datetime format in a datetime column.
The format i want to use is:
14-05-2012 14:08:54
Is the above by the way possible?
The reason for this is that we use Excel sheets to read from the database and using this format will help us a lot in our daily work.
Please advise.
I appreciate your help and answers.
I am not sure if there is an exact output format for the desired result. But I got this to work
SELECT CONVERT(varchar(10), getdate(), 105)
+ ' ' + CONVERT(varchar(8), GETDATE(), 108)
Datetime is datetime, it's not associated with any specific format. If you need to format datetime, use:
select convert( char(10), getdate(), 105) + ' ' + convert( char(8), getdate(), 108)
Related
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 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)))
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
I need to build a date in the yyyy-mm-dd format on an SQL Server.
What's the best way to do this?
Thanks.
Edit
I did try this
DECLARE #IsoDate varchar(10)
SET #IsoDate = CONVERT(varchar(10), GETDATE(), 120)
Would that be a good approach?
Replace GetDate() with the column you are using:
SELECT CONVERT(VARCHAR(10), GETDATE(), 120) AS [YYYY-MM-DD]
And for many other formats / examples consult this: http://www.sql-server-helper.com/tips/date-formats.aspx
SELECT replace(convert(varchar, getdate(), 111), '/', '-')