Converting a Date Format to YYYYMM in Cognos Report Studio - sql

I have a date column in a format 12 May, 2014 I want to convert that into yyyymm/ 201405 format.
i tried multiple options like
extract(year, sys_date)*100 + extract(month, sys_date))*100
cast(extract( year, sys_date), varchar(4)) + cast(extract( month, sys_date), varchar(2))
This one works but returns me in YYY,YMM format.
CAST(to_char(sys_date, 'YYYYMM'), INT )
For the above two, it gives an error:
An error occurred while performing operation 'sqlOpenResult' status='-28'
Can some one please guide. Thank you in advance.

I got what I was looking for. This is what I did:
translate(CAST(to_char(sys_date, 'YYYYMM'), varchar(6) ), ',' , ' ')
I am sure there would be a better way to get the result but for now I would use this SQL as it gives me what I want :)

I did it so.
substring(cast(substring(cast([sys_date];char(7));1;4)||substring(cast([sys_date];char(7));6;2);char(6));1;6 )
to me worked

Related

How to return only the Date with formate from a SQL Server DateTime datatype

I know there are many questions related to this. I try to find my answer but can't able to find it.
when I write SELECT CONVERT(date, getdate()) from tblBlogs
this query its give me result like this 2018-09-10
but I want to 10, September, 2018
I hope someone helps me
If your SQL-server version higher than 2012 you can try to use FORMAT function to make it.
SELECT FORMAT(getdate(),'dd,MMMM,yyy')
sqlifddle
Or you can use DATENAME function get the name to be your expect result.
SELECT DATENAME(day,Getdate())+','+DATENAME(month,Getdate())+','+DATENAME(year,Getdate())
sqlfiddle
Result
10,September,2018
try using FORMAT
you can specify your date format whatever you want
SELECT FORMAT(getdate(),'dd,MMMM,yyy') from tblBlogs;
Personally I try to avoid FORMAT it can be awfully slow when applied against a dataset. Another method would be to use DATENAME:
DATE NAME(DAY,GET DATE()) +', ' + DATENAME(MONTH,GETDATE()) + ', ' + DATENAME(YEAR,GETDATE())
However, if it for the display format, often it's best to do that in your presentation layer, rather than the DBMS.
If your SQL Server does not support FORMAT function
SELECT CAST(DAY(#DateTime) AS VARCHAR) + ', ' + DATENAME(MONTH, #DateTime) + ', ' + CAST(YEAR(#DateTime) AS VARCHAR)

How to get year of character hijri date SQL Server

I searched and tried many examples unable to solve my hijri date is like,
19/07/1440
I tried this query
SELECT TOP 200
DATEPART(YEAR, EndDateHejri)
FROM
student
but I'm getting this error
Conversion failed when converting date and/or time from character string
I'm unable to solve error - hoping for your suggestions
I bit of Google-Fu and format 131 should help you convert Hijri dates into Gregorian Dates...
DECLARE #hijri DATETIME = CONVERT(datetime, ' 7/05/1421 12:14:35:727PM', 131)
SELECT #hijri
Unfortunately, all the date functions (DATEPART(), DATENAME(), even DATEADD(), etc) are all based on manipulating Gregorian dates. So you can't use them.
So, you're forced to use string manipulation.
DECLARE #hijri DATETIME = CONVERT(datetime, ' 7/05/1421 12:14:35:727PM', 131)
SELECT #hijri
SELECT DATEPART(year, #hijri)
-- Gives 2000 :(
SELECT RIGHT(CONVERT(VARCHAR(10), #hijri, 131), 4)
-- Gives 1421 :)
https://dbfiddle.uk/?rdbms=sqlserver_2017&fiddle=901209ae0cdcf38cdcdea8afad4fd034
Posting a different answer. As the OP is only after the year part, and they've stated that it's always in the format 00/00/yyyy why not just use RIGHT? So:
SELECT RIGHT(EndDateHejri,4) as HejriYear;
I tried answer #Vishnu Chandel it's working for me .
SELECT DATEPART(YEAR,CONVERT(datetime2(0),convert(VARCHAR,EndDateHejri),103))
And full code is :
SELECT TOP 200
SELECT DATEPART(YEAR,CONVERT(datetime2(0),convert(VARCHAR,EndDateHejri),103)) as year
FROM
student
Please try below code to get the correct output.
SELECT DATEPART(YEAR,CONVERT(datetime2(0),convert(VARCHAR,EndDateHejri),103));

Hardcode a specific day in data time while pulling the data - SQL

Actually I have different date in SQL table when I pull those via SQL query, day of datetime field should have fixed day.
Example: (DD-MM-YYYY) day should be "7" > (7-MM-YYYY)
10-08-2007 > 07-08-2007
27-12-2013 > 07-12-2013
01-03-2017 > 07-03-2017
Can someone help me on this. Thanks in Advance.
Find the difference between 7 and the day of the original date and add that to the original date:
SELECT DATEADD(DAY, 7 - DAY(OriginalDate), OriginalDate)
Use DATEPART to take out the month and year parts. Cast those into varchar and concatenate with 07.
Query
select '07-' +
cast(DATEPART(mm, [date_column]) as varchar(2)) + '-' +
cast(DATEPART(yyyy, [date_column]) as varchar(4))
from your_table_name;
Assuming You might have to change the day number example
DECLARE #dayNum char(2)
SELECT #dayNum = '07'
select #dayNum + Right(convert(char(10),getdate(),105),8)
If that is not the case You could do this
select '07'+ Right(convert(char(10),'10-08-2007',105),8)
I'd go this way:
SELECT CONVERT(DATE,CONVERT(VARCHAR(6),GETDATE(),112)+'25',112);
CONVERT with format 112 will return the date as unseparated ISO (today we would get 20170407). Convert this to VARCHAR(6) will implicitly cut away the day's part (201704).
Now we add a day and use again CONVERT with 112, but now with DATE as target type.
One thing to keep in mind: The day you add must be two-digit. You can achieve this with
DECLARE #int INT=7;
SELECT REPLACE(STR(#int,2),' ','0');
Use DATEFROMPARTS: Updated ONLY works from 2012 - OP has tagged SQL-Server 2008
select DATEFROMPARTS ( year('10-08-2007'), month('10-08-2007'), 7 )
Assuming that your field is of datetime datatype and your fixed day is of integer type.
select datetimecolumn+(yourparamfixedday-datepart(dd,datetimecolumn))

Date Conversion in SQL

I have a date in following format in my DB.
10/16 - mm/yy
I need to convert it to:
October/16
Is this possible?
If it's not possible then please tell me why.
This is not a date, it's missing the day, it's a bad way to store year/month. There should be a 4 digit year to avoid confusion and the year should be listed first to enable correct sorting, e.g. '2016/10' or a numeric value 201610.
You can cast it to a DATE first and then use a FORMAT to disply only month/year:
set dateformat myd;
select format(cast(mystupidcolumn + '/1' as date), 'MMMM/yy')
Or SUBSTR the month part and use a CASE.
try this format,
SELECT DATENAME(month, DATEADD(month, #mydate-1, CAST('2008-01-01' AS datetime)))
You can display date by using this code
select datename(month, YourColumnName) + '/' + right(YEAR(YourColumnName),2)
FROM yourTableName
Simply change yourColumnName with name of your table column and yourTableName with name of table.
Yes you can, and it depend in what database you use to call date functions
If you column Datetime format
SQL server DATENAME(Month, GETDATE())
MySQL database MONTHNAME(now())
otherwise
convert it will in your choice at database or you code logic
split the value and lookup at month enum or fake the date to be accepted and complete date format like 01/10/16
so do something like SELECT DATENAME(Month, datecolumn) + '/' + YEAR (datecolumn)
also you can use instead of Year function DATEPART(yy,datecolumn)
the way you do it with format will look like
CONVERT(VARCHAR(11),GETDATE(),106)
but excepted to get first 3 char of month JUN

Cast Function to display only the month and day from a datetime

SELECT
DateAdded,
Cast(DateAdded AS Date) AS DateAddedV1,
Cast(DateAdded AS Time) AS DateTime,
SELECT CAST(DateAdded AS Date(mm:dd) AS OrderDate
FROM Products
I am a beginner with SQL and I am trying to return the DateAdded column as OrderDate with only the month and the day but I cannot seem to get the syntax right. I appreciate anyone's assistance.
Use DatePart Function to extract day and month from Datetime type,
Select Datepart(Month,DateAdded) AS [Month], -- Month(Dateadded)
Datepart(Day,DateAdded) as [Day], -- Day(Dateadded)
..
From Products
Update: Only by using cast function you cannot extract Month and Day. If you want to keep month and day in same column
SELECT CONVERT(VARCHAR(2), Month(DateAdded)) + ':'
+ CONVERT(VARCHAR(2), Day(DateAdded))
.....
To get leading zero's use right function and to extract Time from DateAdded use Convert function with 108 value
SELECT right('0'+CONVERT(VARCHAR(2), Month(DateAdded)),2) + ':'
+ right('0'+CONVERT(VARCHAR(2), Day(DateAdded)) ,2) as [Mon:Day],
convert(varchar(10),DateAdded,108) as [Time]
.........
I think I'm working on this assignment this semester. This is what I ended up with since it HAD to be done using the CAST function:
SELECT DateAdded,
CAST(DateAdded AS date) AS DateOnly,
CAST(DateAdded AS time) AS TimeOnly,
CAST(DateAdded AS varchar(6)) MonthDay
FROM Products;
This works because varchar always has the Month the Day exactly in the first 6 characters. All the Months are 3 Characters (Jan, Feb, Mar, Apr) and then there is either a space then a 2-digit day, or 2 spaces and a 1-digit day. Either way it always adds up to 6 characters.
Here you are asking about the visible format of date/time information.
In MS SQL Server from SQ 2012 and later you can use a FORMAT()
function. If you use a version that supports this function I recommend you use it. Keep in mind that the second parameter is CASE SENSITIVE
In all versions you may use the CONVERT() function, which has
this syntax:
CONVERT ( data_type [ ( length ) ] , expression [ , style ] )
Note in paticular that style parameter (an integer) this is important if using CONVERT. A very useful reference for using either approach for display of date/time information is HERE and below is a tiny example:
SQL Fiddle demo
Query 1:
SELECT
FORMAT(SYSDATETIME(), 'MMM dd yyyy')
, CONVERT(VARCHAR(11), SYSDATETIME(), 100) -- note length set to (11)
Results:
|-------------|-------------|
| Oct 11 2015 | Oct 11 2015 |
Instead of converting, as in NoDisPlayName's answer, you could use the RTRIM functionality.
SELECT RIGHT('0' + RTRIM(MONTH(DateAdded)), 2)