I have 2 tables in my database.
Customers_Details
Transaction
Both tables have a date column. I want to change the date format from YYYY-MM-DD to DD-MM-YYYY permanently for both tables.
Can i do it in one query or do I have to write separate query for each table?
The query which I have written is not changing the format of date permanently.
select
[customer_ID], [Gender], [Area_code],
convert(varchar, dob, 105) as DOB
from
[dbo].[customer_details]
dates are stored in an internal format. You wouldn't want to look at that string of bits anyway. But you do want to store the data that way -- to use date functions, so ordering is correct, and so on.
What you can do is add a computed column that has the string form that you want:
alter table [dbo].[customer_details] add dob_ddmmyyyy as (convert(varchar(10), dob, 105);
You can then use dob_ddmmyyyy in queries.
You can use following method, (tested in SQL 2014 and higher)
select FORMAT (GETDATE(), 'yyyy-MMM-dd')
In your case it would be as follows:
select [customer_ID],[Gender],[Area_code],
format(dob, 'dd-MM-yyyy') as DOB
from [dbo].[customer_details]
Related
I'm new to SQL, using SSMS SQL 18.12. I've been searching and know how to convert yyyy-mm-dd to dd-mm-yyyy using SELECT. Just wonder whether there is a way to store this format from the start.
CREATE TABLE BILL
(
BILLCODE NVARCHAR(10) PRIMARY KEY,
ORDERDATE DATE,
CUSTOMERCODE NVARCHAR(5)
FOREIGN KEY REFERENCES CUSTOMER(CUSTOMERCODE),
TOTAL MONEY
)
INSERT INTO BILL
VALUES ('HD001','20220601','KH01',82000),
Result is 2022-06-01 in the ORDERTIME column. But I need 01-06-2022. And I also need 01/06/2022 format.
PS: I managed to display it with
SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 103), '-', '/') AS TIME FROM BILL
Thank you!
As far as I know sql server saves date as binary so you don't need to convert it when you make the column you can just convert it when you select the data
To do it you can use this as the query
SELECT FORMAT (getdate(), 'dd-mm-yyyy') as date
This was asked several times but I wasn't able to implement any of the answers.
I have a table with a column with Dates that has been created as VARCHAR, I want to convert it to Date(103).
I've seen several responses with the following:
SELECT CONVERT(VARCHAR(10), GETDATE(), 103) AS [DD/MM/YYYY]
This will work nicely to convert the result of a query...but I want to convert the actual column not just when I do a select query.
I probably just don't understand where to put this line of SQL code. Can anyone help me out?
There is no such thing as DATE(103); dates are stored using an internal format. I'll assume that is the format of the string.
What to do? Change the string to something that can be implicitly converted to a date. Then change the column:
update t
set datecol = convert(date, datecol, 103);
alter t alter datecol date;
If this returns an error, you need to figure out where the values don't convert properly. That would use:
select datecol
from t
where try_convert(date, datecol, 103) is null and
datecol is not null;
You'll need to figure out how to fix the broken values. And in the process learn one important reason not to store date/time values as strings.
When I try to insert into tableA with
insert into tableA
values (convert(date, '12-1-2012', 105))
then I try to
select * from tableA
it always shows the dates in yyyy-mm-dd format
But when I use
select CONVERT(varchar, thedate, 105) from tableA
then it shows dd-mm-yyyy
Can I make even select * always shows the dd-mm-yyyy format?
Like changing the default display of date format?
The best way to do this would be to make a view, and set one of the fields in the view as:
CONVERT(varchar,thedate,105) AS thedate
and reference your VIEW in your select statement instead of your TABLE name.
Maybe this would help:
How to change Date Format after installing SQL server
I'm inserting data into a table and before inserting I need to check if data exists.
I have a composite key consisted of two columns of datetime and int.
Before inserting I need to check if the data with the same time and id exists in the table.
The date that user is inserting is in 'mm/dd/yyyy'.
The datetime data in the table looks like this: '2016-01-12 00:00:00.000'.
The id field is int.
So, I have a query:
if not exists(select count(*) from table_1 where MyDate = #myDate and id = #id)
insert into table_1 .....
What is the right way to format the date user sends to match the datetime format in the table?
Check this sqlfiddle about how to use different date formats in your query. Might help you to solve it.
http://www.sqlfiddle.com/#!2/fd0b7/5
I am guessing that the question is about SQL Server, based on the syntax. The issues in the code snippet far transcend date formats.
First, the expression:
if not exists(select count(*) from table_1 where MyDate = #myDate and id = #id)
will never return true, because the subquery always returns one row with one column. If nothing matches, the column contains 0, which does exist.
You intend:
if not exists(select 1 from table_1 where MyDate = #myDate and id = #id)
Second, this check is not necessary if you wisely choose to have the database enforce the uniqueness constraint. So, define a unique index or constraint on the two columns:
create unique index unq_table_1_id_mydate on table_1(id, MyDate);
Now, the database won't let you insert duplicate values and no if is necessary.
Next, I would suggest that you fix the date format at the application layer. YYYY-MM-DD is an ISO standard date format and quite reasonable. However, if you don't want to do that, use convert():
insert into table_1(id, MyDate, .....)
select #id, convert(datetime, #MyDate, 101), . . .
The value in the database looks to be correct stored as a date/time value, so this should work fine.
You can use following line to convert date to required format in SQL server:
select FORMAT(#your_date, 'yyyy-MM-dd HH:mm:ss', 'en-US') from Your_Table
I have a CSV file Date column with the following values:
StartDate
---------------------------
01/02/2014 0:00
09/04/2013 0:00
I want to extract only the date part from the above column value. I created the table with data type as Varchar, because if I declare it as DATETIME then I am not able to perform a bulk insert.
Date part alone you can extract using LEFT function.
(provided all your data in the format as you specified above)
select LEFT(colname,10)
You can BULK INSERT into a table where you will keep the data for intermediary storage. Try something like:
SELECT id, LEFT(CONVERT(VARCHAR, dateCol, 103), 10) FROM temptbl
Take a look at the following MSDN page for date formats available to you with CONVERT():
http://msdn.microsoft.com/en-us/library/ms187928.aspx
get the date part by splitting by "space"
cols[col_num].split(" ")[0]
and then make insert statement.
Why the table has datatype varchar?? why not date?