Combining date , month , year columns in SQL Server - sql

DD(10)-MM(05)-YYYY(2013)
I have a table with DATE, MONTH, YEAR in separate columns. How I can combine them into a single column Created date?
Output must be: 10-05-2013 (DD-MM-YYYY format)

SELECT RIGHT('00' + DAY, 2) + '-' + RIGHT('00' + MONTH, 2) + '-' + YEAR AS Date
FROM YourTable

I guess you want have a date field instead of a string. So use TO_DATE() function. You can format date anyway you want later.
TO_DATE(YEAR + '/' + MONTH + '/' + DATE, 'yyyy/mm/dd')

You can do it easily:
SELECT to_char(TO_DATE('10 01 2014', 'DD MM YYYY'),'dd-mm-yyyy') AS MYDATE FROM DUAL

To concatenate columns you just need to use "+" in your select statement
SELECT DATE + '-'+ MONTH + '-'+ YEAR FROM table

Related

How can I convert an integer like 3212007 to date 3-21-2007 uisng IBM DB2 SQL?

The dataset I inherited has a DATE column but the values in this column are integers of the form 3212007 which should be 03-21-2007. I can't get it back into date format.
I can convert the integer to a string using CAST(myinteger as varchar(8)) without difficulty. Then I can CAST that as date by CAST(CAST(myinteger as varchar(8)) as date) which gets me a date. The problem is that my integer is formatted as 'mmddyyyy' so for 3212007, I get 3212-01-07.
select TRANSACTION_DATE from MA_NORFOLK fetch first row only;
[returns: 3212007]
select CAST(TRANSACTION_DATE as VARCHAR) from MA_NORFOLK fetch first row only;
[returns: 3212007]
select CAST(CAST(TRANSACTION_DATE as varchar(8)) as date) from MA_NORFOLK fetch first row only;
[returns: 3212-01-07]
Other posts suggest using CONVERT command, but all I get are errors
"DATE" is not valid in the context where it is used..."
Could you please advise me?
Try this:
date(to_date(digits(dec(3212007, 8)), 'MMDDYYYY'))
If you may have one digit for month, there is an alternative:
select
date
(
case when substr(char_dt, 1, 2)='00'
then translate('EFGH-0D-0C', char_dt, 'ABCDEFGH')
else translate('EFGH-AB-CD', char_dt, 'ABCDEFGH')
end
) dt, char_dt
from
(
select digits(dec(i, 8)) char_dt
from table(values 3212007, 312007) t(i)
) t;
DT CHAR_DT
---------- --------
2007-03-21 03212007
2007-01-03 00312007
This works (at least in MS SQL Server):
SELECT CAST(CONCAT( RIGHT(3212007,4),'-',
(3212007 / 1000000), '-', ((3212007 % 1000000) / 10000)) AS date)
The trick is that you don't know if you will have a single digit, or two-digit month, so you have to start with getting just the right 4 characters as the year. Then by using combinations of modulo and integer divide, you can parse out the month and day.
Of course, you will want to substitute your actual date column for the sample data that I used above.
I think this may works well:
create table #temp(
date int
)
insert into #temp (date)
values(3212007),
(12032019)
select
case when len(cast(date as varchar)) = 7
then
'0' + left(cast(date as varchar), 1) + '-' + substring(cast(date as varchar), 2,2) + '-' + right(cast(date as varchar), 4)
else left(cast(date as varchar), 2) + '-' + substring(cast(date as varchar), 3,2) + '-' + right(cast(date as varchar), 4) end
from #temp

Construct a date from parts in SQL Server 2008

