Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8772481
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:02:36+00:00 2026-06-13T18:02:36+00:00

Simple PL/SQL question from a beginner: What is the proper PL/SQL syntax for creating

  • 0

Simple PL/SQL question from a beginner: What is the proper PL/SQL syntax for creating a new variable in an existing dataset?

I’d like to extract the year from a date field (svcdat) in an insurance claims dataset (rpt_claim). The date field is numerical and follows the format YYYYMMDD, for example 20120704 for July 4, 2012.

My first pass at creating a “year” variable was unsuccessful:

declare
  year number(4);
begin
  select svcdat into year 
  from rpt_claim
  where year=floor(svcdat/10000);
end;

Any help is much appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:02:37+00:00Added an answer on June 13, 2026 at 6:02 pm

    To extract year from a date you probably will use extract function, but before that, as you store your dates as numbers, you have to convert them to date datatype using to_date function. Here is an example(oracle 11g is used):

    -- For the sake of simplicity this table contains only one column.
    SQL> create table rpt_claim(
      2    date_col number(8)
      3  )
      4  /
    
    Table created
    
    SQL> insert into rpt_claim(date_col) values(20120704);
    
    1 row inserted
    
    SQL> commit;
    
    Commit complete
    
     SQL> declare
      2    l_year number(4);
      3  begin
      4    select extract(year from to_date(date_col, 'yyyymmdd'))
      5      into l_year
      6      from rpt_claim
      7    where rownum = 1;
      8    dbms_output.put_line(to_char(l_year));
      9  exception
     10    when no_data_found
     11    then dbms_output.put_line('No data has been selected');
     12  end;
     13  /
    
    2012                  --<-- result
    
    PL/SQL procedure successfully completed
    

    Note, in the example above the query returns 1 (first selected) row, because it was specifically asked (where rownum = 1) to do that. In your case will probably more than one record be returned by the query and to handle that you have to use cursors and cursor FOR loop(for example) to process returned data or collections.

    Here is an example of using collections:

    -- add more records 
    SQL> insert into rpt_claim(date_col) 
      2    select 20120704 + level 
      3      from dual 
      4    connect by level < = 5;
    
    5 rows inserted
    
    
    SQL> commit;
    
    Commit complete
    
    SQL> select * from rpt_claim;
    
     DATE_COL
    ---------
     20120704
     20120705
     20120706
     20120707
     20120708
     20120709
    
     6 rows selected
    
    SQL> declare
      2    type t_years is table of number(4);
      3    l_year t_years;
      4  begin
      5    select extract(year from to_date(date_col, 'yyyymmdd'))
      6      bulk collect into l_year
      7      from rpt_claim;
      8  
      9    if l_year is not empty
     10    then
     11      for i in l_year.first..l_year.last
     12      loop
     13        dbms_output.put_line(to_char(l_year(i)));
     14      end loop;
     15    else
     16      dbms_output.put_line('No data has been selected');
     17    end if;
     18  end;
     19  /
    
    2012
    2012
    2012          --<-- our result
    2012
    2012
    2012
    
    PL/SQL procedure successfully completed
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Simple SQL syntax question. I'm writing a script to move data from an old
I have a seemly simple question with SQL that I am having trouble figuring
I created the following simple PL/SQL stored procedure example to ask a specific question.
Simple question: Office debate about whether the keyword AS is necessary in our T-SQL
I have this very simple sql statement: SELECT max_dose FROM psychotropes WHERE (patient_meds.psychotrope =
I have a simple SQL statement that is to get the first rowid from
Quite a simple question. In SQL 2008 if I have a stored procedure (see
With regards to this question here SQL CLR return two new columns I am
Got this as an interview question from Amazon to test basic SQL skills and
This is a simple question: I know and have heard from almost everyone that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.