I'm using SQL Server 2014.
I am required to export data via SQL. One of the fields needs to look like the below: (Not sure what the 'T' is). The source field is in datetime format.
2019-08-15T08:30:00
Is there a specific datatype for this, or will I have to convert to varchar etc and concat the 'T'? As it stands my SQL is pretty simple:
SELECT [Jobf_DteLog] as DateLogged,
FROM [tblJobs]
2019-08-15T08:30:00 is the ISO 8601 date format.
To get the desired output you can use ether FORMAT() or CONVERT() function.
select format(Jobf_DteLog, 'yyy-MM-ddThh:mm:ss')
, convert(varchar(30), Jobf_DteLog, 126)
Demo
Related
I'm trying to convert a nvarchar(max) column to a datetime in SQL Server. The format is dd-MM-yyyy hh:mm:ss, which I'd like to keep, however I'd like to change the type to datetime. I've found many similar questions about this conversion, but not with my particular format.
I've tried CONVERT(nvarchar(MAX), string_column, 121), however without luck. How can this be done? As example, I'd like to convert the string 26-11-2021 08:19:16 to datetime.
This works for me:
SELECT CONVERT(datetime2(0), '26-11-2021 08:19:16', 105);
/* Output: 2021-11-26 08:19:16 */
Recent versions of SQL Server also have the TRY_PARSE method:
SELECT TRY_PARSE('26-11-2021 08:19:16' As datetime2(0) USING 'en-GB')
/* Output: 2021-11-26 08:19:16 */
DECLARE #InString NVARCHAR(20)='26-11-2021 08:19:16';
SELECT #InString;
SELECT CONVERT(DATETIME,#InString,105);
Could you please try if it is suitable for you
I'm trying to add a named calculation field in ssas project, the following expression works with sql server datasource not with an oracle one :
convert(datetime,convert(date,DATE_HR_INSC),110)
Thank you!
I guess you would like to convert your date value into string using format 110, which means mm-dd-yyyy (12-30-2019).
The way to do it in oracle is:
to_char(date,'mm-dd-yyyy')
The first inner step of this SQL Server code is casting DATE_HR_INSC to a date. Then, this is being converted to a datetime with format mask 110. I'm not sure this makes sense, and I would instead have expected to see the following:
CONVERT(varchar, CONVERT(date, DATE_HR_INSC), 110)
This would have generated a datetime with the format mm-dd-yyyy. We can try to approximate this in Oracle using the TO_DATE and TO_CHAR functions. Something like this:
TO_CHAR(TO_DATE(DATE_HR_INSC, 'yyyy-mm-dd'), 'mm-dd-yyyy')
Demo
This assumes that DATE_HR_INSC is in yyyy-mm-dd format. If its format is something else, then replace the mask used in the call to TO_DATE above.
I want to convert datetimeoffset(7) to datetime in SQL Server.
For example: my datetimeoffset(7) is: 2014-11-07 00:00:00.0000000 +05:30
I want convert to Datetime like this: 20141107 (Style 112) without using varchar.
i want convert to Datetime like this: 20141107 (Style 112) without
using varchar.
Datetime and datetimeoffset data types are stored in SQL Server in binary format. Data representations like '2014-11-07 00:00:00.0000000 +05:30' and '20141107' are in fact strings, so in T-SQL, you must convert to varchar in order to format the data as desired for display purposes so that the client application renders the data as the returned formatted string.
It is generally best to format data for display purposes in the presentation layer instead of T-SQL. Client applications typically have more robust formatting capabilities.
I have a TimeStamp (varchar(50),null) column in my SQL Server 2008 table which looks misleading by the name TimeStamp. I mean it appears as if it's a datatype timestamp but it's varchar.
But it has values like 201403240004 which looks like a date. Can I convert it into date and use?
Read online that timestamp is only a sequence of numbers and has nothing to do with date and time.
You can.
Providing that the format is YYYYMMDDHHmm, a simple way to do that would be:
SELECT CONVERT(DATETIME,
SUBSTRING([TimeStamp],1,4)+'-'+SUBSTRING([TimeStamp],5,2)+'-'
+SUBSTRING([TimeStamp],7,2)+' '+SUBSTRING([TimeStamp],9,2)+':'
+SUBSTRING([TimeStamp],11,2)+':00.000')
FROM Table
This will take this "timestamp" and first transform it to SQL-readable datetime string, i.e. for your example it would be 2014-03-24 00:04:00.000. Then, it will be easily converted to datetime.
Yes, your column should be convertible to DATETIME, but you may have to do the converison yourself if CONVERT() does support the format.
I can't tell from you example what the time format really is.
If it is YYYYMMDDHHMM them
SELECT CONVERT(DATETIME,LEFT('201403240004',8),112)
+CONVERT(DATETIME,SUBSTRING('201403240004',9,2)+ ':' + RIGHT('201403240004',2)+':00' ,108)
Its SQL Server 2000.
I am starting with a character string in the format DD/MM/YYYY
Here's the table: http://msdn.microsoft.com/en-us/library/ms187928.aspx
Sample:
select convert(datetime,'22/03/2005', 103)
SET DATEFORMAT dmy
SELECT CAST('22/03/2005' AS datetime)
or
SELECT convert(datetime,'22/03/2005', 103)
It depends your the context.
SQL Server understands '2010-06-21' as a date without requiring any convert/cast, so I would just use the string in the format 'yyyy-mm-dd' if that suits your needs.
Otherwise, the other responses using cast may be better if you need to compare with date fields containing hours as well.