Convert 2017-01-13T06:00:00Z time format to yyyy-mm-dd hh:mm:ss - sql

I'm trying to convert time formats. The database table has time stored in a VARCHAR(50) column in the format of "yyyy-mm-ddThh:mi:ssZ". When running a query, I want the time formatted as "yyyy-mm-dd hh:mi:ss". (I don't care about time zone adjustment.) I don't understand why GETDATE with the style 120 returns what I want, but when I use the field name it does nothing.
SELECT
PlateEffectiveFrom as Value_In_Table,
STUFF(CONVERT(VARCHAR(33),PlateEffectiveFrom, 120),20,4,'') as 'Gets_Rid_of_Z',
CONVERT(varchar(33), PlateEffectiveFrom, 120) AS Does_Nothing,
CONVERT(varchar(33), GETDATE(), 120) AS Format_I_Want
FROM dbo.TVLTagDetails13
WHERE ID = 5
Returns:
Value_In_Table Gets_Rid_of_Z Does_Nothing Format_I_Want
2008-03-12T06:00:00Z 2008-03-12T06:00:00 2008-03-12T06:00:00Z 2019-01-08 11:16:41

The solution to this problem would be to change the data type of the column to DateTime2 if you don't need the time zone data, or DateTimeOffset if you need the time zone data.
Assuming this can't be done, what you do is a simple string manipulation to get from "yyyy-mm-ddThh:mi:ssZ" to "yyyy-mm-dd hh:mi:ss":
SELECT LEFT(REPLACE(PlateEffectiveFrom, 'T', ' '), 19) AS Format_I_Want
FROM dbo.TVLTagDetails13
WHERE ID = 5
Also, following the conversation in the comments - a must read: Aaron Bertrand's Bad habits to kick : choosing the wrong data type

Related

Convert date to ISO 8601 in SQL Server FOR JSON PATH?

I am converting the data into json (using FOR JSON PATH) in SQL Server, one of the column in that data has a datetime datatype which I need to convert to ISO8601
Correct format: 2018-11-13T20:20:39+00:00
When I tried CONVERT(NVARCHAR(33), datetimecolumn, 127), the result is: 2019-10-24T12:35:12.870
When I tried TODATETIMEOFFSET(datetimecolumn, '+00:00'), the result is: 2015-03-31T00:00:00Z
In both cases I am not getting +00:00.
It appears that SQL Server represents a zero offset or UTC time with Z.
If you were to run SELECT TODATETIMEOFFSET(GETDATE(),60), which grabs the current time and applies a 1 hour offset, you would get the format you are expecting. However, I believe that Z denoting UTC is perfectly acceptable in ISO8601, but could be mistaken.
This command seems to output the JSON as expected.
SELECT TODATETIMEOFFSET(GETDATE(),60) as T FOR JSON PATH
If it is vital that the zero offset be represented as 00:00, I believe you will be force to work with strings.
select CAST(CONVERT(NVARCHAR(33), getdate(), 127) as NVARCHAR(35)) + '+00:00' as T
FOR JSON PATH
JSON (as a data-interchange format) doesn't specify the format for date and time values. FOR JSON implicitly converts date and time values to strings, but you can cast the datetime values in your statement with the appropriate type and style before using FOR JSON:
Statement:
SELECT CONVERT(varchar(30), CONVERT(datetimeoffset(0), GETDATE()), 126) AS T
FOR JSON PATH
Result:
[{"T":"2020-04-22T08:02:58+00:00"}]
If you want to convert to a specific time zone, you may try to use AT TIME ZONE:
Statement:
SELECT CONVERT(nvarchar, CONVERT(datetime2(0), GETDATE()) AT TIME ZONE 'Central European Standard Time', 126) AS T
FOR JSON PATH
Result:
[{"T":"2020-04-24T08:33:49+02:00"}]

How to convert YYYY-MM-DD HH:MM:SS TO MM-DD-YYYY HH:MM:SS IN SQL Server?

