How do I get just the date from a field which is labelled as for example "2015/16 Text" when I just want the date from it?
You can get the date from a string using the STR_TO_DATE() function.
SELECT STR_TO_DATE(StringColumn, '%Y/%m') FROM table
The %m and %Y are date placeholders. In the resource section of my answer you can find out more about these and which to use in your case.
Resource
Visit this page for more information
Related
I read other similar question where the answer is to use FORMAT(getdate(),'yyy-MM') or something similar. However the problem for me in using anything like this, is that it changes the date type to a varchar or char. I need it to stay as datetype but just want Year and Month.. I tried the following..-> FORMAT(a.completeddate,'yyy-MM') which works to change to year and month but the date is no longer a datetype or date format. So when I try to do the following -> select #FirstCompletion = (A.completeddate) i get this error..Conversion failed when converting date and/or time from character string. Basically I need to convert the date column to year and month as date format so I can then pass values to variables using select #FirstCompletion = (A.completeddate) and set #secondMonth = DATEADD(month, 2, #FirstCompletion) which are Datetype variables.. Would appreciate any help I can get.. Thanks..
Below is the date format for the 'date' field, which I'm getting from API to Bigquery as a string.
2020-09-17T00:00:00+03:00
I want to parse the date format to '%Y%m%d' and then extract it as ISOWEEK.
This is the code I came up with;
EXTRACT(ISOWEEK FROM PARSE_DATETIME("%Y%m%d", REGEXP_EXTRACT(date, r"...................")))
However, this gives me an error.
Could you guide me what needs change?
Thank you
Let's break down the steps here. The date input is a string of a date in a certain timezone (+03:00), for example "2020-09-17T00:00:00+03:00". Then on this string you can apply:
Keep the date on the correct timezone
DATE("2020-09-17T00:00:00+03:00", "+03:00")
Format the date with the desired format in a string
FORMAT_DATE("%Y/%m/%d", DATE("2020-09-17T00:00:00+03:00", "+03:00"))
Parse the date from the previously formatted string
PARSE_DATE("%Y/%m/%d", FORMAT_DATE("%Y/%m/%d", DATE("2020-09-17T00:00:00+03:00", "+03:00")))
Finally extract the ISOWEEK from the date
EXTRACT(ISOWEEK FROM PARSE_DATE("%Y/%m/%d", FORMAT_DATE("%Y/%m/%d", DATE("2020-09-17T00:00:00+03:00", "+03:00"))))
So, this will give you the result you want.
select EXTRACT(ISOWEEK FROM PARSE_DATE("%Y/%m/%d", FORMAT_DATE("%Y/%m/%d", DATE(date))))
You can also skip the "%Y/%m/%d" if it's not really helping you with something else and simply do
select EXTRACT(ISOWEEK FROM DATE("2020-09-17T00:00:00+03:00", "+03:00"))
which results to the same thing
May I know could I verify whether the date displayed in the field is in specific format
As per my requirement, the datetime should be displayed in format 'yyyy/mm/dd HH(24hr)/MM/SS'
Eg: The valid value should be '2014/07/18 14:16:48'. If the date is displayed as '18/07/2014 14:16:48', then it is invalid.
Using query how I verify whether it is shown in the format which I have expected. I could use IsDate option to verify it is a valid date and also I could use Mid function to verify the date separator which is '/', but how could I verify the format.
Thanks
If the column is stored as Text, use the SQL Like operator. Select valid dates:
SELECT * FROM myTable WHERE myDate Like "####/##/## ##:##:##"
Select invalid dates
SELECT * FROM myTable WHERE myDate Not Like "####/##/## ##:##:##"
# stands for a single digit.
See Like Operator.
But note that this only makes sense if myDate is a Text! If the type of myDate is Date/Time, the date is stored in a numeric format internally and is only formatted as a date for display. So don't confuse the date value per se and the date as it is displayed.
A Date/Time is always stored in the same way, no matter how it is formatted and displayed!
All you need to do is to open the Table in Design View and to set the Format property.
I have 2 fields in my DB2 database: DATE and TIME. DATE is string (8/25/2013), DATE is timestamp (1/1/1970 7:00:00 AM). I want to write a sql to replace 1/1/1970 with 8/25/2103. Please help.
Thanks.
Well, you need to cast your string to a date, using the DATE function
DATE(DATE)
Where you see that's it's a bad idea to name your columns with existing function names...
Then combining the TIMESTAMP, DATE, and TIME function (again really unclear with your field names, but...)
TIMESTAMP(DATE(DATE), TIME(TIME))
Will give you a timestamp where the date part is coming from the DATE field, and the time part from the TIME field
See this and that
After casting to a DATE format, adding the difference between those two absolute dates should work -
UPDATE <table-name>
SET DATE= DATEADD(day,DATEDIFF(day,'1/1/1970','8/25/2013'),DATE)
WHERE DATE < '1/2/1970' AND DATE>='1/1/1970'
More on DATEADD and DATEDIFF
Also, it is recommended to not use 'DATE' as a column name as it is a reserved [edit] function name.
I have two columns. ColA and ColB contains char(10) with data "20090520" and "20090521".
I want to select and get the date difference in days. I have tried using Format() and CDate()
but MS Access always display as #ERROR.
Access prefers its dates in this format:
#2009-12-01#
You can convert your date to something Access understands with:
CDate(Format([ColA], "0000-00-00"))
Or alternatively:
DateSerial(Left([ColA],4),Mid([ColA],5,2),Right([ColA],2))
And to display the result in your preferred format:
Format(<date here>, "dd-mm-yyyy")
Try using DateSerial() to convert the dates:
DateSerial(Left([FieldName],4),Mid([FieldName],5,2),Right([FieldName],2))
If at all possible, change the datatype to a date datatype. You should not store dates as character data.
I am connecting to another database which I have no control on. That is why this problem occurred. Thanks for the feedback.