I am using SQL Server 2008 and trying to form a query that will use a CASEstatement with a date calculation to return one of two dates depending on what day of the month the calculation results in.
The goal is if the calculation (in query below) CURRENT_TIMESTAMP + LEAD_TIME results in a day between the 1st and 15th, then assign the 15th of that month. Otherwise, return the last day of that month.
For example, if CURRENT_TIMESTAMP + LEAD_TIME = 07/12/19, return 07/15/19. If that calculation = 07/16/19, return 07/31/19.
This seems like it would be possible using DATEFROMPARTS but I believe since I am using SQL Server 2008 that function is not defined (that is the error I am returning). Any ideas on a work around?
SQL:
SELECT I.po_number,
I.po_item_number AS 'po_item',
S.orderentry_date,
I.po_req_ship_date,
I.ex_factory_date,
I.del_indicator,
H.po_type,
H.vendor_no,
CASE WHEN DATEPART(dd,(CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME)) BETWEEN 1 AND 15
THEN DATEFROMPARTS(DATEPART(yyyy,(CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME),mm,(CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME),15)
ELSE DATEFROMPARTS(DATEPART(yyyy,(CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME),mm,(CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME)+1,0))
END AS 'LT_CALC',
H.po_created_by,
I.comment
FROM rbk_sap_user..vw_po_header H
JOIN rbk_sap_user..vw_po_item I ON H.po_number = I.po_number
JOIN rbk_sap_user..vw_mm_material MM ON I.material = MM.material
JOIN (SELECT order_no,
orderentry_date
FROM asagdwpdx_prod..SimoxOrder1
UNION ALL
SELECT order_no,
orderentry_date
FROM asagdwpdx_prod..SimoxOrder2
UNION ALL
SELECT order_no,
orderentry_date
FROM asagdwpdx_prod..SimoxOrder3
) S ON S.order_no = H.ahag_number
WHERE S.orderentry_date BETWEEN '01/31/2019' AND '02/13/2019'
AND I.del_indicator <> 'L'
AND H.po_type NOT IN ('02','06','10','UB')
AND MM.business_segment_code NOT IN ('420','421','422','424')
You can convert all the parts of the date to VARCHAR and string them together to yyyy-mm-dd format, and then CONVERT toDATETIME.
SELECT
...
CASE WHEN DATEPART(dd,(CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME)) BETWEEN 1 AND 15
THEN CONVERT(DATETIME, CONVERT(varchar, DATEPART(yyyy,(CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME)))+ '-' + CONVERT(VARCHAR, DATEPART (mm,(CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME))) + '-' + CONVERT(VARCHAR, 15))
ELSE DATEADD(DAY, -1, CONVERT(DATETIME, CONVERT(VARCHAR, DATEPART(yyyy,(CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME)))+ '-' + CONVERT(VARCHAR, DATEPART(mm,CURRENT_TIMESTAMP + MM.IAM_MAN_LEAD_TIME)+ 1) + '-' + CONVERT(VARCHAR, 1)))
END AS 'LT_CALC'
...
Note that in the ELSE statement, the logic to get the last day of the month is to go the first day of the following month, and then use the DATEADD function to subtract one day. The SQL Server function EOMONTH , introduced in SQL Server 2012, eliminates the need for that logic.
Try this
SELECT CAST(LEFT(CONVERT(varchar, CURRENT_TIMESTAMP + LEAD_TIME, 103 ),2) as int)
then
CASE WHEN CAST(LEFT(CONVERT(varchar, CURRENT_TIMESTAMP + LEAD_TIME, 103 ),2) as int) < 15 THEN
....
ELSE
....
END

merge two column with date format

I need merge two column to date format
input
month year
---- ----
7 2013
The result should be come date format like this (DD/MM/YYYY) :
New_Date
--------
01/07/2013
You can use for example this:
select convert(date, '01/' + convert(varchar(2), month) + '/' + convert(varchar(4), year), 101)
The output of your data is depended on the collation, not how you store it.
To get the date, try this:
SELECT DATEFROMPARTS(year, month, 1);
Check This: Suppose you have variable declaration as below and you can get the desired output in desired form by using CONVERT
DECLARE #year INT = '2017', #month INT = '7'
SELECT CONVERT(VARCHAR(10),DATEFROMPARTS(#year, #month, 1), 103) AS [DD/MM/YYYY]
OUTPUT:
DD/MM/YYYY
01/07/2017
You have so many date format which you can use to get output in various format please check this http://www.sql-server-helper.com/sql-server-2008/sql-server-2008-date-format.aspx
Try below code :
SELECT RIGHT('00'+CAST('1' AS VARCHAR(2)),2) + '/' + RIGHT('00'+CAST('7' AS
VARCHAR(2)),2) + '/' + CAST('2013' AS VARCHAR(4))

Convert string to Datetime

I've received a flat file and after parsing and inserting it in a table. I've a column which has dates in a format yyyyMMddhhmmss
Here, yyyy is year, MM is month, dd is day, hh is hours, mm is minute and ss is seconds part
I'm trying to convert this to DateTime type as mentioned below but not working for me
SELECT CAST(StrDate as DateTime) FROM [dbo].[Mytable]
For example:
Column has a value 20150121190941 in Varchar format and it should be converted to DateTime as 2015-01-21 19:09:41.000
I apologize if it's a duplicate one.
You can use select DATETIMEFROMPARTS ( year, month, day, hour, minute, seconds, milliseconds )
declare #dt nvarchar(25) = '20150121190941'
select datetimefromparts(left(#dt,4), substring(#dt,5,2), substring(#dt,7,2),substring(#dt,9,2), substring(#dt,11,2), substring(#dt,13,2), '000')
SQL Server readily recognizes YYYYMMDD as a date. So, converting the first 8 characters to a date is easy. Alas, I don't think there is a time representation without colons.
So, here is one rather brutal method:
select (convert(datetime, left(StrDate, 8)) +
convert(time, substring(StrDate, 9, 2 ) + ':' + substring(StrDate, 11, 2 ) + ':' + substring(StrDate, 13, 2 )
)
)
Declare #dt varchar(50)
set #dt=20150121190941
create table tblDateTbl
(
col1 datetime
)
insert into dt (col1)
select SUBSTRING(#dt,1,4) + '-' + SUBSTRING(#dt,5,2) + '-' + SUBSTRING(#dt,7,2)+ ' ' + SUBSTRING(#dt,9,2)+ ':' + SUBSTRING(#dt,11,2)+ ':' + SUBSTRING(#dt,13,2)+ '.000'

Get data on date ranges

Table Structure
Id No Date Time
0001, 01-05-2009, 040000
0001, 02-05-2009, 020000
0002, 01-05-2009, 060000
0002, 01-05-2009, 180000
Time and Date is nvarchar
I want to get the data between:
Yesterday 030001 to today 030000
Day before yesterday 0300001 to yesterday 030000
I tried the below mentioned query
Select
idno, min (time), max (time)
from
table
where
time between 030001 and 030000
Nothing displayed in the result because it is taking today time from 03.00 am to 03.01am
Exactly I need today 03.00 am to yesterday 03.01 am
I need the sql query for the above condition
Can anyone help me?
It's hard to sort or filter when your date is in a format like DD-MM-YYYY. In such a format, 01-01-2009 comes before 02-01-2000 !
So, first convert your date with the long years first, and the short seconds last:
YYYY-MM-DD HH:MM:SS
A query like this can do that:
select [id no],
substring(date,1,2) + '-' + substring(date,4,2) + '-' +
substring(date,7,4) + ' ' +
substring(time,1,2) + ':' + substring(time,3,2) + ':'
substring(time,5,2) as NormalDate
from YourTable
Now you can easily select all rows for a particular timespan:
select *
from (
select [id no],
substring(date,1,5) + '-' + substring(date,7,4) + ' ' +
substring(time,1,2) + ':' + substring(time,3,2) + ':'
substring(time,5,2) as NormalDate
from YourTable
) sub
where '2009-05-01 03:00:00' < NormalDate
and NormalDate < '2009-05-02 03:00:00'