How can we convert date format to YYMM(ex:1208) for the given date 25/08/2012.
Use convert with style 12 and pick the first four characters.
select convert(char(4), getdate(), 12)
try this:
declare #date date='08/25/2012'
select CONVERT(varchar(4),#date,12)
You did not specify the datatype for the value 25/08/2012.
declare #dt char(10)
set #dt = '25/08/2012'
select right(#dt, 2) + substring(#dt, 4, 2)
If you don't like remembering the string convert formulas, you can also use:
declare #date date=CURRENT_TIMESTAMP
select right(CAST(year(#date) as varchar(4)), 2) + RIGHT('0'+cast(month(#date) as varchar(2)), 2)
It is a bit more cumbersome, but saves a trip to the help pages.
Related
It must be very simple, but I don't know SQL language very well.
I need to filter data by date which is in this format:
How to do it right to filter data this way?
FROM [TableName] where
FileDate>=20220505
I've already tried the command LEFT and CAST but with no success
Something like this may work:
declare #now Datetime = getdate();
declare #intNow int = cast(cast(datepart(year, #now) as varchar(4)) + RIGHT('00'+CAST(datepart(month, #now) AS VARCHAR(2)),2) + RIGHT('00'+CAST(datepart(day, #now) AS VARCHAR(2)),2) as int)
Although if you have your date to check against in the right format e.g. using:
declare #dateToCheck Datetime = cast(cast(20220505 as varchar) as datetime)
And then
FileDate>= #dateToCheck
it should work
You can create an integer representation of your datetime by multiplying and adding the date parts:
year * 10000 20220000
month * 100 500
day 5
-------------------------
20220505
...
FROM [TableName]
WHERE (DATEPART(year, [FileDate]) * 10000) + (DATEPART(month, [FileDate]) * 100) + (DATEPART(day, [FileDate])) >= 20220505
However I'd still look into fixing the condition input format instead.
Credit to #Rangani in Yesterday's date in SSIS package setting in variable through expression for "multiply and add instead of string concat" trick
I am trying to convert INT (YYYYMM) value to DATE.
I have read articles which mention that converts from char to date format. So I am curious to know - which of the foll. is a good approach?
Example:
DECLARE #PERIOD INT ='201806'
SELECT CAST(CONCAT(#Period,'01') AS DATE) SQLCAST1
,CAST(CAST(#Period AS varchar(6))+'01' AS DATE) SQLCAST2
Which is the ideal approach and why? Do we have other better approach?
I am not a fan of implicit data type conversions, so I would phrase the first as:
select convert(date, convert(varchar(6), #PERIOD ) + '01')
Or use datefromparts() which is a built-in function for constructing dates:
select datefromparts( #PERIOD / 100, #PERIOD % 100, 1)
Apparently concat is the most efficient method.
https://raresql.com/2013/03/12/sql-server-string-concatenation-faster-method/
Cast also appears to be the most efficient method for parsing a standard date string (see the try_cast times for DateTime)
SQL - Convert INT to Date
Your first method is most efficient.
Here is another method, First you have to convert to char then convert to date time,
declare #intValue int
select #intValue = 201907
select convert(date, convert(varchar(6), #intValue ) + '01')
The dates are currently displayed as: ddmmmyyyy (12DEC2013)
I've been playing around with this formula:
DECLARE #Date char(8)
set #Date='12312009'
SELECT CONVERT(datetime,RIGHT(#Date,4)+LEFT(#Date,2)+SUBSTRING(#Date,3,2))
but I didn't have any success, can someone help me out with this. Additionally all my dates are in a column called TERMDT and I'd like to put all the new date values in a new column formatted as such.
Just give convert() an appropriate 3rd argument (the format):
SELECT CONVERT(datetime,
RIGHT(d, 4) + LEFT(d,2) + SUBSTRING(d, 3, 2),
112)
from (select '12312009' as d) t
We have the following solution:
select
substring(convert(varchar(20),convert(datetime,getdate())),5,2)
+ ' ' +
left(convert(varchar(20),convert(datetime,getdate())),3)
What is the elegant way of achieving this format?
You can do it this way:
declare #date as date = getdate()
select replace(convert(varchar(6), #date, 6), ' ', '-')
-- returns '11-Apr'
Format 6 is dd mon yy and you take the first 6 characters by converting to varchar(6). You just need to replace space with dash at the end.
You can use the dateName function:
select right(N'0' + dateName(DD, getDate()), 2) + N'-' + dateName(M, getDate())
If you really want the mmm part to only have the tree-letter abbreviation of the month, you're stuck with parsing the appropriate conversion type, for example
select left(convert(nvarchar, getDate(), 7), 3)
The problem is that dateName doesn't have an option to get you the abbreviated month, and the abbreviation isn't always just the first three letters (for example, in czech, two months start with Čer). On the other hand, convert 7 always starts with the abbreviation. Now, even with this, I assume that the abbreviation is always three letters long, so it isn't necessarily 100% reliable (you could search for space instead), but I'm not aware of any better option in MS SQL.
DECLARE #t datetime = getdate()
SELECT CONVERT(VARCHAR(24),LEFT(#t,6),113)
Try this...
SELECT LEFT(CONVERT(NVARCHAR(10), GETDATE(), 6), 6)
Basically I have 3 separate columns in a table. I will call them SMonth, Sday, Syear. They are stored as numeric values for some reason. I can use the following string to format them into what looks like a date but doesn't allow me to use functions such as sort, order by, datediff or dateadd.
CAST(SMonth AS varchar(2)) + '/' + CAST(SDay varchar(2)) + '/' + CAST(SYear AS varchar(4))
Anyone know how to convert this into a workable date, without changing the table?
It doesn't matter how it looks as long as I can use it ie a date or datetime makes no difference.
Thanks in advance.
Just convert your result into a date or datetime.
DECLARE #SMonth AS INT = 12
DECLARE #SDay AS INT = 31
DECLARE #SYear as INT = 2013
SELECT CONVERT(DATE,CAST(#SMonth AS varchar(2)) + '/' + CAST(#SDay AS varchar(2)) + '/' + CAST(#SYear AS varchar(4)))
you should convert format the string as yyyy/mm/dd in order to make sure that SQL Server uses ODBC canonical format
SELECT CONVERT(date, CONVERT(varchar, Syear) + '/' + convert(VARCHAR, SMonth) + '/' + convert(VARCHAR, SDay ) )
Otherwise it your results could depend on the default dateformat or someone changed it using SET DATEFORMAT, for example:
05/10/2013 could mean
May 10, 2013 if the DATEFORMAT is U.S.
October 5, 2013 if the DATEFORMAT is Brithish / French
see this for complete reference of dateformat