I have one datetime field(startdatetime) store in Netezza DB as varchar because SSIS is truncating milisec and I want datetime till milisec.
So now I have startdatetime value is like 2014-1-2 11:23:31.921000000
and I want it 2014-01-02 11:23:31.921 Length should be 23. How can I do that?
So what I am doing is I want to do incremental load so i am getting max(startdatetime) from the table and compare it to startdatetime (datetime data type) field coming from source so when startdatetime > max(startdatetime), load that new data. So for that purpose I need to convert it to like 2014-01-02 11:23:31.921
Thanks for the help.
I think what you're asking for is the to_char function, specifically to_char(startdatetime, 'YYYY-MM-DD HH-MI-SS.US'). See IBM's documentation.
If that still leaves too many '0' characters on the end, try using the regex functions to replace it.
regexp_replace(to_char(startdatetime, 'YYYY-MM-DD HH-MI-SS.US'), '0+$', '')
Related
I need help in figuring out the date conversion logic in Snowflake. The documentation isn't clear enough on this.
In SQL Server, I would try
SELECT CONVERT(DATE, '20200730', 101)
and it gives me '07/30/2020'.
If I try the following in Snowflake,
to_varchar('20200730'::date, 'mm/dd/yyyy')
it gives me '08/22/1970'. Why would it give an entire different date? Need help in getting the logic with the correct date.
The issue with what you are doing is that you are assuming that Snowflake is converting your string of '20200730'::DATE to 2020-07-03. It's not. You need to specify your input format of a date. So, 2 options based on your question being a bit vague:
If you have a string in a table and you wish to transform that into a date and then present it back as a formatted string:
SELECT TO_VARCHAR(TO_DATE('20200730','YYYYMMDD'),'MM/DD/YYYY');
--07/30/2020
If the field in the table is already a date, then you just need to apply the TO_VARCHAR() piece directly against that field.
Unlike SQL Server, Snowflake stores date fields in the same format regardless of what you provide it. You need to use the TO_VARCHAR in order to format that date in a different way...or ALTER SESSION SET DATE_OUTPUT_FORMAT will also work.
Try select to_varchar(TO_DATE( '20200730', 'YYYYMMDD' ), 'MM/DD/YYYY'); which produces 2020-07-30
You may need to refer to https://docs.snowflake.com/en/user-guide/date-time-input-output.html#timestamp-formats
I am trying to change the date format in Comment field in SQL, My comments field display data some thing like this 'shaik4, jabeen4 7/2/2019 10:11:41 AM',
Comments can be longer until 2000 characters followed by date , i want my date to be in 07/02/2019 format
select Convert(VARCHAR(23), Cast(right(Comments, 23) as Datetime), 106) as Date
FROM *Table*
Might be the trick.
First we get the right side of the comments field to grab the date and time, then we cast it to a datetime value, then we convert our datetime to the appropriate format.
You will need to replace the 23 in comments with the appropriate amount of cells away. So 05/05/2019 11:21:00.000 would be 23. Then the 106 needs to be replaced with the correctly formatted conversion. The convert part should be easy to look up with some external resources.
If this doesn't do the trick, share more info on your database type and hopefully we can work through this together. Thanks
I currently have the date format 2016-11-23 16:47:21.007 and I am trying to convert it with this SQL:
update user_table set login_date = CONVERT(datetime,login_date,120)
So that I only get 2016-11-23 16:47:21 but it's currently just staying as the same format.
Am I missing something?
Your current requirement is mainly a presentation need. Since your login_date is already stored as a datetime, there is no need to change anything internally in your database.
When you need to present the login_date without milliseconds, then go ahead and call CONVERT as you were:
SELECT CONVERT(varchar(19), login_date, 120) AS login_date_no_millis
FROM yourTable
But there is no need to do the UPDATE you showed us in your question.
Altering the column to datatype datetime2(0) will mean milliseconds aren't stored. If you're certain you don't need them.
Equally, casting it to datetime2(0) in your select will do the same in the presentation level whilst maintaining the milliseconds in the database if required. Also means that unlike cast/convert to varchar, it's still a datetime value rather than a string if that makes a difference to the app.
I have a date format 2011-01-06T06:30:10Z in Excel.I want to just load the date part into a table from excel.How do I get the date part from it.
i.e. 2011-01-06
Thanks
Try this:
select cast(TO_TIMESTAMP_TZ(REPLACE('2011-01-06T06:30:10Z', 'T', ''), 'YYYY-MM-DD HH:MI:SS TZH:TZM') as date) from dual
I think, some more explanation is needed.
Loading data into database is one part, and displaying it after fetching is another part.
If you have loaded the data into database, then all you need to do is use TRUNC. It will truncate the time portion and will display only the date portion.
A DATE always has a datetime part together. TIMESTAMP is an extension to the DATE type. And what you see the date looks like is not the way it is stored in database. The format is for we human beings to understand. A date is stored in 7 byte in internal format.
More information Based on OP's question via comments
NEVER store a DATE as VARCHAR2 datatype. A date is not a string literal. Oracle provides lot of FORMAT MODELS to display the datetime the way you want. Sooner or later, you will run into performance issues due to data conversion. Always use explicit conversion to convert a literal to a perfect DATE to compare it with other date value.
I have an ASP page which will fetch records from a SQL server DB table. The table "order_master" has a field called order_date. I want to frame a select query to fetch order date > a date entered by user(ex : 07/01/2008)
I tried with convert and cast, but both are not working. The sample data in order_date column is 4/10/2008 8:27:41 PM. Actually, I dont know what type it is (varchar/datetime).
Is there any way to do that?
I'd check to make sure that the SQL datatype is a DateTime or SmallDateTime first, then I'd check to make sure that you're passing in a Date/DateTime value from the page.
If those are both correct, then you'd probably be better off following Joel's advice and explicitly convert both values to dates before trying the comparison. Also, check the precision of the time values that you're looking at; it seems obvious, but 1/1/2008 12:00:00.001 AM will not be equal to 1/1/2008 12:00:00.000 AM. Yes, I am speaking from experience. :P
the 07/01/2008 date is the British/French annotation, so all you need to do is:
SELECT myColumn FROM myTable WHERE myDateField >= convert(datetime, '07/01/2008 00:00:00', 103)
this code will get all rows where myDateField has the date 7th of January 2008, since 00:00:00 (hh:mm:ss) so, the first second on that day... in simple words, the entire day.
for more info, check Books online on MSDN
You could create a stored procedure like this
CREATE PROCEDURE GetOrders
#OrderDate DATETIME
AS
SELECT
*
FROM order_master
WHERE Order_Date > #OrderDate
GO
Then you can just convert the users input to a date before calling the stored procedure via your ASP code.
Edit
I just noticed the remark about the column type, you can run this command
sp_help order_master
to get column information to find the data type of order_date.
Have you tried CONVERT()'ing both values to a datetime type?
Remember that when comparing dates, 4/10/2008 8:27:41 PM is not equal to 4/10/2008. SQL Server will interpret 4/10/2008 to mean 4/10/2008 12:00:00 AM, and do an exact comparison down to the second. Therefore 4/10/2008 is LESS THAN 4/10/2008 8:27:41 PM
You state you don't know the field type. That would be the first problem to solve, find out. You can do that with:-
SELECT DATA_TYPE
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'Order_Master' AND
COLUMN_NAME = 'Order_Date'
If its not one of the datetime types it should be converted, if that isn't your responsibility then get on to someone who does have the responsibility.
The fact that you are concerned about the 'format' of the date indicates that you may be building the SQL using concatenation. If so stop doing that. Use a command with a parameter and pass in the date as date type.
Now your issue is one of how the date is entered at the client end and getting it into an unambigous format that can be parsed as a date in the ASP code.
If that is not something you have solved add a comment to this answer and I'll expand this answer.