I have two column in sql
year month
2016 4
2014 5
What I want to do now is to combine these two columns together and get the period
Output
year month result
2016 4 201604
2014 5 201405
Are there ways to do this?
If the column data types are integer, do
select year * 100 + month from tablename
First, you need to CAST them to VARCHAR to allow concatenation. Then use RIGHT for month to pad the value with 0:
WITH Tbl AS(
SELECT 2016 AS [year], 4 AS [month] UNION ALL
SELECT 2014, 5
)
SELECT *,
CAST([year] AS VARCHAR(4)) + RIGHT('0' + CAST([month] AS VARCHAR(2)), 2)
FROM Tbl
Assuming both columns are numeric types:
SELECT year,
month,
CAST(year as char(4)) + RIGHT('00' + CAST(month as varchar(2)), 2) as result
FROM YourTable
Related
I wrote the following query to convert 3 varchar columns into 1 datetime column:
select
MYKEY,
CAST(
CAST(YEAR AS VARCHAR(4)) +
RIGHT('0' + CAST(MONTH AS VARCHAR(2)), 2) +
RIGHT('0' + CAST(DAY AS VARCHAR(2)), 2) as DATETIME) as AD_BORN
from SOURCE
where YEAR IS NOT NULL AND MONTH IS NOT NULL AND DAY IS NOT NULL AND YEAR<>''
It works and gives me the list of expected dates.
Nevertheless, when I try to insert its results into a new table I get an error:
sql server conversion failed when converting date and/or time from
character string
This is my statement:
select
MYKEY,
CAST(
CAST(YEAR AS VARCHAR(4)) +
RIGHT('0' + CAST(MONTH AS VARCHAR(2)), 2) +
RIGHT('0' + CAST(DAY AS VARCHAR(2)), 2) as DATETIME) as AD_BORN
into MY_DATES
from SOURCE
where YEAR IS NOT NULL AND MONTH IS NOT NULL AND DAY IS NOT NULL AND YEAR<>''
MY_DATES does not exist before this query. YEAR, MONT and DAY in SOURCE are varchar (4, 2 and 2 respectively). They may contain null values.
What's wrong with that?
No need to Pad your strings. If 2012+ use try_convert()
Example
Declare #YourTable Table ([Year] varchar(50),[Month] varchar(50),[Day] varchar(50))
Insert Into #YourTable Values
(2018,1,25)
,(2018,5,55) -- Bogus Date
Select *
,AsDate = try_convert(date,[Month]+'/'+[Day]+'/'+[Year])
From #YourTable
Returns
Year Month Day AsDate
2018 1 25 2018-01-25
2018 5 55 NULL -- Bogus Date
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
My query provides the following results:
select year, month, day from table
year month day
---- ----- ---
1999 10 9
2000 11 14
2003 6 4
2007 9 14
2008 9 30
1999 3 8
1999 5 27
1999 10 19
I would like to have my results look like this:
date
19991009
20001114
20030604
20070914
20080930
19990308
19990527
19991019
My answer is more from the point of view of MS sql server since I don't know which rdbms you are using:
select cast(year as varchar(10)) + right('00' +cast(month as varchar(10)),2) + right('00' +cast(day as varchar(10)),2)
from table
this will work
SELECT CAST(year AS varchar) + RIGHT('0' + CAST(month AS varchar),2) + RIGHT('0'+ CAST(day AS varchar),2)
FROM Table
If you data is already string you can use
SELECT year +
RIGHT('00'+ month, 2) +
RIGHT('00'+ day, 2)
For integer something like this
SELECT
CAST(year AS VARCHAR(4))+
RIGHT('00'+ CAST(month AS VARCHAR(2)),2) +
RIGHT('00'+ CAST(day AS VARCHAR(2)),2)
FROM YourTable
For integer this also may work
SELECT
CAST(year AS VARCHAR(4))+
RIGHT(100 + month, 2) +
RIGHT(100 + day, 2)
FROM YourTable
I am having table containing two columns one for month and other for year. Based on these two columns I want all records for that financial year. For eg. if month is 10 and year is 2014 then offcourse its financial year will be FY 01StApr14 - 31stMar15.
How do I write a SQL select query to get all the records which were created between FY 01stApr14-31stMar15 available in that table based on month and year.
M trying below query but getting error messge as 'Incorrect syntax near the keyword 'and'.'
with cte as
(
select id,iono,iomonth,ioyear,
convert(datetime,'01/'+right('00' + convert(varchar(10),iomonth,2),2)
+ '/' +convert(varchar(4),IOyear),103) as FinDate from iodetails where iono=12345
)
select month(findate),* from cte where iono=12345 and
findate between
case
when month(findate)>=4
then convert(datetime,'01/'+'04'+ '/' +convert(varchar(4),IOyear),103) and
convert(datetime,'31/'+ '03' + '/' +convert(varchar(4),IOyear+1),103)
else
convert(datetime,'01/'+'04'+ '/' +convert(varchar(4),IOyear),103) and
convert(datetime,'31/'+ '03' + '/' +convert(varchar(4),IOyear),103)
end
Try this:
SELECT *
FROM tableA
WHERE CAST((CAST(YearCol AS VARCHAR(10)) + (CAST(MonthCol AS VARCHAR(10))) AS INT)
BETWEEN CAST((CASE WHEN #V_Month BETWEEN 4 AND 12 THEN CAST((CAST(#V_Year AS VARCHAR(10)) + '04') AS INT)
WHEN #V_Month BETWEEN 1 AND 3 THEN CAST((CAST(#V_Year - 1 AS VARCHAR(10)) + '04') AS INT)
ELSE ''
END
) AS INT)
AND CAST((CASE WHEN #V_Month BETWEEN 4 AND 12 THEN CAST((CAST(#V_Year + 1 AS VARCHAR(10)) + '03') AS INT)
WHEN #V_Month BETWEEN 1 AND 3 THEN CAST((CAST(#V_Year AS VARCHAR(10)) + '03') AS INT)
ELSE ''
END
) AS INT);
If I understand your requirements correctly, you might try this:
SELECT * FROM mytable
WHERE year(dateadd(q, -1, mydate)) = 2014
This will subtract 1 quarter (3 months) from the value of mydate and extract the year. So 31 Mar 2015 becomes 31 Dec 2014 after the subtraction, which when the year is extracted is 2014.
Oops my apologies, I see that you have two columns, month and year. In that case you might try this:
SELECT * FROM mytable
WHERE year(dateadd(q, -1, cast(cast(mymonth AS char) + '/01/' + cast(myyear AS char) AS datetime))) = 2014
That is probably not the most efficient way of doing it however.
UPDATE per my comments
Here is how you might created a computed column on your table:
ALTER TABLE mytable
ADD mydate AS cast(cast(mymonth AS char) + '/01/' + cast(myyear AS char) AS datetime)
my source table column is ReportingPeriod Nvarchar(6)
delete query has to be for datas before 2 years.
this query does not work
delete from table where ReportingPeriod < year(getdate() -2) and month as 12
I need to get 201212 as result
the criteria is current year -2 and month is 12
in 2014 expected result is 201212
in 2015 expected result is 201312
I would do all of the date maths first keeping things as datetime and then convert to nvarchar at the end. Handily, CONVERT will just truncate the result if not enough space is provided, so we just provide space for the year and month and we get what we want:
select CONVERT(nvarchar(6),
DATEADD(year,DATEDIFF(year,'20010101',CURRENT_TIMESTAMP)
,'19991201')
,112)
Result:
201212
This works because we exploit the relationship between two arbitrary dates - for instance, for any date in 2001, the result we would want would be a date in december 1999 - so that's what I've used.
It's true that the above may look overly complex for this requirement, but it's possible to make a great many problems fit the DATEADD/DATEDIFF pattern with the appropriate choice of the period to use and the two arbitrary dates and the relationship between them.
You could try something like
SELECT FROM <YOUR_TABLE>
WHERE ReportingPeriod < Convert(nvarchar(4), year(getdate()) - 2) + '12'
You could cast your ReportingPeriod to a date, so 201212 would become 2012-12-01 as I assume you aren't concerned with the day of the month.
cast(left(ReportingPeriod,4) + '-'
+ RIGHT(ReportingPeriod, 2) + '-01' as datetime)
Then take the current year -2 from GETDATE() and append the month 12 and day 31 to compare these values against, which would return 2012-12-31.
cast(cast(
cast(year(getdate()) -2 as nvarchar(4))
+ '-12-31' as nvarchar(10)) as datetime)
So combining these you should be able to select all records older than 2 years, based on the month being set to 12.
select cast(left(ReportingPeriod,4) + '-'
+ RIGHT(ReportingPeriod, 2) + '-01' as datetime) ReportPeriodDate
from [YourTable]
where cast(left(ReportingPeriod,4) + '-'
+ RIGHT(ReportingPeriod, 2) + '-01' as datetime)
<= cast(cast(
cast(year(getdate()) -2 as nvarchar(4))
+ '-12-31' as nvarchar(10)) as datetime)
Then you just need to delete with this logic.
delete [YourTable]
where cast(left(ReportingPeriod,4) + '-'
+ RIGHT(ReportingPeriod, 2) + '-01' as datetime)
<= cast(cast(
cast(year(getdate()) -2 as nvarchar(4))
+ '-12-31' as nvarchar(10)) as datetime)
Demo SQL Fiddle