Find number of years between a VARCHAR and DATE in Netezza - netezza

What is the best way to calculate the number of years between a VARCHAR(200) and a date column in Netezza?
My column create_ts is a VARCHAR(200) and my mbr_dt_of_birth is a date but I am having issues when casting and using the datediff functions.
My mbr_dt_of_birth is in the following format 2003-10-06 00:00:00 while create_ts has the following 2017-01-25 16:15:28
I have tried the following snippets of code in my select statement but neither are working, can anyone tell me where i am going wrong please?
months_between(to_date(pahi.CREATE_TS),dmem.MBR_DT_OF_BIRTH)
and
DATEDIFF(year, CAST(pahi.CREATE_TS AS DATE),dmem.MBR_DT_OF_BRTH)

Related

Problem converting an NVARCHAR column value to datetime format mm/dd/yyyy hh:mmAM or PM using tsql

I am working with an audit table that has an nvarchar(255) column that can have many types of different data values based on another field value. For one of those other field values I'm trying to convert the nvarchar value to a datetime value. Here is an example of what I'm trying to do:
declare #d nvarchar(255) = '2022-06-01'
SET #d = CONVERT(datetime,#d)
select #d;
The value returned is: Jun 1 2022 12:00AM
What I am trying to get is: 06/01/2022 12:00AM
I did try the code below but I'm missing the 4 digit year and I want the seconds removed but it's a lot closer than the example above:
declare #d nvarchar(255) = '2022-06-01'
SET #d = CONVERT(varchar,CONVERT(datetime,#d),22);
select #d;
The value returned is: 06/01/22 12:00:00 AM
What I am trying to get is: 06/01/2022 12:00AM
Is there a way to accomplish that? Any help/direction would be greatly appreciated. Thank you.
Thanks #Zhorov and #Melinda,
#Melinda - Glad that you have cleared the issue.
Based on your suggestions posting that as an answer to help the other community members who are facing with the same issue.
Please refer this file for Format SQL Server Dates with FORMAT Function.
Here is the Synatx for Format function which we need to use,
SELECT FORMAT(CONVERT(datetime2(0), '2022-06-15', 23), 'MM/dd/yyyy hh:mmtt') as Date1
SELECT FORMAT(CONVERT(datetime2(0), GETDATE(), 23), 'MM/dd/yyyy hh:mmtt') as Date2
Query with Output:

Converting a string to a datetime in SQL Server 2008

Simple question cannot seem to get right.
I have a table with a date in this format 20/09/2012 as varchar.
I need to convert to a datetime
Tried various solutions but cannot get it right.
Any suggestions?
Thanks
Using the CONVERT function with style 103 (British/European) seems to work for me:
DECLARE #input VARCHAR(20) = '20/09/2012'
SELECT
CONVERT(DATETIME, #input, 103)
With this approach, you could convert all your VARCHAR dates to their proper datatype - DATETIME - and store them as DATETIME in the first place ....
See Aaron Bertrand great blog post on bad habits to kick : mis-handling date / range queries for more background info on why it's really really bad to keep storing dates as VARCHAR columns ....

Excel incorrectly converts Date into Int

I'm pulling the data from SQL database. I have a couple columns with date which need to be converted into Int type, but when I do this the date changes (-2 days). I tried Cast and Convert and it's always the same.
Converting to other type works fine and returns the correct date, but doesn't work for me. I need only the date part from datetime and it needs to be recognised as a date by Excel.
Why is this happening? Any ideas how to get it sorted?
I'm using the following query:
SELECT wotype3, CONVERT(INT,wo_date2 ,103), CAST(duedate AS int) FROM Tasks WHERE
duedate > DATEADD(DAY,1, GETDATE())
AND wo_date2>0
AND wo_date2<DATEADD(WEEK,3,GETDATE())
ORDER BY wotype3
I've had big problems with this, checking my SQL Server's calculation results with "expected results" which a user had created using Excel.
We had discrepancies just because of this 2-day date difference.
Why does it happen ?
Two reasons:
SQL Server uses a zero-based date count from Jan 1 1900, but Excel uses a 1-based date count from Jan 1 1900.
Excel has a bug in it (gasp!) which makes it think that the year 1900 was a leap year. It wasn't. SQL Server correctly refuses to let you have a date value containing "29-Feb-1900".
Combine these two discrepancies, and this is why all dates, from March 1 1900 onwards, are always 2-days out.
Apparently, this Excel bug is a known issue, to keep it in line with Lotus 1-2-3.
The Intentional Date Bug
Microsoft's own explanation
From now on, I think I'll justify bugs in my code with the same excuse.
;-)
For SQL Server 2008 and above, you can use the DATE datatype.
declare #dt datetime = '12/24/2013 10:45 PM' -- some date for example
SELECT #dt as OriginalDateTime, CAST(#dt as DATE) as OnlyDate
For versions prior to SQL Server 2008, you would need to truncate the time part using one or the other functions. Here is one way to do that:
declare #dt datetime = '12/24/2013 10:45 PM' -- some date for example
SELECT #dt as OriginalDateTime, CAST(FLOOR(CAST(#dt AS FLOAT)) as DATETIME) as OnlyDate

how to get today's Date only and not with the time using sql [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Most efficient way in SQL Server to get date from date+time?
I want to update a field with today's date only but the problem i am having now is it is giving me the date and the time. I would like to update with specific format like this:
11/09/2012
how can i achieve this? here is my current code:
UPDATE MyTable
SET Field1 = GETDATE()
One way is to update and select only date (not time)
UPDATE #tab SET dates=(CONVERT(VARCHAR(10),GETDATE(),103));
SELECT CONVERT(VARCHAR, GETDATE(), 23) FROM #tab
Not much different here apart from slight syntax differences however I would recommend wrapping it up in a function.
CREATE FUNCTION [dbo].[GetDateOnly] (#Datetime datetime)
RETURNS datetime
AS
BEGIN
RETURN CONVERT(Datetime, FLOOR(CONVERT(float,#Datetime)))
END

unable to convert varchar to date in sql server

in my data a column called 'duedate' which is in the varchar(5) format has dates stored in the following format '20110725' for 25th july 2011, is there a way in which i can convert this to date format
i tried using
cast(duedate as datetime)
which did not work
then i tried to convert it to bigint and then to datetime
cast( cast(duedate as bigint) as datetime)
which said
arithematic overflow error
Sorry for the confusion it was varchar(50) - typo error, and thanks a lot for the help ill try the things you guys mentioned
If the column is really varchar(5), then it can't have values like '20110725', because that would be 8 characters.
Maybe the values were truncated when they were inserted?
'20110725' shortened to 5 characters becomes '20117', and converting that won't work.
I guess the problem is something like that, because as Mladen Prajdic already said, converting '20110725' to datetime works perfectly.
Please see this table of valid date-time conversion codes. The code you will need is 112 which converts from yyyymmdd format.
CONVERT (datetime, duedate, 112)
how didn't it work? doing SELECT cast('20110725' as datetime) works perfectly for me.
Try running this on your table to find the values that aren't dates:
SELECT duedate
FROM your_table
WHERE ISDATE(duedate) = 0;
Like Christian said, a VarChar(5) won't be able to hold the value you're expecting it to either.

Resources