How to convert an integer field into a date in Netezza? - sql

i have an integer field which has date values but i would like to convert it as date field. I have tried several methods but with no success. The field has Date values but is stored as an Integer. This is what i have tried:
cast(MYFIELD AS DATE) AS MYCOLUMN
but i get this error "Cannot cast type INT4 to DATE".
I have done several research but coul not find good solution for netezza.

You can concatenate 01 and then run a to_date
select to_date(201004||'01','YYYYMMDD')

I don't think this is something that you can do, not in an obvious way at least. There are hundreds of ways a human could represent a date as an int, so the conversion would not be built in.an int would be something like 20120415 or 04152013 or hundreds of other formats and a date would be something like '2012-04-15'
I suggest you look at the top answer for How i can get the first 3 digits in 123456 Numbers in sql? and extract your data manually. what you should do though, is convert the field into a real date field and edit the dependencies to expect that format.

Related

Simple way to standardize varchar "dates" to single date format in SQL?

I have a table in a postgres database with a varchar date column that mixes MM/DD/YY with MM/DD/YYYY data formats. For example:
1/17/89
1/28/2018
12/30/2006
10/1/17
I'd like all of the dates to follow a MM/DD/YYYY format:
1/17/1989
1/28/2018
12/30/2006
10/1/2017
I'm aware it's not a best practice for dates to be in a varchar field, but I did not create this table and I cannot change the data type. Is it possible to use SQL to make this kind of change to my table?
I'm aware of similar questions like this one, but this seems a bit more than what I'm looking for, and I can't seem to extract an answer from it that's appropriate for my issue.
This question seems closer to what I'm looking for, but again, I can't seem to implement the answer. How would it know which table and field to make changes to? (I'm a total SQL noob if you can't tell).
You can try to use this code:
SELECT to_char(to_date(my_date,'MM/DD/YY')::TIMESTAMP, 'MM/DD/YYYY') as new_varchar_date FROM my_table;
-- for update the actual values
UPDATE my_table SET my_date = to_char(to_date(my_date,'MM/DD/YY')::TIMESTAMP, 'MM/DD/YYYY');
At first you need to convert the varchar to date, then to timestamp and then to varchar again.
Result should look like:
01/17/1989
01/28/2018
12/30/2006
10/01/2017

Converting Varchar(200) to YYYY-MM-DD format in Teradata SQL

I have a Varchar like so:
23FEB2025
I am trying to convert it into a format like:
1994-02-23 or YYYY-MM-DD
I have tried select cast ('23FEB2025' as date format 'yyyy-mm-dd'); and sel convert(date,'23FEB2025')
There are other dates in the column that are formatted like 12DEC65.
I am now starting to assume that there is no simple way to convert this so I am asking for a little guidance. Would i need to take sub strings of the date and use a bunch of select case statements?
I was hoping to find a short way to do this but it seems there might not be one. I read on here that storing dates as a string is a bad idea and I fully subscribe to that notion now.
Thank you for any help or advice!
The format portion of casting a date is the input format. The output format is based on your locale and date settings. In your case, you want this:
select
cast ('23FEB2025' as date format 'ddMMMYYYY')
Which will return 2025-02-23.

Converting from mmddyyyy to yyyymmdd with SQL

