I have an assignment to change the existing date format in an existing T-SQL view statement. Where the date format is set to YYYY-MM-DD 00:00:00.000 the task is to change is to ALTER VIEW to MM-DD-YY formatting. I'm lost on this one.
USE Ch8_simpleco
ALTER VIEW
AS invoice
SELECT INV_DATE
SET INV_DATE (MMDDYY)
FROM dbo.v_cust_invoices;
You are probably looking for something like this:
ALTER VIEW invoice
AS
SELECT COVERT(char(8), INV_DATE, 10) -- will get you dd-MM-yy (no century!)
FROM dbo.v_cust_invoices
You can use FORMAT() like this:
ALTER VIEW invoice
AS
SELECT FROMAT(INV_DATE,'MM-dd-yy') as INV_DATE
FROM dbo.v_cust_invoices
Related
I am using the latest SQL Server. I have a table with a CreatedDate column. I need to write a Query that uses dates that are plus or minus 7 from the Date in CreatedDate. I have no clue how to go about this. My thought was this:
DECLARE #Date datetime
DECLARE #SevenBefore datetime
DECLARE #SevenAfter datetime
SET #Date = CreatedDate
SET #SevenBefore = DATEADD(day,-7,#Date)
SET #SevenAfter = DATEADD(day,7,#Date)
SELECT *
FROM <table>
WHERE <table> BETWEEN #SevenBefore AND #SevenAfter
The issue with this is that I cannot use "CreatedDate" as a SET #DATE because SQL gives an error "Invalid column name 'CreatedDate'"
Any help would be appreciated. I cannot list a date because every date in that column could be different.
Thanks
In this case, you need to stop thinking as a programmer would, and start thinking as a Database programmer would.
Lets work only with this central part of your query:
SELECT *
FROM <table>
WHERE <table> BETWEEN #SevenBefore AND #SevenAfter
Now, you say that the CreatedDate is a column in a table. For this example, I will assume that the CreatedDate is in a table other than the one in your example above. For this purpose, I will give two fake names to the tables. The table with the CreatedDate, I will call tblCreated, and the one from the query above I will call tblData.
Looking above, it's pretty obvious that you can't compare an entire table row to a date. There must be a field in that table that contains a date/time value. I will call this column TargetDate.
Given these assumptions, your query would look something like:
SELECT *
FROM tblCreated tc
INNER JOIN tblData td
ON td.TargetDate BETWEEN DATEADD(day, -7, tc.CreatedDate) and DATEADD(day, 7, tc.CreatedDate)
Looking at this, it is clear that you still need some other associations between the tables. Do you only want all data rows per customer based on the Created date, or perhaps only want Creations where some work was done on them as shown in the Data records, or ??. Without a fuller specification, we can't help with that, though.
I'm trying to change the date format of a column stored in a view by doing an ALTER VIEW statement, but it's not working and I'm not sure why.
My query:
ALTER VIEW v_cust_invoices
AS
SELECT
FORMAT(INV_DATE,'MM-dd-yy') as INV_DATE
FROM
v_cust_invoices
I always get the error
View or function 'v_cust_invoices' contains a self-reference. Views or functions cannot reference themselves directly or indirectly
I'm trying to change the date format of INV_DATE to mm-dd-yy (it's currently yy-mm-dd). Can someone help me? Not sure what I'm doing wrong.
Edit: If I do
ALTER VIEW v_cust_invoices
AS
SELECT FORMAT(INV_DATE,'MM-dd-yy') as INV_DATE
FROM INVOICE
instead, it deletes all of the columns except for INV_DATE.
View definition:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[v_cust_invoices] AS
SELECT
CUSTOMER.CUST_NUM, CUST_LNAME,
CUST_BALANCE, INV_NUM, INV_DATE, INV_AMOUNT
FROM
CUSTOMER, INVOICE
GO
You must change the query so that it does not reference itself.
Look at the current definition of the v_cust_invoices view, at its FROM clause. You will see a reference to a table or to some other view. replace the last line in your query with the last line below, substituting the name of the table (or other view) this view gets it's data from.
ALTER VIEW v_cust_invoices
AS
SELECT FORMAT(INV_DATE,'MM-dd-yy') as INV_DATE,
[Plus all the rest of the output columns
from CURRENT definition of v_cust_invoices]
FROM [here Put the table or other view that is in current view definition]
or, now that op has posted complete view definition:
ALTER VIEW [dbo].[v_cust_invoices] AS
SELECT FORMAT(INV_DATE,'MM-dd-yy') INV_DATE,
CUSTOMER.CUST_NUM, CUST_LNAME,
CUST_BALANCE, INV_NUM, INV_DTE, INV_AMOUNT
FROM CUSTOMER, INVOICE
GO
I have a view with a field for date as a varchar. I need to change it to date time using the following code
CONVERT(DATETIME, MyDates)
This works fine when executing the view but I want to make the change permanent. I need some help with the syntax. So far I have
ALTER VIEW tableName
CONVERT(DATETIME, MyDates)
but it's obviously not working
Since a view (unless it's a materialized/indexed view which has some extra peculiarities) is more or less just a stored select query, what you do is to just change the select query and alter the view using that.
For example, if you have the view;
CREATE VIEW testview AS
SELECT id, value FROM test;
...where value is a varchar and you want it to be reflected in the view as a datetime, you can just issue;
ALTER VIEW testview AS
SELECT id, CAST(value AS DATETIME) value FROM test;
...to make it appear as a datetime in the view.
An SQLfiddle with a simple demo.
A view only fetches the data from the table as per the query.So you cannot change the datatype of the view. you have to change it in table.
I have a column BirthDate in a table that is using the datetime data type. Currently, the values resemble the format 1987-12-30 00:00:00.000.
I would like to update all the rows of this table, changing them to the following format with the date data type: 1987-12-30
I can run a SELECT...
SELECT CONVERT(date, BirthDate) FROM CUSTOMERLIST
But this is only affecting the display. I would like for it to update the entire column and also change the data type of that attribute as well. Any assistance would be greatly appreciated!
You will need to ALTER the table:
ALTER TABLE CUSTOMERLIST ALTER COLUMN BirthDate date not null
See Sql Fiddle with demo
i want to convert date to Persian date (jalali) in oracle Database
there is any function ?
You can use this statement:
select to_char(hiredate,'yyyy/mm/dd','nls_calendar=persian') from emp
hiredate is a date field, with this select hiredate is displayed in persian
First of all you must set nls_calendar to Persian like below:
Alter session set nls_calendar=persian;
Now you can change format of date like below:
Select to_char(sysdate,'yyyy/mm/dd') from dual;