Right now, I have a column which is in date + timestamp format. Like so:
2022-02-24 17:30:00.000
Does anyone know how to convert this into something like Thursday 17:30:00.000
I don't want to convert anything to a string and concat that way, as then it'll be difficult to graph in snowsight.
Snowflake has a 3 Letter Day, but does not appear to have full name days, for free in the TO_CHAR formatting.
SELECT '2022-02-24 17:30:00.000'::timestamp as my_timestamp,
TO_CHAR(my_timestamp, 'DY HH:MM:SS.FF');
MY_TIMESTAMP
TO_CHAR(MY_TIMESTAMP, 'DY HH:MM:SS.FF')
2022-02-24 17:30:00.000
Thu 17:02:00.000000000
but that is not the long form you requested.
Related
I'm doing the date conversion the following way:
CONVERT(VARCHAR, CONVERT(DATE, ZA2_DTFIM), 103) AS DATA
How can I add 5 days to that system date?
I tried several methods but so far nothing, I'm a beginner in SQL Server.
In TSQL the function GETDATE() provides the system date (and time).
To add 5 days to that use DATEADD(day,5,GETDATE())
To display this as a string you can use FORMAT() or CONVERT() e.g. both of these will add 5 days to the system date and then display that in day/month/year style:
select
convert(varchar,dateadd(day,5,getdate()),103)
, format(dateadd(day,5,getdate()),'dd/MM/yyyy')
Note: you do not need to convert to date because converting (or formatting) GETDATE() to dd/MM/yyyy will suppress display of time anyway.
We receive a csv file that has a column in this date format -- Wed Oct 14 08:00:00 CDT 2020, along with a column that has a count for each date/time
I am using an SSIS package to grab the file and import this data into a sql table, then I can format it the way I need to and then actually export the data in the format needed.
If there is a way to do this all within one SSIS package I am all ears but currently I am working on just getting the data into SQL and converted to the right format so that I can export it.
I need to get that file and convert that date format and split it up into two separate columns
One column will be just the date in this format 2020-10-14 00:00:00.000
One column will be just the time in this format 08:00:00.0000000
Updated to change the dates to match so it's not as confusing and also the error I am receiving when running the suggested code below.
Image of Error I'm recieving
Image of table with the data I am trying to convert
Image of table attributes
Screenshot of my screen when running a select * from the table I am pulling the data that I need converted
Screenshot of the error I receive when running the query by Aaron.
If this is the format it will always be in, and timezone is irrelevant, you can first try to convert it to a datetime, then you can extract the parts from that.
SET LANGUAGE us_english; -- important because not all languages understand "Oct"
;WITH src AS
(
SELECT dt = TRY_CONVERT(datetime, RIGHT(OpenedDateTime ,4)
+ SUBSTRING(OpenedDatetime, 4, 16))
--, other columns...
FROM [dbo].[VIRTUALROSTERIMPORT_Res_Import]
)
SELECT OpenedDateTime = CONVERT(datetime, CONVERT(date, dt)),
OnHour = CONVERT(time, dt)
--, other columns...
FROM src;
Results:
OpenedDateTime OnHour
-------------- ----------------
2020-10-14 08:00:00.0000000
If you need to shift from one timezone to another timezone, that's a different problem.
I was just showing the date formats, don't look so into the actual date examples I used. The time zone is irrelevant I just need the formats changed.
When I used The code Aaron suggested I got a conversion error: I'm assuming its because the columns are varchar in the table, but I cant get the dates to load as date formats bc SSIS keeps giving me truncated errors-- so I have to load it as varchar.
Below is the code I was running, I tweaked it to use the column and table names I am using.
SET LANGUAGE us_english; -- important because not all languages understand "Oct"
DECLARE #foo varchar(36) = 'Wed Oct 14 08:00:00 CDT 2020';
;WITH src(d) AS
(
SELECT TRY_CONVERT(datetime, RIGHT(#foo,4) + SUBSTRING(#foo, 4, 16))
)
SELECT OpenedDateTime = CONVERT(datetime, CONVERT(date, OpenedDateTime)),
onhour = CONVERT(time, OpenedDateTime)
FROM [dbo].[VIRTUALROSTERIMPORT_Res_Import];
I need to convert a string to a valid date format
I have tried using cast and convert functions to no avail.
Convert( mystring, mm/dd/yy)
I have got a string like: Tuesday, July 09, 2019 12:00 AM to get the output mm/dd/yy . I am not able to get this work.
Tuesday, July 09, 2019 12:00 AM---> mm/dd/yy
Try this:
SELECT TRY_PARSE('Tuesday, July 09, 2019 12:00 AM' AS DATETIME USING 'en-US')
to convert to valid date.
Then:
SELECT CONVERT(VARCHAR(10), TRY_PARSE('Tuesday, July 09, 2019 12:00 AM' AS DATETIME USING 'en-US'), 101)
to convert it to mm/dd/yy.
The most important hint was provided by Larnu as a comment: Do not store date as string, rather use the native date formats and think about the string format in output scenarios only.
So hopefully you are trying to fix this technical issue and therefore you need this conversion in order to store the values in neatly typed columns ;-)
The suggestion by gotqn is the best, as you obviously need to use a specific language/culture and TRY_PARSE is the only approach which allows to specify this parameter. TRY_PARSE will need at least v2012 and - this might be a draw back, it is really slow.
So, if this is a one-way-ticket (just one conversion in order to store your values correctly) and you have a very large count of rows, you might go this route:
DECLARE #yourstring VARCHAR(100)='Tuesday, July 09, 2019 12:00 AM';
SET LANGUAGE ENGLISH;
SELECT CAST(SUBSTRING(#yourstring,CHARINDEX(',',#yourstring)+1,100) AS datetime);
The simple idea is: Cut away the leading Weekday's name. The rest is working implitly.
I have 2 x fields T1.period and T1.year both have data type smallint
Using SQL Management Studio 2014 how may I Concatenate them AND return result as a DATE type?
Also, T1.period has values 1 to 12 how may I pad this out to 01 to 12 ... or will changing to date type sort this out?
Much appreciated!
Sample data ...
period yr
1 2015
2 2009
12 2009
11 2010
10 2011
Result will be ...
Date
01/01/2015
01/02/2009
01/12/2009
01/11/2010
01/10/2011
Thanks!
Looks terrible struggling to get it into lists - sorry :(
Converting Your Values The Old Fashioned Way
Assuming that your t1.period value actually just represents a month, you could consider just converting your values to strings and then converting that concatenated result into a date via the CAST() function :
SELECT CAST(CAST(t1.year AS VARCHAR) + '-' + CAST(t1.period AS VARCHAR) + '-1' AS DATE)
This will build a string that looks like {year}-{month}-1, which will then be parsed as a DATE and should give you the first date of the given month/year.
Using The DATEFROMPARTS() Function
SQL Server 2012 and above actually support a DATEFROMPARTS() function that will allow you to pass in the various parts (year, month, day) to build a corresponding DATE object, with a much easier to read syntax :
SELECT DATEFROMPARTS(t1.year,t1.period,1)
Additionally, if you needed a DATETIME object, you could use DATETIMEFROMPARTS() as expected.
If I use this
SELECT CONVERT(DATE, '26/03/2014', 101)
I get an error:
Conversion failed when converting date and/or time from character string.
But if I use this
SELECT CONVERT(DATE, '26/03/2014', 103)
There's no error and this is the result returned:
2014-03-26
I don't understand why the first code is not working, as far as I searched and understand is that 101 is for US date and 103 is for UK/French Date.
This:
SELECT CONVERT(DATE, '26/03/2014', 101)
will be interpreted in the US way (mm/dd/yyyy) : the 26th month, 3rd day of 2014 - this obviously fails (no 26th month).
This however:
SELECT CONVERT(DATE, '26/03/2014', 103)
will be interpreted the European way (dd/mm/yyyy): the 26th day of the 3rd month (March) of 2014.
You need to very careful with parsing strings to date! Check out all the defined styles for CONVERT here
If you want to be sure it works always, use the ISO-8601 format: YYYYMMDD or in your case:
SELECT CAST('20140326' AS DATE)
will always work, no matter what language/regional settings you have
Thats becuase of the format specifier(101) which you are using.
101 is mm/dd/yyyy
So 26 cannot be a month. Hence resulting in error.
103 is dd/mm/yy
And hence it is working correctly. if the day would have been less than 13, it would have taken it as month and there would be logical error.
The convert signature is as follows
CONVERT(data_type(length),expression,style)
for the date conversion, the styles are as follows
101 mm/dd/yy USA
103 dd/mm/yy British/French
more formats here http://www.w3schools.com/sql/func_convert.asp
Use language neutral date representations for literals. In case with style 101, SQL Server assumed the MM/dd/yyyy instead of dd/MM/yyyy.
Here's a nice link with more info from MVP Tibor Karaszi:
http://karaszi.com/the-ultimate-guide-to-the-datetime-datatypes