I have a list of auto-generated tables in MS-SQLServer that differ only in the date listed at the end of the name.
The challenge is that I want to create a script that always references a table 'x days back'.
So for example if the table name would be:
dbo.tablename_20200825
I can get "close" to the date format I need as a string with the following statement and style
select convert(varchar(10), getdate(), 102);
However I still have those periods of separation between each part of the date.
How do I make the resulting string appear as '20200825' instead of '2020.08.25'
Thank you as always for any insight and help.
I think you're looking for 112
select convert(varchar(10), getdate(), 112);
Results
20200825
You can use format() to get exactly what you want:
select format(getdate(), 'yyyyMMdd')
Related
I am not sure if this has already been answered but I could not find anything.
I am trying to convert the column of dates from MM/DD/YYYY to DD/MM/YYYY.
I must confess that I don't even know where to begin on this. I was thinking I might try an update statement but I am unsure as to how to format it. Any help would be appreciated.
You need to do two level of conversation :
select convert(varchar(12), convert(date, datecol, 101), 103)
In general, you need to fix the data-type (if that is in your hand) instead of doing conversation in SQL. Date formatting should be done at the presentation layer.
You can try this by fixing the data type in the actual table. For displaying purpose either on a webpage, reports or whatever.
SQL Server provides a number of options you can use to format a date/time string. In select, you can try one of the suggested methods as here.
For all these conversions you need to pass the date values in proper data type which may be the date or date-time.
Here is one of the examples of your illustration.
declare #DateInString varchar(20) = Cast(getdate() as Varchar(20))
select convert(varchar(12), convert(date, #DateInString, 101), 103)
You could also do Select Format(datecol, 'dd/MM/yyyy')
This will return your datetime field as a varchar - you should then be able to insert that into your target.
I have a table that contains date and the format is :'01-16-1989' which is mm-dd-yyyy but I want to insert to another table that has format like this: '1989-01-16' which is yyyy-mm-dd. What function can I use in the insert statement to do this?
insert into des_table
select date from source_table
How to update the second line in order to finish the date format conversion?
You can convert a string from mm-dd-yyyy format to a date using conversion type 110:
select convert(date, [date], 110)
from source_table
You can then convert this back to a string in the yyyy-mm-dd format using code 120:
select convert(varchar(10), convert(date, [date], 110), 121)
You can format date with DATE_FORMAT()
This is one way
SELECT DATE_FORMAT(date, '%Y-%m-%d') FROM source_table;
Source: http://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-format
Unless they are not actually datetime data types, your insert should be golden as it is.
Also, to answer this you should tell us what RDBMS you are using.
If you are using MySQL this should get you covered:
select DATE_FORMAT(current_date,'%y/%m/%d');
On the dates note you should keep all your dates
ISO formated
Since your column is already a DATE you don't need to do any conversions on the insert. If you want it to look a certain way in a specific query result, you can use CONVERT. There are many format options, but again, you don't need to bother with changing how it looks on the insert.
Here are some resources
https://msdn.microsoft.com/en-us/library/ms187928.aspx
https://technet.microsoft.com/en-us/library/ms187928(v=sql.105).aspx
http://www.w3schools.com/sql/func_convert.asp
I use SQL server 2014, i try the following query to select between two dates in the same table, the datatype is nvarchar, i executed the following query it just shows me three rows such('30/03/2015','30/04/2015','30/04/2015'),but in reality there is('29/02/2015','30/03/2015','31/04/2015','30/04/2015','30/04/2015')
select RegisteredDate
from Student
where Student.RegisteredDate between convert(nvarchar, '30/01/2014', 103)
and convert(nvarchar, '30/04/2015', 103)
Cast the other way round, other way you are comparing strings:
select RegisteredDate from Student
where convert(date, Student.RegisteredDate, 103) between '20140130' and '20150430'
The fact is that those dates saved as strings are ordered as:
'29/02/2015',
'30/03/2015',
'30/04/2015',
'30/04/2015',
'31/04/2015'
Now imagine where would you add filter values?
'29/02/2015',
'30/01/2014' --start date
/-------------\
|'30/03/2015',|
|'30/04/2015',|
|'30/04/2015',|
\-------------/
'30/04/2015' --end date
'31/04/2015'
So between will return you those three rows.
Also you have in your data 29/02/2015. In 2015 February ends on 28(you have incorrect data in tables already). You will never be able to insert such values if you choose types correctly.So the conclusion is:
Use Appropriate data types for your data!
As i have read the other answers and comments, i could recommend you to firstly change the datatype of "RegisteredDate" from "nvarchar" to "date". Secondly use this standard 'yyyy-MM-dd' below code is what you need
select RegisteredDate
from Student
where Student.RegisteredDate between '2014-01-30' and '2015-04-30'
you will not be in need of any conversions, this is how i do it for myself
Try to cast your string to date instead of casting all the dates in the table to string.
You are casting all the records in the table to string and it might be millions of them. This way not only your performance will be better but more important you will compare them to dates, not as String.
SELECT RegisteredDate
FROM Student
WHERE Student.RegisteredDate BETWEEN Convert(Date, '30/01/2014',103) AND Convert(Date, '30/04/2015', 103)
As I said in the comments, a string comparation uses alphabetical order one char after another starting from the left. For example: 20/01/1302 will be after 01/12/4016 as the "2" char goes after the "1" char in ASCII.
Update: convert Student.RegisteredDate to date if it is still in nvarchar type. I would recommend you to change the type if you can to this. It might be a source of bugs and performance problems if you do not do this.
SQL Server 2014 makes the string to dates conversion automagically but only if it needs to. Comparing a '30/04/2015' string with a nvarchar is just a String comparation.
I am using SQL server 2008 R2. I know I can use CONVERT with different format code as the third parameter to do the conversion to DATETIME first and CONVERT again to VARCHAR with another format code to change the display format.
The real problem now is I have mixed raw data in a single column. So my question is how do you write a single SELECT statement to display from mixed YYYY/MM/DD, DD/MM/YYYY all to DD/MM/YYYY?
I tried to use ISDATE() but it think 31/01/2013 is not a date while 01/01/2013 is a date. Now I could only think of to see if the YYYY is on the left or on the right to determine the correct input format, but I dont know how to write it out in a single SELECT statement.
Any procedure to change the format first then do a simple SELECT is not an option. I am not allowed to change the source.
Thank you
Why not just use string manipulations? Something like:
select (case when substring(d, 5, 1) = '/' -- YYYY/MM/DD
then right(d, 2)+'/'+substring(6, 2)+'/'+left(d, 4)
else d
end)
By the way, if you are choosing formats for dates when represented as strings, I highly recommend YYYY-MM-DD (or YYYY/MM/DD) because comparison operators work on them.
If you are sure that only those 2 formats (yyyy/mm/dd and dd/mm/yyyy) exist in the data, then you could probably get away with a CASE statement along the lines of:
CASE
WHEN (SUBSTRING(dateColumn, 5, 1) = '/') THEN CONVERT(datetime, dateColumn, 111)
ELSE CONVERT(datetime, dateColumn, 103)
END
The task:
I need to count every record in a table that was added after 07/01/2011.
Info:
There is a column in the table "Date_added" which is formatted as varchar and contains dates in format mm/dd/yyyy.
What I've tried:
I tried the following query:
SELECT count(date_added) FROM Abatements_Doc WHERE date_added >= '07/01/2011'
Unfortunately it counted any date where the month was greater than 07 regardless of the year or day. I'm at a loss as to how I should format my query to get it to read the dates properly instead of by ascii sort. In my research I discovered the CAST and CONVERT options, but am not sure how to use them, or which one I should use if any. I'm extremely new to SQL, and want to make sure I don't mess up the data in the table. I just need to get the count as specified above without altering the data in the table in any way. Any help in this regard will be very much appreciated. Thanks in advance.
I didn't test. But try convert statement.
SELECT count(date_added)
FROM Abatements_Doc
WHERE convert(datetime,date_added,1) >= convert(datetime,'07/01/2011',1)
(1) don't use mm/dd/yyyy as your date format. Use a safe and unambiguous format; the only one I trust for DATETIME/SMALLDATETIME is YYYYMMDD.
(2) change the data type in your table to DATETIME/SMALLDATETIME/DATE depending on accuracy required. Then `WHERE date_added >= '20110701' will work just fine (and will use an index on that column, if one exists).
(3) if you can't change the data type, then the following will work just fine as well (though no index will be used):
WHERE CONVERT(DATETIME, date_added, 101) >= '20110701'
Varchar can't be compared, try changing your column to date type.
Convert both date_added column values and your target date to DATETIME and compare; as in:
SELECT COUNT(1)
FROM Abatements_Doc
WHERE (CONVERT(datetime, date_added, 101)) >= CONVERT('07/01/2011', date_added, 101))