How to convert yyyy-mm-dd hh:mm:ss (in 24 hours format) to dd-mm-yyyy hh:mm:ss (in 24 hours format? I am using Sql Server 2008.
Given Date Format: 2017-12-18 18:16:49 - Its in DateTime format
Required Date Format: 18-12-2017 18:16:49
This Would work in Older SQL Server Versions Also, Converted to datetime first if it's VARCHAR otherwise you can skip that conversion.
SELECT convert(varchar,convert(datetime,'2017-12-18 18:16:49'),105) + ' ' +
convert(varchar(8),convert(datetime,'2017-12-18 18:16:49'),14);
Use this Link as Reference for Date & Time conversion Formats
Try this
SELECT FORMAT(CAST('2017-12-18 18:16:49' AS datetime) , 'dd-MM-yyyy HH:mm:ss')
DECLARE #date DATETIME = '2017-12-18 18:16:49'
SELECT FORMAT(#date,'dd-MM-yyyy HH:mm:ss')
I would advice not change directly in SQL formate, instead you could change your php code where query fetch the date data
for example you could assign your $yourDate object to new dateTime, and use formate function to define the date format:
<?php
$yourDate = new DateTime();
$timestring = $yourDate->format('m-d-Y h:i:s');
echo $timestring;
the output will be: 05-18-2018 05:00:34
You could take reference in this older discussion
This will prevent some bug error might be occurred if your data table has some other date format relative to, and also it is more useful once you need to have change the date format again
Hope this will be help

How to cast the DateTime to Time

I am casting DateTime field to Time by using CAST Syntax.
select CAST([time] as time) as [CSTTime]
DateTime
2015-03-19 00:00:00.000
Present Output : Time
03:05:36.0000000
I need only HH:MM:SS and not Milliseconds or 0000's
How to filter or Cast it to exact HH:MM:SS Format.
Time is not stored with its display format in SQL Server.
Therefore, from the user perspective, you can say that it has no format.
Of course, that's not completely accurate since it does have a storage format, but as an average user you can't really use it.
This is true for all date and time data types:
Date, DateTimeOffset, DateTime2, SmallDateTime, DateTime and Time.
If you need a format then you don't need to cast to time but to a char. Use Convert to get the char you need:
SELECT CONVERT(char(10), [time], 108) as CSTTime
Here is some background data if you're interested:
In this article published in 2000 the writer explains in depth how SQL Server treats dates and times. I doubt if anything significant changed between 2000 and 2015 in the way SQL Server stores date, time and datetime values internally.
Here are the relevant quotes, if you don't want to read all of it:
So how does SQL Server internally store the dates? It uses 8 bytes to store a datetime value—the first 4 for the date and the second 4 for the time. SQL Server can interpret both sets of 4 bytes as integers.
........
........
SQL Server stores the second integer for the time as the number of clock ticks after midnight. A second contains 300 ticks, so a tick equals 3.3 milliseconds (ms).
since time is actually stored as a 4 byte integer, it really doesn't have a format as an integral part of the data type.
You might also want to check out this article for a more detailed explanation with code samples.
You can achieve it with CAST just simple use TIME(0) datatype in following:
SELECT CAST('2015-03-19 01:05:06.289' AS TIME(0))
OUTPUT:
01:05:06
SQL Server 2008:
select cast(MyDate as time) [time] from yourtable
Earlier versions:
select convert(char(5), MyDate , 108) [time] from yourtable
Other Options:
SELECT CONVERT(VARCHAR(20), GETDATE(), 114)
The simplest way to get the time from datetime without millisecond stack is:
SELECT CONVERT(time(0),GETDATE())
Hour and Minute
SELECT substring(CONVERT(VARCHAR, GETDATE(), 108),0,6) AS Time

Convert Data from yyyy-dd-mm to mm/dd/yyyy Issue

I have a column in my table with Dates in the format yyyy-mm-dd I want to convert all the dates in that column to the format mm/dd/yyyy
I am using the below query
UPDATE Test.dbo.Status
SET DateIn = CONVERT(DATE,DateIn ,101)
The DateIn column is defined as Date in my table (DateIn DATE NULL)
The query does no change to the data. am I doing some thing wrong here?
You can change the default format in which SQL Server displays a date, but you can't alter the way a DATE value is stored via CONVERT(). You can format a date however you want if you store it as a string, but you lose functionality when you do that and it's not advisable. If you are hell-bent on storing a formatted version, you might want to create a new VARCHAR() field so you can preserve your DATE version.
You're better off formatting the date at the application level.
The reason your query does nothing is that the actual DATE values are equivalent. Notice when you take any valid date format and CAST() it as DATE the resulting format is the same regardless of the input:
SELECT CAST('20040510' AS DATE)
SELECT CAST('2004-05-10' AS DATE)
SELECT CAST('May 10, 2004' AS DATE)
All return: 2004-05-10 on my instance of SQL Server.

How do I strip the date off of a datetime string in SQL SSIS?

I'm working on a data warehouse project and would like to know how to (preferably in a Derived Column component in a Data flow) strip the date piece off of a SQL datetime record.
Once I have the datetime converted to just a time I am going to do a lookup on the time to find the related time record in a time dimension table.
Can someone give me a simple function to accomplish this inside a derived column transform?
Example: Transform a datetime such as "12/02/2008 11:32:21 AM" into simply "11:32:21 AM".
I would just do a cast to DT_DBTIME type (using Derived Column transform, or Convert type transform). DT_DBTIME contains just (hours, minutes, seconds) part of the date/time, so you'll get rid of the date part.
If you need to do this in a variable expression Michael's solution won't work, but you can use the following expression:
(DT_DATE)(DT_DBDATE)GETDATE()
(DT_DBDATE) converts the current date and time to a date only. But the new datatype is not compatiple with SSIS's datetime. Therefore you'll have to use (DT_DATE) for converting to a compatible type.
Courtesy of this solution belongs to Russel Loski who has posted it in his blog:
http://www.bidn.com/blogs/RussLoski/ssas/1458/converting-datetime-to-date-in-ssis
Actually if you reverse the first 2 expressions like this: (DT_DBDATE)(DT_DATE)GETDATE()
instead of (DT_DATE)(DT_DBDATE)GETDATE(), then you will TRUNCATE the time off the date field.
If the DT_DATE expression is before the DT_DBDATE expression, you will still have the time portion in your output, but it will be reset to all zeroes.
Ran into this with writing a report for a scheduling app, needed the time that was stored as part of a datetime data type. I formated the datetime as 0 which gives you this mon dd yyyy hh:miAM (or PM), and just did a substring of that which returned the time only in an AM/PM format.
Example below.
DECLARE #S DATETIME = GETDATE()
SELECT SUBSTRING(CONVERT(NVARCHAR(30), #S , 0) , 13 , 10) AS ApptTime
, CONVERT(NVARCHAR(30), #S , 0) AS ApptDate
I personally use a series of functions for this. E.g.:
ALTER FUNCTION [dbo].[TIMEVALUE]
(
#Datetime datetime
)
RETURNS datetime
AS
BEGIN
RETURN (#Datetime - CAST(ROUND(CAST(#Datetime AS float), 0, 1) AS datetime))
END
I'd love to claim all the credit but it should really go to this guy.