I have been trying to achieve this all day, I have followed numerous tutorials and can't seem to crack it, I have been trying things like:
select CONVERT(VARCHAR(10), DATE, 131) from Table
yet it does not seem to change anything.
Any advice or help would be appreciated. Thankyou in advance.
SELECT CONVERT(VARCHAR(10), DATE, 12)
12 is the right code for the format you want. See: https://msdn.microsoft.com/en-GB/library/ms187928.aspx
Try this
declare #TDATE Date = '2015-11-10';
select Convert(varchar,Datepart(Year, #TDATE))+Convert(varchar,Datepart(Month, #TDATE))+Convert(varchar,Datepart(Day, #TDATE))
Output:
20151110
12 is the right code. Use below query to get output as 'yymmdd'
select CONVERT(VARCHAR(10), DATE, 12) from Table
On PostgreSQL the easiest way is to use to_char:
to_char(date, 'yyyymmdd')::int
One method is to construct the value from date parts. Here is a numeric conversion:
select (year(date) % 100) * 10000) + month(date) * 100 + day(date)
It is easy to convert this to a number:
select cast( (year(date) % 100) * 10000) + month(date) * 100 + day(date) as varchar(10))
The slight advantage to this approach over using convert is that a human being can understand the logic without perusing arcane documentation, specific to SQL Server. I don't know why SQL Server doesn't support a simple format-type function similar to most other databases (and programming languages).
select date_format(column_name, '%Y%m%d') from table_name;
Related
I have date column in my table as string type example Feb-18. How do I change it to date type as 01-02-2018?
Use convert() with add one day :
select convert(date, '01-' + 'Feb-18')
SQL Server is pretty good about figuring out dates. But you need a day, so:
select convert(date, '01-' + datecol)
Note: You should be very careful about storing dates as strings. I would recommend that you test the conversion to be sure it works for all values:
select datecol
from t
where try_convert(date, '01-' + datecol) is null and
datecol is not null;
If this returns any rows, then you have bad dates in your data. Oh, it would have been better to catch these by rejecting the insert/updates in the first place. However, you might be able to figure out how to fix them.
You can also try the following way.
Select try_cast('01-' + 'Feb-18' as Date) as [String To Date]
I'm pretty new to Sybase and am writing a query to return results after a specified date, and also before a specified date. MM/DD/YYYY format
At the moment im doing..
SELECT *
From aTable
WHERE afterDate >= 08/07/2013
AND beforeDate <= 08/08/2013
I'm getting records back, but as I'm a Sybase newbie, I want to be sure Sybase is interpreting these dates correctly..
Their online doc is pretty bad for basic explanations on things like this!
Anyone able to confirm if what I have works, or does it need some formatting round the dates?
You'll need to convert the dates into DATETIME and tell sybase what the format is to be sure.
According to this documentation the code for MM/DD/YYYY is 101, so something like this:
SELECT *
FROM aTable
WHERE afterDate >= CONVERT(DATETIME,'08/07/2013',101)
AND beforeDate <= CONVERT(DATETIME,'08/08/2013',101)
You can see the difference by running the following select statements:
SELECT CONVERT(DATETIME,'08/07/2013',101) --MM/DD/YYYY (2013-08-07 00:00:00.000)
SELECT CONVERT(DATETIME,'08/07/2013',103) --DD/MM/YYYY (2013-07-08 00:00:00.000)
For any date-time field in sybase, instead of going through the convert function, there is a more direct approach.
SELECT *
From aTable
WHERE afterDate >= '2013-08-07'
AND beforeDate <= '2013-08-08'
The date has to be in the form 'YYYY-MM-DD'
If you want to add a time, it can be included along with the date. The date and the time have to be separated by a T.
Any date time field can be directly used using the format 'YYYY-MM-DDTHH:MM:SS'
Using the functions is too lengthy. Noone needs a bazooka to shoot a squirrel! :)
CAST( '2000-10-31' AS DATE )
will convert from text to date format....
I am assuming that your two fields (afterDate and beforeDate) are in Date format.
Your example would be:
SELECT *
From aTable
WHERE afterDate >= CAST( '08/07/2013' AS DATE )
AND beforeDate <= CAST( '08/08/2013' AS DATE )
Also, usually (but not always) a date range is on the SAME field. As I said, that is not true all the time and you may have a good reason for that.
The best approach is to use the ANSI standard which does not require any conversion: yyyymmdd (you can also include hh:mm:ss) for instance:
DateField1 >= "20150101" and DateFile1 <= "20150102"
You should decide which Input-Strings the user is going to use as parameter and then convert them and concatenate them like you want, unless it is Datetime it is not important which initial format it had, you can use it in a between-condition.
E. g. the user is from Europe and uses "DD.MM.YY" and "hh:mm" as an input parameter, I would convert and concatenate like this:
WHERE dateCol between convert(DATETIME,
convert(char(11),
convert(DATETIME, '01.06.14', 4), 16) || ' ' || '00:00', 8)
AND convert(DATETIME,
convert(char(11),
convert(DATETIME, '01.07.14', 4), 16) || ' ' || '16:00', 8)
Much like this question: "How to get Time from DateTime format in SQL?", I am trying to select only the time from a datetime, but unlike the above question, I would like to know how to do it in SQL Server Compact. Does anyone know how to do this?
SELECT DATEPART(hour, OrderDate), DATEPART(minute, OrderDate) FROM MyOrders
Ref. http://msdn.microsoft.com//library/ms173998%28v=sql.90%29.aspx
Solved it. Luigi's answer is not actually the correct one but I have upvoted it, as it helped me find the answer.
To get only the time from a datetime in SQL Server Compact, the proper query is:
select ltrim(str(DATEPART(hour, columnName))) + ':' + ltrim(str(DATEPART(minute, columnName))) + ':' + ltrim(str(DATEPART(second, columnName))) from table
Example (not working like this):
SELECT * FROM User
WHERE LastActivity > Date(1980,1,1)
Where Date should use the parameters year, month, day as integer numbers. To avoid troubles with locales, I explicitly do not want to use a string like "yyyy/mm/dd" or similar.
I am working with Microsoft SQL Server Management Studio on an MSSQL Database, if this matters.
Note: I am sure this is trivial, but I could not find the solution neither using google or SO.
To avoid troubles with locales, I explicitly do not want to use a
string like "yyyy/mm/dd" or similar.
To avoid this, the best way is passing the date as language-neutral like YYYYMMDD. This way it will be language independent:
SELECT * FROM User
WHERE LastActivity > '19800101';
Use ISO date format which is yyyymmdd
SELECT * FROM User
WHERE LastActivity > CONVERT(DATE, (CONVERT(VARCHAR(4), #Year) +
RIGHT('0' + CONVERT(VARCHAR(2), #Month),2) +
RIGHT('0' + CONVERT(VARCHAR(2), #Date),2) ))
I have always found that most databases will accept 'dd-mmm-yyyy' as a date parameter and I use it almost everywhere. There are some who don't (E.g. MS/Access) but SQL/Server does. This format is widely accepted, though obviously you may need to make it language specific.
SELECT * FROM User WHERE LastActivity > '11-Dec-2012';
I think the 'Convert' function does what your looking for. To make the 20th of March 1980, do the following:
Convert(date, '1980-03-20')
Your exact query would be:
SELECT * FROM User
WHERE LastActivity > Convert(date, '1980-01-01')
If you are using SQL Server 2012 and up, you can use the simpler DATEFROMPARTS function:
SELECT * FROM User
WHERE LastActivity > DATEFROMPARTS(1980,01,01)
I need to get the current 2 digit year and increment by one. So the current number I'm looking for should be 11. How?
You can do ( YEAR( GETDATE() ) % 100 ) + 1
See GETDATE & YEAR
This will work for you
select Right(Year(getDate())+ 1,2)
For SQL Server 2012 and above, I'd suggest using FORMAT(#DATE, 'yy'):
SELECT FORMAT(DATEADD(year, 1, GETDATE()), 'yy')
Format offers a cleaner, more readable solution. Thus, less guesswork, and a better maintainability.
SELECT RIGHT(CONVERT(VARCHAR(8), GETDATE(), 1),2) as YEAR
You can try this in sql server
SELECT FORMAT(GETDATE(), 'yy')
If you are always going to be using GetDate() why not just do something like this:
Select (Year(GetDate()) - 2000) + 1
Dang people. Always making things so complicated. It's not like you are going to be living for another 1000 years!
select CAST( DAY(GETDATE()) as varchar(10))+'/'+CAST( month(GETDATE()) as varchar(10))+'/' +cast(right(year(getDate()),2) as varchar)