How to apply a particular format of date of birth in a SQL query? - sql

Suppose I want a particular format of date of birth, like (mm-dd-yyyy).
How can I do that in SQL?

Although some people have listed the proper syntax for this in multiple RDBMSs (you really need to indicate which one you're using), I'd like to point out that formatting your data is typically something that should be done by the front end of your application. That's not to say that there's never a reason to do formatting in SQL, but usually it's best to just pass it as a date/time to the front end and let the front end handle how it will represent it to the user. Hopefully, you understand the difference between a date/time and a string.

Use CONVERT function if it is SQL SERVER
Select CONVERT(VARCHAR(10),Birthdate,110) from YourTable

Assuming your RDBMS is SQL Server, you can do the following:
SELECT CONVERT(VARCHAR(10), [DateOfBirth], 110)
There's more information about date formatting here.

SQL Server
SELECT convert(varchar, getdate(), 110)
SELECT convert(varchar, DateOfBirth, 110) FROM YourTable
How to format datetime & date in Sql Server 2005

If you're using Oracle:
select to_char(your_date_column,'MM-DD-YYYY') from your_date_table;

Related

Trying to convert SQL getdate into varchar with no in-between characters

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')

How can I switch the Month and Day when the column format is M/D/Y?

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.

SQL CE SELECT Convert

I'm trying to convert a DateTime field (Date of Birth), within the an SELECT FROM that needs to work for SQL CE and SQL Access databases.
I've found a thread that shows me that it can be done, but not the formate I want it to be. Does anyone know how I can change this to show yyyy-mm-dd format not mm/dd/yyyy
SELECT CONVERT(nvarchar(10), C.DOB,101) FROM table
You can use the following query
SELECT CONVERT(NVARCHAR(10), C.DOB, 120) AS FormatedDate FROM Table
I've been able to find how I can do it. Its using the convert function, but the code passed into it is different
So to get the correct format, I need the below code
CONVERT(nvarchar(10), A.TestDate,21)
enter link description here

How do I display varchar date string with mixed format to another format?

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

Comparing dates in Sql

I am having trouble trying to compare dates in my sql query:
SELECT restrictions, restrictions_adddate FROM restrictions WHERE id = '555555'
AND restrictions_adddate BETWEEN ? and ?;
Both of the parameters system print in the format 'mm-dd-yyyy'. I am getting a wrong date format error, but have tried doing it in multiple ways. Any help is appreciated, and if more info is needed, please let me know. thanks.
This should work on any standard compliant DBMS:
SELECT restrictions, restrictions_adddate
FROM restrictions
WHERE id = '555555'
AND restrictions_adddate BETWEEN DATE '2011-01-01' and DATE '2011-12-31';
This is my preference:
AND restrictions_adddate BETWEEN
CONVERT(datetime, FLOOR(CONVERT(float, CAST('01-01-1900' AS DATETIME))))
AND CONVERT(datetime, FLOOR(CONVERT(float, Getdate())));
This allows you to compare dates (minus the time stamps).
Try checking out the CONVERT function T-SQL reference in BOL. You should be able to use this dictate the format of your dates.