I should preface my question by saying I am very new to SQL (or any programming involving databases). I started learning SQL a couple of weeks ago when I decided to take on a data project.
I have been using SSMS in wrangling large tables in comma-separated text file format. I need to be able to sort by dates, and the current format is mmddyyyy, but when I try to sort by date it does not work.
I know that converting dates is something that gets asked a lot around here, but I haven't found any solutions that explain things for a newb like myself.
So far my guesses for a solution are to use the CONVERT or CAST solutions, but I'm not sure if that is the right approach. I have found several CAST/CONVERT posts but none have really applied to my situation.
I hate to have this as my first question, but I'd thought I'd take some down vote bullets if it means I could figure this out. Thank you.
Sample of what I'm trying to do:
SELECT *
FROM [databasename].[dbo].[table1]
WHERE [ column1] > 01012017;
I get the entire table back, unsorted.
Since your:
SELECT *
FROM [databasename].[dbo].[table1]
WHERE [ column1] > 01012017;
does not error, we could say that the [column1]'s datatype is either a character type (like VARCHAR, char), or datetime.
Since you are getting back all the data and I would think you don't have data in the future, it should be a varchar (or char) - with datetime that means 1900-01-01 + 1012017 days.
To make it a datetime you need to 'cast' your column1 which is in mmddyyyy form by first converting it to yyyymmdd style (which would work under any date and language setting):
cast(right([column1],4)+substring([column1],1,2)+substring([column1],3,2) as datetime)
and you would write that 01012017 as a string (quotes around) and also again in yyyymmdd format (it would be implicitly casted to datetime):
'20170101'
So your SQL becomes:
SELECT *
FROM [databasename].[dbo].[table1]
WHERE cast(right([column1],4) +
substring([column1],1,2) +
substring([column1],3,2) as datetime) > '20170101';
Having a date\datetime column as varchar and using like this would render the ability to use simple indexes but that is another matter. For now, this would return the data you want.
Assuming your column's datatype is [Date], try something similar to:
SELECT *
FROM [databasename].[dbo].[table1]
WHERE FORMAT([column1],'dd/MM/yyyy') >'01012017'
If it's string format, you'll have to use CONVERT() to convert the column to Date with a query like
SELECT *
FROM [databasename].[dbo].[table1]
WHERE CONVERT(NVARCHAR(10), [Column1], 112) >'01012017'
Refer to this W3Schools article if you need more help with the CONVERT clause

How to re-order number or change to be date format dd/mm/yyyy in PosgrestSQL?

My question, now I have table customer in Postgresql and contain the column name is update (for keeping track of update customer info date.)
The date format is ex:20170302 but I want to convert to be 02/03/2017.
Note: the datatype of the update is character varying.
I have tried several times to find all the solutions by google but not fix.
First, you should fix the data type to be a proper date or datetime. Don't store dates as strings!
But you are. You can convert the value to a date and then back to a string:
select to_char(to_date(update, 'YYYYMMDD'), 'DD/MM/YYYY')
The documentation contains the formatting elements that you can use.

Insert only Month and Year date to SQL table

I am using MS SQLServer and trying to insert a month/year combination to a table like this:
INSERT INTO MyTable VALUES (1111, 'item_name', '9/1998')
apparently, the above command cannot work since
Conversion failed when converting date and/or time from character string.
Because 9/1998 is a bad format. I want to fix this and this column of the table will show something like:
9/1998
12/1998
(other records with month/year format)
...
Can someone help me with this?
thank you
SQL Server only supports full dates (day, month, and year) or datetimes, as you can see over on the MSDN data type list: http://msdn.microsoft.com/en-us/library/ff848733(v=sql.105).aspx
You can use a date value with a bogus day or store the value as a string, but there's no native type that just stores month/year pairs.
I see this is an old post but my recent tests confirm that storing Date or splitting the year and month to two columns (year smallint, month tinyint) results in the overall same size.
The difference will be visible when you actually need to parse the date to the filter you need (year/month).
Let me know what do you think of this solution! :)
Kind regards
You can just use "01" for the day:
INSERT INTO MyTable VALUES (1111, 'item_name', '19980901')
You can:
1) Change the column type to varchar
2) Take the supplied value and convert it to a proper format that sql server will accept before inserting, and format it back to 'M/YYYY' format when you pull the data: SELECT MONTH([myDate]) + '/' + YEAR([myDate]) ...
You may want to consider what use you will have for your data. At the moment, you're only concerned with capturing and displaying the data. However, going forward, you may need to perform date calculations on it (ie, compare the difference between two records). Because of this and also since you're about two-thirds of the way there, you might as well convert this field to a Date type. Your presentation layer can then be delegated with the task of displaying it appropriately as "MM/yyyy", a function which is available to just about any programming language or reporting platform you may be using.
if you want use date type, you should format value:
declare #a date
SELECT #a='2000-01-01'
select RIGHT( convert (varchar , #a, 103), 7) AS 'mm/yyyy'
if you want make query like SELECT * FROM...
you should use varchar instead date type.