Convert varchar into date without day - sql

Goal:
Gain datatype date with year and month
Problem:
Need to help to convert varchar value into datatype date
Unfortunatley, no day is included in the data from column. Year and month only.
I tried converting into date without using day but I failed.
Varchar(10) column data
data
-------
1997-05
1984-12
1988-11
1984-10
1984-02
1984-01
1984-04

Assuming returning the first day of the given month is ok, you can use:
SELECT CONVERT(DATE, data + '-01') FROM dbo.table;
As others have suggested, you can't have a date without a day.

If you don't care about the day, you can add an arbitrary day to the data:
CAST(data + '-01' as date)

Related

Changing date to day before for certain daily time ranges in SQL

We have a table with a datetime field and need to get it to work properly with other tables that have date fields that work on system date, not calendar date.
Our system day runs until 1:59 AM, so anything after midnight until then is considered the day before in all of these tables (which don't actually show datetime, just date).
I am trying to figure out how I can create a date field where '2019-01-01 01:45:00.111' would read as '2018-12-31', but '2019-01-01 2:00:00.111' would read as '2019-01-01'.
Any ideas would be much appreciated.
Try this simple logic below by removing 120 minutes from your each datetime value and then cast the datetime value as date.
SELECT CAST(DATEADD(MINUTE,-120,<your_date_time_column>) AS DATE)
FROM <Your_Table>
You can make use of CASE WHEN.. clause to increment or decrement dates by 1 like below sample
SELECT CASE WHEN TO_CHAR(DATE,
'HH:MM') BETWEEN '00:00' AND
'01:45' THEN DATE-1 ELSE
DATE END FROM TABLE

convert int YYYYMMDD to date AS400

I need to convert an integer to a date format in AS400.
I have the field called ivdat8, which is integer in the formatted YYYYMMDD.
I need to use a WHERE clause to select data between two dates, today and 3 days ago.
I am using the below line to do this:
Where
ivdat8 Between (Select current date - 3 days from sysibm.sysdummy1) And (Select current date from sysibm.sysdummy1)
The current date from sysibm is a true date format, but ivdat is integer.
How can i cast ivdat8 to be a date format i can use within the WHERE clause ?
I have tried the below to convert the int to date:
cast(cast(A.ivdat8 as varchar(50)) as date)
&
TIMESTAMP_FORMAT(ivdat8,'YYYYMMDD')
Actually it's better for performance not to convert the ivdat8 column data, but do this with parameters like below.
select ivdat8
from table (values
int(to_char(current date - 2 days, 'YYYYMMDD'))
, int(to_char(current date - 5 days, 'YYYYMMDD'))
) t (ivdat8)
where ivdat8 between
int(to_char(current date - 3 days, 'YYYYMMDD'))
and int(to_char(current date, 'YYYYMMDD'));
Easiest way to do it without causing a complicated conversion in the query is to use this:
cast(digits(cast(A.ivdat8 as dec(8))) || '000000' as date)
The full where clause doesn't need to select from sysibm.dummy1 either.
where cast(digits(cast(A.ivdat8 as dec(8))) || '000000' as date) between current date - 3 days and current date
If you have indexes built on ivdat8 though, the fastest selection will be:
where A.ivdat8 between cast(Current date - 3 days as dec(8)) and cast(Current Date as dec(8))
Managed to convert the int to a date, first i had to cast it as a string, then use TO_DATE
To_Date(cast(A.ivdat8 as varchar(50)),"YYYYMMDD")
try this
Where cast(TIMESTAMP_FORMAT(cast(ivdat8 as varchar(8)), 'YYYYMMDD') as date)
Between (current date - 3 days) and current date

Where datetime between 12 am of column time and 10 am next day of column time

I have a column with data type as datetime and the way data is imported is always the previous day to today.
There is not historical data stored in the table - the previous data is deleted before the new data is imported.
That being said I will like to have a where clause written where my ESTEndTime column is between 00:00:00 of the timestamp column and 10:00:00 the next day of the timestamp column.
I've searched but couldn't find anything specific to what I was looking for.
The purpose is to primarily capture the end times of those who work overnight. When the data is exported it always exported as the previous day to the next day, for example October 1 to October 2. This will allow us to see the latest logout times of those who worked overnight. In the example that I just mentioned the data will include everything from 00:00:00 October 1 to 23:59:59 October 2. There is no way for us to limit to a specific time. So using the example when I was hoping to achieve is limit it in a where clause where the time is between 00:00:00 October 1 and 10:00:00 October 2. The problem is I don't want to use those actual dates as my dates are always changing hence why I am here for some help.
Code that I have but not getting the expected results
SELECT timestamp, emp_id, dept, ESTStartTime, ESTEndTime
,CONCAT(emp_id, '-', FORMAT(ESTStartTime, 'yyyy/MM/dd-HH:mm')) AS Index2Start
,CONCAT(emp_id, '-', FORMAT(ESTEndTime, 'yyyy/MM/dd-HH:mm')) AS Index3Stop
,CONCAT(emp_id, '-', FORMAT(ESTEndTime, 'yyyy/MM/dd')) AS Index3StopDay
FROM dbo.[test]
WHERE ESTEndTime BETWEEN DATEADD(DAY, DATEDIFF(DAY, 0, timestamp), 0) + '00:00'
AND DATEADD(DAY, DATEDIFF(DAY, 1, timestamp), 1) + '10:00'
ORDER BY ESTEndTime DESC
Here is another possible way you can try
SELECT DISTINCT ee.*
FROM table ee
CROSS APPLY (
select MIN(timestamp) AS timestamp, NextDate = DATEADD(HOUR,10,DATEADD(DAY,1,MIN(timestamp))) from table
) maxdate
WHERE ee.ESTEndTime BETWEEN maxdate.timestamp AND maxdate.NextDate
See if this will work for you. I declared 2 variables to hold the previous day's date and time and the current day's date and time. I used GETDATE() to get the current date and time. I extracted the date part out of it and subtracted a day for the previous date. Then I converted the date to a VARCHAR and added the time. SQL Server will implicitly convert the string back to a DATETIME value.
--Previous day at 12:00am
DECLARE #prevday DATETIME = CONVERT(VARCHAR,CONVERT(DATE,(DATEADD(DD,-1,GETDATE())))) + ' 12:00AM';
--Current day at 10:00am
DECLARE #currentday DATETIME = CONVERT(VARCHAR,CONVERT(DATE,GETDATE())) + ' 10:00 AM' ;
SELECT timestamp, emp_id, dept, ESTStartTime, ESTEndTime
,CONCAT(emp_id, '-', FORMAT(ESTStartTime, 'yyyy/MM/dd-HH:mm')) AS Index2Start
,CONCAT(emp_id, '-', FORMAT(ESTEndTime, 'yyyy/MM/dd-HH:mm')) AS Index3Stop
,CONCAT(emp_id, '-', FORMAT(ESTEndTime, 'yyyy/MM/dd')) AS Index3StopDay
FROM dbo.[test]
WHERE ESTEndTime BETWEEN #prevday AND #currentday
ORDER BY ESTEndTime DESC

How to insert only date or day or year from date or datetime data type?

How to insert only date or day or year from date or datetime data type?
Conversion failed when converting date and/or time from character string.
You must have properly defined type on column into which you are inserting. If you are trying to insert only the datepart, it should work correctly (with time part being set to zeros or if the column type is Date), but if you only have day or year, be aware that their type is Integer, not DateTime.
The return type will be integer:
SELECT DATEPART(DD,GETDATE()) 'Day', GETDATE()
SELECT DATEPART(MM,GETDATE()) 'Month', GETDATE()
SELECT DATEPART(YY,GETDATE()) 'Year', GETDATE()

How to extract week number in sql

I have a transdate column of varchar2 type which has the following entrees
01/02/2012
01/03/2012
etc.
I converted it in to date format in another column using to_date function. This is the format i got.
01-JAN-2012
03-APR-2012
When I'm trying to extract the weekno, i'm getting all null values.
select to_char(to_date(TRANSDATE), 'w') as weekno from tablename.
null
null
How to get weekno from date in the above format?
After converting your varchar2 date to a true date datatype, then convert back to varchar2 with the desired mask:
to_char(to_date('01/02/2012','MM/DD/YYYY'),'WW')
If you want the week number in a number datatype, you can wrap the statement in to_number():
to_number(to_char(to_date('01/02/2012','MM/DD/YYYY'),'WW'))
However, you have several week number options to consider:
WW Week of year (1-53) where week 1 starts on the first day of the year and continues to the seventh day of the year.
W Week of month (1-5) where week 1 starts on the first day of the month and ends on the seventh.
IW Week of year (1-52 or 1-53) based on the ISO standard.
Try to replace 'w' for 'iw'.
For example:
SELECT to_char(to_date(TRANSDATE, 'dd-mm-yyyy'), 'iw') as weeknumber from YOUR_TABLE;
Select last_name, round (sysdate-hire_date)/7,0) as tuner
from employees
Where department_id = 90
order by last_name;
Use 'dd-mon-yyyy' if you are using the 2nd date format specified in your answer. Ex:
to_date(<column name>,'dd-mon-yyyy')