I am trying to get the month year from date that is in database.
I used the following query to get the date. But the formatting is not as I require. Due to this my query is not giving the result which I am expecting.
select top 3 datepart(yyyy, cia.DateCreated) as YYyy, datepart(mm, cia.DateCreated) as mm,
case when (datepart(mm, cia.DateCreated) < 10) then
cast(concat('0',datepart(mm, cia.DateCreated)) as varchar)
else
datepart(mm, cia.DateCreated)
end as Months
from v_AuthListInfo cia
I am getting the month values as 1, 2, 3, 4, 5, 6, 7, 8 but I need the value like 01, 02, 03, 04 ...
I tried to use DatePart, Cast and Concatinate function but still I am getting the data like 1, 2, 3, 4.
use right() function
RIGHT('0' + RTRIM(MONTH(cia.DateCreated)), 2);
You can use the built-in function format():
format(month(cia.DateCreated), '00')
If you want to use the right() method, I would suggest:
right(concat('00', month(cia.DateCreated)), 2)
Here's general approach how to pad with ceratin character:
right(#padding + cast(#stringToPad as varchar), 2)
where #padding consists of characters you want to use and its length has to be the desired length, so in your case it would be 00.
#stringToPad is string you want to extend, in your case cast(month(cia.DateCreated) as varchar).
So finally, giving an example, it would look like:
select right('00' + cast(1 as varchar), 2)
Related
I'm using SQL Server 2014, and I'm trying to perform a data conversion, the value being passed '04-SEP-44'
Since this was the birth year, it needs to be 1944, not 2044.
I've tried
select convert(DATETIME, '04-SEP-44')
select convert(DATETIME, '04-SEP-44', 1)
select convert(DATETIME, '04-SEP-44', 13)
But they are all using the year as 2044.
Are there any quick fixes to this issue?
Possibilities:
If the value is greater than today subtract 100 years...
Force "19" into the string...
But it really depends on your business rules, is it always going to be 1900, or could some dates be early 2000's? You should really update your front end to capture the full date.
select case when Y.[Value] > getdate() then dateadd(year, -100, Y.[Value]) else Y.[Value] end
, convert(date, substring(X.[Value], 1, 7) + '19' + substring(X.[Value], 8, 2))
from (values ('04-SEP-44')) as X ([Value])
cross apply (values (convert(date, X.[Value]))) as Y ([Value]);
Note: why would you convert a date to a datetime? Don't use a datetime if you don't have a time component.
I want to get the week number, and I did using DATEPAR(WEEK,DATE) , however, one-digit week numbers do not come with a zero in front.
Basically I need to get 01, 02, 03 instead of 1, 2, 3.
In SQL Server, you can left pad with this logic:
select right('0' + datename(week, date), 2)
With the function FORMAT():
FORMAT(DATEPART(WEEK, your_date), '00')
You need case when length() to do that
Select case when
length( DATEPAR(WEEK,DATE)) <2
then '0'||DATEPAR(WEEK,DATE) else
DATEPAR(WEEK,DATE) end
From table
I have the following string 2015089 or 2016075, for example.
I need to get the result in yyyy/mm/dd format based on the given input.
So, based on 2015089, I get, 2015/mm/dd. dd is a 89th day of 2015 and mm is a month that has 89th day.
How can I do something like that?
I think the simplest way is to convert to a date using dateadd():
select dateadd(day, right(str, 3) - 1, datefromparts(left(str, 4) + 0, 1, 1) )
That is, add one less than the number of days to the beginning of the year. This assumes that Jan 1 is represented as "1" and not "0".
You can then format the date however you like.
In pre-SQL Server 2012, you can do:
select dateadd(day, right(str, 3) - 1, cast(left(str, 4) + '0101' as date))
WITH Valid_dates (ValidDate)
AS
(
SELECT '19' + SUBSTRING(column, 0, 3) + '-' + SUBSTRING(column, 3, 2) + '-' + SUBSTRING(column, 5, 2) AS ValidDate
FROM table
WHERE SUBSTRING(column, 3, 2) <= '12' --Max month
AND SUBSTRING(column, 3, 2) >= '01' --Min month
AND ISNULL(SUBSTRING(column, 3, 2), '') <> '' --Empty string
AND SUBSTRING(column, 5, 2) <= '31' --Max day
AND SUBSTRING(column, 5, 2) >= '01' --Min month
AND ISNULL(SUBSTRING(column, 5, 2), '') <> '' --Empty string
AND LEN('19' + SUBSTRING(column, 0, 3) + '-' + SUBSTRING(column, 3, 2) + '-' + SUBSTRING(column, 5, 2)) = 10 --Must match 10 character format
)
SELECT CONVERT(DATETIME, ValidDate)
FROM Valid_dates
When I execute this query in SQL Server Management Studio, I get the following error message: "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value."
I have filtered out all invalid dates. Non-existent dates such as Feb 30, Apr 31, Jun 31 etc. are not filtered out per se as you can see, but I have checked for them, and they do not exist. Running only the CTE query yields plenty of results with which I can not find any discrepancies.
This is an issue with SQL Server. It does not guarantee the order of evaluation of clauses in the query. There is no sense that the where (even in the CTE) is evaluated "before" the rest of the query.
Note: This is a feature of the optimizer. By rearranging the evaluation of different components of the query, the optimizer can create a better query plan.
In SQL Server 2012+, the simplest method is TRY_CONVERT():
with . . .
select try_convert(datetime, validdate)
from valid_dates;
In earlier versions, you can use case:
select (case when <all your conditions here>
then convert(datetime, validdate)
end)
SQL Server does guarantee the sequential evaluation of case statements under normal circumstances (there are some quirky situations with aggregations), so this also fixes the problem.
I'm pretty sure that this is culture related.
You did not specify the actual input, from your code I suppose it is some unseparated format without a year like 920130 meaning the 30th of January in 1992?
This will be transformed in something like 1992-01-30.
Your default culture is probably one using something like yyyy-dd-mm which will lead into out-of-range error with a "month" higher than 12...
Try to use 102 as culture key:
SELECT CONVERT(DATETIME, ValidDate,102)
FROM Valid_dates
UPDATE
If my assumptions are true, it was much easier to try
SELECT TRY_CAST('19' + column AS DATE) FROM table
SQL Server allows natively to cast unseparated strings looking like yyyymmdd. Using TRY_CAST will return NULL if the cast is not possible.
With older versions (<2012) use this
SELECT CASE WHEN ISDATE('19' + column)=1 THEN CAST('19' + column AS DATE) ELSE NULL END FROM table
I'd like to return a date from SQL in the following format 2011-12-DEC, 2012-01-JAN, 2012-02-FEB, 2012-03-MAR etc...
Currently I have the below code, but don't think it's the best. It also does not return a 0 in front of the month (i.e. 8 rather than 08)
print CONVERT(VARCHAR(20), DATEPART(yyyy,GETDATE())) + '-'+ CONVERT(VARCHAR(20), DATEPART(mm,GETDATE()))
For SQL Server, something like this
select convert(varchar(8), getdate(),120)
+ convert(varchar(3),datename(month,getdate()))
For Oracle use TO_CHAR and date formatting. For Example
SELECT TO_CHAR(SYSDATE, 'YYYY-DD-MON') FROM DUAL
There is a wide range of possible date format strings
See here.
If you really do want yyyy-mm-mmm then you could get it like so (for SQL server):
select substring(convert(nvarchar, getdate(), 120), 1, 7)
+ '-' +
substring(upper(datename(mm, getdate())), 1, 3)
Consider a DATE_FORMAT function or the like, depending on your RDMS.
Well,
you should use a formatter (eg AS [DD/MM/YYYY] ).
All of them are listed here including your requirement for "month" naming
http://www.sql-server-helper.com/tips/date-formats.aspx
SELECT SUBSTRING(CONVERT(VARCHAR(11), GETDATE(), 113), 4, 8) AS [Mon-YYYY]
would give you Apr-2006
just turn it around as you like.